java 解压多层zip 包
时间: 2023-10-21 07:07:30 浏览: 20
可以使用 Java 的 ZipInputStream 类来解压多层 zip 包。这个类可以从一个输入流中读取压缩文件的内容,并将其解压缩成单个文件。
以下是一个解压多层 zip 包的示例代码:
```
public static void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
// 遍历所有层级的 zip 包,并逐层解压
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zipIn, filePath);
} else {
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[4096];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
```
这个代码会遍历所有层级的 zip 包,并逐层解压,直到解压出所有的文件。