Skip to content

Instantly share code, notes, and snippets.

@viet-wego
Created February 12, 2018 13:40
Show Gist options
  • Save viet-wego/bae618bebc62d303826d1a05ebe5dc3d to your computer and use it in GitHub Desktop.
Save viet-wego/bae618bebc62d303826d1a05ebe5dc3d to your computer and use it in GitHub Desktop.
Unzip a file with java 8
public static List<String> unzip(String zipFilePath) {
try {
File zipFile = new File(zipFilePath);
try (ZipInputStream inputStream = new ZipInputStream(new FileInputStream(zipFile))) {
List<String> fileList = new ArrayList<>();
ZipEntry entry = inputStream.getNextEntry();
while (entry != null) {
File newFile = new File(zipFile.getParent() + File.separator + entry.getName());
new File(newFile.getParent()).mkdirs();
if (!entry.isDirectory()) {
try (FileOutputStream outputStream = new FileOutputStream(newFile)) {
int length;
byte[] buffer = new byte[1024];
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
}
}
fileList.add(newFile.getAbsolutePath());
entry = inputStream.getNextEntry();
}
inputStream.closeEntry();
return fileList;
}
} catch (Exception e) {
System.out.println(String.format("unzip error: %s", e));
return Collections.emptyList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment