Created
October 17, 2019 15:53
-
-
Save team172011/8fc836c607ec2ea7abdbdc1e43ce901a to your computer and use it in GitHub Desktop.
Unzip a zip file with all zip entries in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void unzipToParentDir(File file) throws IOException { | |
try (ZipFile zipFile = new ZipFile(file)) { | |
Enumeration<? extends ZipEntry> entries = zipFile.entries(); | |
while (entries.hasMoreElements()) { | |
ZipEntry entry = entries.nextElement(); | |
File entryDestination = new File(file.getParent(), entry.getName()); | |
if (entry.isDirectory()) { | |
entryDestination.mkdirs(); | |
} else { | |
entryDestination.getParentFile().mkdirs(); | |
InputStream in = zipFile.getInputStream(entry); | |
OutputStream out = new FileOutputStream(entryDestination); | |
IOUtils.copy(in, out); | |
IOUtils.closeQuietly(in); | |
out.close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment