Skip to content

Instantly share code, notes, and snippets.

@oldshensheep
Created December 4, 2022 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oldshensheep/bea5bd4c313f27884bc87dced9917bf6 to your computer and use it in GitHub Desktop.
Save oldshensheep/bea5bd4c313f27884bc87dced9917bf6 to your computer and use it in GitHub Desktop.
java unzip
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.zip.ZipFile;
public class S {
public static void main(String[] args) throws IOException {
f("abc.zip", "./");
}
public static void f(String filePath, String dst) throws IOException {
try (ZipFile zipFile = new ZipFile(filePath);) {
String destDir = "./";
zipFile.stream().forEach(zipEntry -> {
if (zipEntry.isDirectory()) {
if (new File(destDir, zipEntry.getName()).mkdirs()) {
// Handle errors
}
} else {
try (var fos = Files.newOutputStream(new File(destDir, zipEntry.getName()).toPath(),
StandardOpenOption.CREATE_NEW);) {
fos.write(zipEntry.getExtra());
} catch (IOException e) {
// Handle errors
}
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment