This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Issue with Download using ZipOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Issue with Download using ZipOutputStream" Watch "Issue with Download using ZipOutputStream" New topic
Author

Issue with Download using ZipOutputStream

Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
Hi ,

I have Download functionality in my application. The function download the file in .xls format and zipped it. So that user can save that zip file on this local machine.
Everything workign fine but the file which is zipped inside have no extension. Example I am expeting the user to download xyz.zip file which interns contains xyz.xls,but unfortunately file having no extension (downloaded as xyz) inside xyz.zip.I am using below statement to zip the file.

response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename="+xyz.zip+";");

ZipOutputStream outZip = new ZipOutputStream(response.getOutputStream());
FileInputStream in = new FileInputStream(xyz.xls);
outZip.putNextEntry(new ZipEntry(xyz.xls));


Please suggest,there is no error/exception found in logs.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8843
    
    7

Can you show us your real code? There's a couple of problems in the code above that would not compile, much less cause a runtime problem.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I'm pretty sure Joe was asking for some real code which illustrated the problem you asked about. What you just posted may be real code, but it doesn't have anything to do with your problem.
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
Paul,
This what responsible for deownloading and zipped the .xls sheet. apart from this there is no code.Just in one jsp I am calling this method which is implemented in struts Action class.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2841
    
  11

Hi Anand,

You might want to check the code you posted. There's no mention of ZipOutputStream, or zipping at all. It also doesn't contain the sample lines you had in your original post, which look like they come from a servlet doGet() or doPost() method.
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
real code to zip the file

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Can you please UseCodeTags next time? Thanks.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
Let me brief on this

After download I can see zip inside the other zip file(but inner zip having no extension )

So what exactly happening is

XYZ.zip file downloaded which when extracted contains a file XYZ(no extension though) which than extracted usinh WinRar contains the actualxls file.

So why it is getting zipped twice is issue .
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And you probably want to close your resources correctly (they don't get closed when an exception occurs).


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
Hi Wouter,

Resources are closed properly and exception handling as well done where we expecting some excetion to be thrown.

The same code working when applicaion got hit from some machines but for few others it is not working.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

No they're not. What happens when an exception is thrown at line 50? Then the methods calls which close the resources are skipped and a stacktrace is printed.
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
Thanks Wouter for higlightinh that point,but i made modification to that code already so the updated code is somewhat:




So bold part here doing that closing of resources now.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Only now you might close fin and outZip twice.
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
OK. Wouter could you please suggest than what should I need to do here please. Keeping this code in mind.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Always close the outer-most stream. That should also close the stream it was wrapping. In this case it's very, very important to close the ZipOutputStream before you close the FileOutputStream, as the ZipOutputStream will write some extra bytes to close the ZIP file itself. If you close the FileOutputStream first those bytes will be missing and the ZIP file will be corrupt.
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
Thanks Rob,
In below code snippet you are saying I need to close zipOutputStreamObject beore fileInputStreamObject?


Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
Also I my code I havenot used any FileOutputStream ,which code you are referring .? Indeed it is FileInputStream
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Getting closer but if zipOutputStreamObject.closeEntry() throws an exception when closing the other resources will not be closed.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Also, what did I tell you about closing the FileInputStream before closing the ZipOutputStream?
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2841
    
  11

Rob Spoor wrote:Also, what did I tell you about closing the FileInputStream before closing the ZipOutputStream?


Actually, you said not to close a FileOutputStream before you close a ZipOutputStream that wraps it. Anand asked you about that specifically a couple of posts ago because he's wrapping the servlet's output stream, not a FileOutputStream, but is using a FileInputStream to read the file. I don't think it matters when you close the Input Stream as long as you've finished reading from it. However, I also don't see anything that would cause the "double zip" issue Anand is seeing.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Ah yes, my apologies. I read fileOutputStreamObject instead of fileInputStreamObject.
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189

Hi Greg ,


} finally {
try {
zipOutputStreamObject.closeEntry();
fileInputStreamObject.close();
zipOutputStreamObject.close();[/b]
} catch (IOException ioe) {
LOGGER.error("An Exception occured while closing IO resources--->"+ ioe.getLocalizedMessage());
ioe.printStackTrace();
}


I can see that if I get any exception in zipOutputStreamObject.closeEntry(); than I have another catch block to handle it. So I am bit confused
what ele I need to do here.
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
Thanks all for your valuable suggestoions.
I manged to get rid of this issue somehow.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2841
    
  11

Hi Anand,

I think Wouter meant that if an exception happens at zipOutputStreamObject.closeEntry(), your program will jump immediately to the the catch block, bypassing the fileInputStreamObject.close() and zipOutputStreamObject.close(). You should really put each close into its own try-catch block or use one of those closeQuietly() utility methods. It's a bigger deal when you're closing something like a database connection or other scarce resources. I've seen server slow down and stop because there just aren't any db connections left ... and all because the close wasn't handled properly when an exception occurred.

I'm glad you solved your problem. Can you post the code here? Then we can mark the thread as resolved, and anyway I'm curious where the error was.
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
Hi Greg,

The issue was with contentType
Instead of response.setContentType("application/zip"); I read some where in Microsoft site and tried below and isssue disappeared:
response.setContentType("application/octet-stream");
So it is some what specific for IE and I am still clueless about this issue.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Issue with Download using ZipOutputStream
 
Similar Threads
Create ZIP file from servlet
zip files without path info...
appropriate content type to download a zip file in a Struts App
zipping file
Zipping Directory Structure - Need to Chop Off First Few Directories