posted 22 years ago
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