aspose file tools*
The moose likes Servlets and the fly likes Create ZIP file from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Create ZIP file from servlet" Watch "Create ZIP file from servlet" New topic
Author

Create ZIP file from servlet

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Hello,

I want to be able to add some files (java.sql.Blob) to a ZIP file in a servlet. At this moment I am able to create the ZIP file, but when opening the ZIP file it´s empty?

Below you see some of my code. I get the files from Blob values:

I guess the problem is my for loop?! I appreciate any help...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Are you trying to send a dynamically-created zip file to the browser?
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
David Newton wrote:Are you trying to send a dynamically-created zip file to the browser?


Yes, I am trying to send a dynamically-created zip file to the browser.

When I call the servlet a window is shown, but when opening the zip file there is no files added to the zip file!
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

Without looking into this in too much depth, it looks like you are writing the contents of the bytearray to the response in line 31. And the bytearray is being filled from in at line 29. in is defined in line 21 as the binary stream from the Blob itself. So it looks to me like your code should be sending the raw blob data to the client - not a zipfile.

Of course there is some other problem with the code if you are getting a zero byte zipfile - I have not looked at what that might be. If the file the client receives does contain data, just is not a zipfile, try changing the extension so you can see if it contains the data you think should be in the zipfile.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

Just tried a simple test case for myself, and tested it on my computer:

Although I am just using a file as my input stream in this case, the theory should be the same. I am leaving the writing of the servlet response up to the ZipOutputStream. All I do is add data to the ZipOutputStream.

Can you see how to modify your code now?
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Thanks Andrew,

Your code example worked at first try :-)
 
wood burning stoves
 
subject: Create ZIP file from servlet