Created
February 12, 2018 13:40
-
-
Save viet-wego/bae618bebc62d303826d1a05ebe5dc3d to your computer and use it in GitHub Desktop.
Unzip a file with java 8
This file contains hidden or 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
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