• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Zip files on the fly

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tried to zip multiple files on the fly in java servlet with zip package (ZipOutputStream), so users can download the zip file by calling the servlet. It works fine for small files. But occasionally the browser stops the download before completion when users invoke the servlet action, and the zip file downloaded is corrupted.

The servlet response header �content length� is not set, since the zip file is generated on the fly and we don�t know the size. �Transfer-Encoding� is set to �chunked� to resolve the dynamic content length:

Here is related code:

response.setContentType("application/zip");
response.setHeader("Transfer-Encoding", "chunked");
ServletOutputStream os = response.getOutputStream();
ZipOutputStream out = new ZipOutputStream(os);

// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filePath));
copyContentsToOutput(out);
out.closeEntry();
out.flush();
out.close();

copyContentsToOutput(OutputStream out) {
InputStream input = FileInputStream(inputFile);
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
try {
while ((bytesRead = input.read(buffer, 0, bufferSize)) != -1) {
output.write(buffer, 0, bytesRead);
}
}
}

Please help. Thanks in Advance.

YSong
 
Yudong Song
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone can help? How should I set the response header correctly, so the browser won't drop the connection before completing the download. It's a
production issue, please help!
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This doesn't seem to be generating much interest here.

I'll move it to our Streams and I/O forum for you.
Maybe it will do better there.
 
And inside of my fortune cookie was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic