0
我想解壓縮含有某些PNG圖像的子文件夾歸檔(test.zip):解壓與java中的子文件夾存檔?
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文件,它工作正常。有關如何解壓縮子文件夾的任何想法?
謝謝,就是這樣! – u123 2011-06-15 21:49:34
此外,爲了提高效率,您應該更好地使用緩衝陣列,而不是單獨讀寫每個字節。 (你正在寫的每個字節至少有兩個本地調用:一個在Inflater(zlib)中,一個在FileOutputStream中,本地調用是昂貴的。) – 2011-06-15 21:55:54