Java
  Home arrow Java arrow Page 2 - Zip Meets Java
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Zip Meets Java
By: Kulvir Singh Bhogal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 92
    2003-11-17

    Table of Contents:
  • Zip Meets Java
  • Zipping Things Up with Java
  • Unzipping Things with Java
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Zip Meets Java - Zipping Things Up with Java


    (Page 2 of 4 )

    First, let’s ZIP up a set of files. In the code in Listing 1, we first define a set of files to be added to our ZIP file. Notice that we state the location of the files on our hard drive. One of our files is located in a subdirectory. You will see later that the directory structure of our files will be kept within the ZIP file we construct. The ZIP file we are creating is called example.zip and will be stored in the root of the C drive. We also create a buffer to aid us as we write our ZIP file. Note that the buffer size must be big enough to house the files you want added to the ZIP file.

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.Deflater;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;

    public class ZipCreateExample {

       public static void main(String[] args) {
         System.out.println("Example of ZIP file creation.");

         // Specify files to be zipped
         String[] filesToZip = new String[3];
         filesToZip[0] = "firstfile.txt";
         filesToZip[1] = "secondfile.txt";
         filesToZip[2] = "temp\thirdfile.txt";

         byte[] buffer = new byte[18024];

         // Specify zip file name
         String zipFileName = "c:\example.zip";

         try {

           ZipOutputStream out =
             new ZipOutputStream(new FileOutputStream(zipFileName));

           // Set the compression ratio
           out.setLevel(Deflater.DEFAULT_COMPRESSION);

           // iterate through the array of files, adding each to the zip file
           for (int i = 0; i < filesToZip.length; i++) {
             System.out.println(i);
             // Associate a file input stream for the current file
             FileInputStream in = new FileInputStream(filesToZip[i]);

             // Add ZIP entry to output stream.
             out.putNextEntry(new ZipEntry(filesToZip[i]));

             // Transfer bytes from the current file to the ZIP file
             //out.write(buffer, 0, in.read(buffer));

             int len;
            while ((len = in.read(buffer)) > 0)
            {
            out.write(buffer, 0, len);
           }



             // Close the current entry
             out.closeEntry();

             // Close the current file input stream
             in.close();

           }
           // Close the ZipOutPutStream
           out.close();
         }
         catch (IllegalArgumentException iae) {
           iae.printStackTrace();
         }
         catch (FileNotFoundException fnfe) {
           fnfe.printStackTrace();
         }
         catch (IOException ioe)
         {
         ioe.printStackTrace();
         }


       }
    }


    The core of our ZIP file construction is performed in our try-catch clause. First we create a new ZipOutputStream object. On this object, we use the setLevel method to specify the compression ratio we’d like to use when we create our ZIP file. The Deflater object contains static member variables for specifying such compression options as best compression (i.e., BEST_COMPRESSION), best speed (i.e., BEST_SPEED), no compression (i.e., NO_COMPRESSION). In our code, we specify the option of BEST_COMPRESSION.

    To add files to our zip file, we iterate through our filesToZip file name array, each time associating a FileInputStream to the file we are currently working with. On all iterations, we also use the putNextEntry method of our ZipOutputStream object, to add the current file to our target Zip file, followed by a writing of the bytes of the current file to the ZipOutputStream. After each file is written, we must use the closeEntry method. Finally, after iterating is complete, we use the close method to close off our ZipOutPutStream, thereby sealing our ZIP file.

    More Java Articles
    More By Kulvir Singh Bhogal


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway