簡體   English   中英

標准zip實用程序無法解壓縮java.util.zip壓縮的文件

[英]File zipped by java.util.zip can't be decompressed by standard zip utility

我設法使用以下簡單的代碼在Java中創建了zip文件:

BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(zipFile);

ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
out.setMethod(ZipOutputStream.DEFLATED);
out.setLevel(5);

byte data[] = new byte[BUFFER];
FileInputStream fi = new FileInputStream(file);
origin = new BufferedInputStream(fi, BUFFER);

ZipEntry entry = new ZipEntry(file.getName());
out.putNextEntry(entry);

int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
    out.write(data, 0, count);
}

out.closeEntry();
origin.close();
out.close();

zip文件創建成功。 但是,當我嘗試使用WinZip或其他工具將其解壓縮時,出現錯誤:

Central and local directory mismatch for file "my_file" (general 
purpose flags - local:808 hex  central: 8 hex). 
Severe Error:  Local and central GPFlags values don't match.

真正奇怪的是,當我解壓縮文件時,WinRAR和內部Win7 zip不會顯示任何錯誤。

我究竟做錯了什么? 有人遇到這個問題嗎? 非常感謝!

必須是缺少out.close。

我認為您忘了關閉拉鏈。

out.closeEntry();
origin.close();
out.close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM