以下代碼用於壓縮普通文本文件。當我使用WinRaR解壓其正確顯示內容時,但是當我用Windows資源管理器打開它的空白時,沒有列出任何文件。我正在使用Windows 7 Enterprise(64位)操作系統。任何想法爲什麼它不在Windows資源管理器中列出?提前致謝。使用Java創建的ZIP文件在使用Windows資源管理器打開時顯示爲空
File file = new File("F:\\sample.txt");
byte[] buf = new byte[1024];
String outFilename = "F:\\zipped_sample.zip";
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
FileInputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.toString()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
out.flush();
}
out.closeEntry();
out.close();
in.close();
} catch (Exception e) {
// log exception here
}
此代碼應該按原樣工作,Windows資源管理器應顯示該zip包含「F:」條目和其中的「sample.txt」條目。在'catch'中正確記錄異常,看看這段代碼是否能在你的計算機上正常完成。 –
你應該在finally塊中關閉()。另外,如果這是Java 7,則使用新的zip文件系統,這將使您的工作更輕鬆。 – fge
@OlegEstekhin代碼正常完成,我沒有看到任何錯誤/異常。但是,當我嘗試右鍵單擊 - >全部提取時,顯示錯誤「Windows無法完成提取。壓縮(壓縮)文件夾無效」。我不知道爲什麼它只與Windows資源管理器失敗。 – Sridhar