要使用ZipFile解壓文件流,可以按照以下步驟:
ZipFile zipFile = new ZipFile(new File("path/to/zipfile.zip"));
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while(entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if(entry.isDirectory()) {
// 如果是目錄,創(chuàng)建目錄
File dir = new File("path/to/output/" + entry.getName());
dir.mkdirs();
} else {
// 如果是文件,解壓文件
InputStream inputStream = zipFile.getInputStream(entry);
OutputStream outputStream = new FileOutputStream("path/to/output/" + entry.getName());
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
inputStream.close();
outputStream.close();
}
}
zipFile.close();
通過以上步驟,可以使用ZipFile解壓文件流到指定目錄。