esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

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:

 
This code sample is only viewable to esus.com members
Login or become a member!


Following example zips up all the .java files in the current directory into javafiles.zip.

Main.java:

 
This code sample is only viewable to esus.com members
Login or become a member!


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:

 
This code sample is only viewable to esus.com members
Login or become a member!


(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!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.