我正在使用以下代码从zip存档中提取文件。
public static List<String> unzipFiles(File zipFile, File targetDirectory) {
List<String> files = new ArrayList<String>();
BufferedOutputStream dest = null;
FileInputStream fileInputStream = null;
ZipInputStream zipInputStream = null;
try {
fileInputStream = new FileInputStream(zipFile);
zipInputStream = new ZipInputStream(new BufferedInputStream(
fileInputStream));
ZipEntry zipEntry;
int count = 0;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
log.debug("Extracting File Name :: " + zipEntry);
count++;
int length;
byte data[] = new byte[bufferGlb];
String fileName = zipEntry.getName();
File opFile = new File(targetDirectory, fileName);
FileOutputStream fileOutputStream = new FileOutputStream(opFile);
dest = new BufferedOutputStream(fileOutputStream, bufferGlb);
while ((length = zipInputStream.read(data, 0, bufferGlb)) != -1) {
dest.write(data, 0, length);
}
dest.flush();
files.add(fileName);
fileOutputStream.close();
}
log.debug("Total " + count + " Files Unziped Successfully ");
} catch (Exception e) {
log.error("Error occured in unzipping the file " + zipFile, e);
}
}
此代码适用于常规zip存档,但不适用于zip64存档。据我所知,Java7 / Java8(ZipInputStream类)应支持zip64,但出现以下异常。
java.util.zip.ZipException: invalid entry size (expected 0 but got 21504 bytes)
at java.util.zip.ZipInputStream.readEnd(ZipInputStream.java:384)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:196)
但是,可以使用commons-compress ZipArchiveInputStream从zip64中提取文件。
任何想法为何使用Java API(ZipInputStream)都无法实现相同的目的。