Reputation: 2001
I am trying to zip a group of binary data (result set returned from database) into a single file. Which can be downloaded via web application. Following code is used to zip the result set and write the zip file to HttpServletResponse
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();
The problem is that while unzipping the downloaded zip file using WinRar I get following error :
File Path: Either multipart or corrupt ZIP archive
Can someone point out where am I making mistake?. Any help would be appreciated.
[EDIT] I tried response.setContentType("application/zip");
but same result.
Upvotes: 4
Views: 2723
Reputation: 6581
Your code looks correct for producing the file. So the problem is probably in your downloading.
Upvotes: 0
Reputation: 23268
The following code works for me:
FileOutputStream os = new FileOutputStream("out.zip");
ZipOutputStream zos = new ZipOutputStream(os);
try
{
for (int i = 1; i <= 5; i++)
{
ZipEntry curEntry = new ZipEntry("file" + i + ".txt");
zos.putNextEntry(curEntry);
zos.write(("Good morning " + i).getBytes("UTF-8"));
}
}
finally
{
zos.close();
}
Zip files generated with this code opens up with 7-zip without problem.
Upvotes: 2