• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generate zip file containing a csv file

 
Nitin Kalra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How can i create a zip file containing a csv file without the actual physical existance of the csv file. I have already developed the piece of code which creates a csv file and then this csv file is zipped and the original csv file is deleted.
Well my task is to generate a csv file out of the dynamic data retrieved from the database. But i do not want any physical file to be created on the server. Niether the csv nor the zip file. The final downloadable file should be a zip file containing the csv file. As the user on my site clicks this button of download option the above task should be performed with no existance of file on the server.
Thanks in advance
- Nitin
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a ZipOutputStream to create the zip file. You can write to this stream much like you previously wrote to a FileOutputStream to create the unzipped file. You can wrap the ZipOutputStream in other OutputStreams or Writers as you find necessary. You also need to add a few calls to putNextEntry() and closeEntry(), along with creating the appropriate ZipEntry info.
If you're creating a zip file containing multiple entries, things get more complicated if you're using other high-level streams/Writers to create the individual entries. The problem is that if you use a particular high-level Writer just for one entry, and then close() it, the close() gets invoked on each nested stream, ultimately including the ZipOutputStream. This is a problem if you were still planning on writing additional entries. Even if you do not specifically close() the high-level streams, if they are garbage collected the close() may be invoked as part of finalization for that instance. (At least, I think this is true for some streams at least, though not all). You may find it useful to wrap the following stream around the ZipOutputStream (but inside any other high-level streams you use) to protect it from premature closure:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic