Home arrow Java & J2EE arrow Page 2 - Zip Meets Java

Zipping Things Up with Java - Java

javaThe ZIP file format has become extremely popular for the distribution and storage of files... The java.util.zip package allows for the programmatic reading and writing of the ZIP and GZIP formats. As youíll see in this article, the API for reading and writing ZIP files is pretty straightforward to use... In this article, Iíll take you through a Java program that creates of a ZIP file and also walk you through another Java program that unzips the file we created.

TABLE OF CONTENTS:
  1. Zip Meets Java
  2. Zipping Things Up with Java
  3. Unzipping Things with Java
  4. Conclusion
By: Kulvir Singh Bhogal
Rating: starstarstarstarstar / 102
November 17, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 & J2EE Articles          >>> More By Kulvir Singh Bhogal
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: