java 解压多层zip 包
时间: 2023-07-01 14:24:55 浏览: 36
可以使用 Java 自带的 ZipInputStream 类来解压多层 ZIP 包。
以下是一个示例代码:
```java
import java.io.*;
import java.util.zip.*;
public class UnzipMultipleLevels {
public static void main(String[] args) {
String zipFilePath = "path/to/your/zip/file.zip";
String unzipFolderPath = "path/to/your/unzip/folder";
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath))) {
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
String entryName = zipEntry.getName();
File file = new File(unzipFolderPath + File.separator + entryName);
if (zipEntry.isDirectory()) {
file.mkdirs();
} else {
// create parent directory if not exists
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
// write file content
FileOutputStream outputStream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = zipInputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
}
zipEntry = zipInputStream.getNextEntry();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
该代码可以递归地解压多层 ZIP 包,并将解压后的文件和文件夹保存到指定的目录中。你只需要将 `zipFilePath` 和 `unzipFolderPath` 替换为你的 ZIP 文件路径和解压目录路径即可。