• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Issue with Download using ZipOutputStream

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
real code to zip the file

 
Sheriff
Posts: 22798
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please UseCodeTags next time? Thanks.
 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you probably want to close your resources correctly (they don't get closed when an exception occurs).
 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only now you might close fin and outZip twice.
 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. Wouter could you please suggest than what should I need to do here please. Keeping this code in mind.
 
Rob Spoor
Sheriff
Posts: 22798
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob,
In below code snippet you are saying I need to close zipOutputStreamObject beore fileInputStreamObject?


 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also I my code I havenot used any FileOutputStream ,which code you are referring .? Indeed it is FileInputStream
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Getting closer but if zipOutputStreamObject.closeEntry() throws an exception when closing the other resources will not be closed.
 
Rob Spoor
Sheriff
Posts: 22798
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, what did I tell you about closing the FileInputStream before closing the ZipOutputStream?
 
Greg Charles
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22798
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah yes, my apologies. I read fileOutputStreamObject instead of fileInputStreamObject.
 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your valuable suggestoions.
I manged to get rid of this issue somehow.
 
Greg Charles
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Grow a forest with seedballs and this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic