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

Create ZIP file from servlet

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Andrew,

Your code example worked at first try :-)
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic