|
Java™ by example!
|
|
|
How do I create a ZIP file containing multiple compressed files?
A ZipOutputStream is able to create an outputstream containing several data streams converted into the ZIP format. The resulting outputstream is either compressed at a certain level (higher level means better compression) or uncompressed. These are the steps to take to create a ZIP stream:
Following example zips up all the .java files in the current directory into javafiles.zip. Main.java:
You can change the compression method with setMethod. ZipOutputStream's default compression method is DEFLATED which uses java.util.zip.Deflater. The only other method that is currently supported is STORED which doesn't do any compression. When using this method, however, you must initialize the ZipEntry with a CRC-value and a file size of a datastream before you can write it to the ZipOutputStream otherwise it will throw the following exception:
(the method DEFLATED doesn't have this requirement). That sucks, cause you will have to process the data stream an extra time (to find out the CRC-value and file size).
Further Information
Author of answer: Joris Van den Bogaert
Comments
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|