二進制數據,我試圖壓縮一組二進制數據(從數據庫返回的結果集)到一個單一的文件。可以通過網絡應用程序下載。下面的代碼是用來壓縮結果集,寫的zip文件HttpServletResponse荏苒在Java
String outFilename = "outfile.zip";
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename= " + outFilename);
OutputStream os = response.getOutputStream();
ZipOutputStream out = new ZipOutputStream(os);
for (int i = 0; i < cardFileList.size(); i++) {
CardFile cardFile = cardFileList.get(i);
out.putNextEntry(new ZipEntry(cardFile.getBinaryFileName()));
out.write(cardFile.getBinaryFile(), 0, cardFile.getBinaryFile().length);
out.closeEntry();
}
// Complete the ZIP file
out.flush();
out.close();
os.close();
的問題是,雖然用winrar解壓下載的zip文件我得到以下錯誤:
文件路徑:要麼多部分或損壞的ZIP檔案
有人可以指出我在哪裏犯錯?任何幫助,將不勝感激。
[編輯]我想response.setContentType("application/zip");
但同樣的結果。