Several of our web-apps have a requirement to deliver formatted data files, let's say for example we might have a .txt, a .xml and a .jpeg file of some data that has been extracted from a database. It is easy for us to deliver these by generating them and providing a link, or even to stream them direct to the browser to force a download from a servlet output stream.
One step up from this is to put these files into a Zip file so that we can deliver multiple files in a single download. Great, but there are two problems with this...
1) there is an IO overhead writing these files to the server a) write data files b) write ZIP file using using data files 2) we are left with a load of redundant files on a server somewhere and we need extra work to deal with periodically removing them, or we need to remove them directly after the download somehow...more overhead.
...so, what I really want is to never actually write a file, I just want to stream the data formats directly into a ZipOutputStream, and then stream that to the browser. Problem is the ZipEntry constructor only accepts a file...does anyone have any ideas of how I could avoid the need to create ZipEntry objects using files, but instead stream this data?
The ZipEntry constructor takes a String name (usually a file or directory name). You create a new ZipEntry with a file name, invoke putNextEntry() on your ZipOutputStream with the ZipEntry as an argument and write the file's bytes to the stream. Exactly what you want to do.
thanks Joe, however I'm not sure I explained this one correctly because that's not quite what I want . You say I can add a new ZipEntry with a file name (String)...but the problem is that file wouldn't exist, instead I want to do something like the following pseudocode...
The ZipEntry has to have a name, otherwise what the user be unzipping? A big bucket of bits? How would the user differentiate the image from the text data? Do you want to tell the users to break out the hex editor? You have these discrete pieces of information on the server in a database. I assume you know which is which. Make up a name on the fly. "X.jpg" where "X" is the database index. "Joe.xml". It doesn't matter that the file doesn't exist on the server. The data isn't coming from a file, it is coming from the OutputStream. The ZipEntry provides a way for the zip archive to differentiate which bucket of bits belongs in which file when the data is extracted on the client.
Joined: Aug 14, 2001
DOH!!! Now I geddit
Sorry Joe, just one of those days. Can't see the wood for the trees :roll: Thank you! [ August 26, 2004: Message edited by: Ben Wood ]
Joined: Aug 14, 2001
Here's some code which I think basically covers it, just incase anyone's interested
[ August 26, 2004: Message edited by: Ben Wood ]
subject: Stream data directly into ZipOutputStream?