81,095
社区成员
发帖
与我相关
我的任务
分享
public static void unzip(ZipFile zipFile, String localPath, int bufferSize) {
byte[] buffer = new byte[bufferSize];
ZipInputStream zip = null;
ZipEntry zipEntry = null;
try {
zip = new ZipInputStream(new FileInputStream(zipFile.getName()));
while ((zipEntry = zip.getNextEntry()) != null) {
File file = new File(localPath, zipEntry.getName()).getAbsoluteFile();
System.out.println(file.getName());
if (zipEntry.isDirectory()) {
file.mkdirs();
} else {
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
file.createNewFile();
long size = zipEntry.getSize();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
int readLen = -1;
while (size != 0) {
readLen = zip.read(buffer);
size -= readLen;
if (readLen != -1) {
fos.write(buffer, 0, readLen);
}
}
} finally {
if (fos != null) {
fos.close();
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (zip != null) {
zip.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}