• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create ZIP file from servlet

 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to send a dynamically-created zip file to the browser?
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11866
194
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11866
194
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andrew,

Your code example worked at first try :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic