我正在尝试解压缩包含带有一些 png 图像的子文件夹的存档(test.zip):
test.zip
| -> images
| -> a.png
| -> b.png
这是我所做的:
public static void unzip(String archive, File baseFolder, String[] ignoreExtensions) {
FileInputStream fin;
try {
fin = new FileInputStream(archive);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if (ignoreExtensions == null || !ignoreEntry(ze, ignoreExtensions)) {
File destinationFile = new File(baseFolder, ze.getName());
unpackEntry(destinationFile, zin);
}
}
zin.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void unpackEntry(File destinationFile, ZipInputStream zin) {
createParentFolder(destinationFile);
FileOutputStream fout = null;
try {
fout = new FileOutputStream(destinationFile);
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
zin.closeEntry();
fout.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void createParentFolder(File destinationFile) {
File parent = new File(destinationFile.getParent());
parent.mkdirs();
}
图像被提取到正确的位置,但已损坏(大小小于预期,因此我假设它们未解压缩)。
如果我用 7Zip 打开 test.zip 文件,它可以正常工作。关于如何解压缩带有子文件夹的存档的任何想法?