本文介绍了用java中的子文件夹解压缩存档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解压缩包含带有一些 png 图像的子文件夹的存档 (test.zip):
I am trying to unzip an archive (test.zip) containing a subfolder with some png images:
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();
}
图像被提取到正确的位置但已损坏(大小小于预期,所以我假设它们没有解压缩).
The images are extracted to the correct location but are corrupt (the size is smaller than expected so I assume they are not decompressed).
如果我用 7Zip 打开 test.zip 文件,它工作正常.关于如何解压缩包含子文件夹的存档有什么想法吗?
If I open the test.zip file with 7Zip it works fine. Any ideas on how to unzip an archive with subfolders?
推荐答案
你在这里做什么?
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
zin.closeEntry();
fout.close();
}
难道你是这个意思?
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
这篇关于用java中的子文件夹解压缩存档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!