File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes generate zip file containing a csv file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "generate zip file containing a csv file" Watch "generate zip file containing a csv file" New topic

generate zip file containing a csv file

Nitin Kalra

Joined: Jan 24, 2003
Posts: 11
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

Joined: Jan 30, 2000
Posts: 18671
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:

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: generate zip file containing a csv file
It's not a secret anymore!