aspose file tools*
The moose likes I/O and Streams and the fly likes Zip files on the fly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Zip files on the fly" Watch "Zip files on the fly" New topic
Author

Zip files on the fly

Yudong Song
Greenhorn

Joined: Feb 06, 2008
Posts: 2
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

Joined: Feb 06, 2008
Posts: 2
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!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Zip files on the fly
 
Similar Threads
Problem in creating zip file on weblogic server
zip files without path info...
Windows XP Compressed Folders doesn't recognize my Java-created zip file
Downloading file
how to zip multiple files and directory into one output