public class FileZipUtil {
public static void unzip(String zipFilePath, String destDir) throws IOException {
File dir = new File(destDir);
if (!dir.exists()) {
dir.mkdirs();
}
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath))) {
ZipEntry entry = zis.getNextEntry();
while (entry != null) {
String filePath = destDir + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zis, filePath);
} else {
File subDir = new File(filePath);
subDir.mkdirs();
}
zis.closeEntry();
entry = zis.getNextEntry();
}
} catch (IOException e) {
throw new IOException(e);
}
}
private static void extractFile(ZipInputStream zis, String filePath) throws IOException {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
byte[] buffer = new byte[1024];
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
}
}