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

Unzipping Things 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.

  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


So we can create zip files with Java. Can we extract them? Listing 2 shows an example Java class that can do just that. Letís cover whatís going on in the class, as it might not be that obvious to the java.util.zip API newcomer.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipUncompressExample

   // specify buffer size for extraction
   static final int BUFFER = 2048;

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

       // Specify file to decompress
       String inFileName = "c:\example.zip";
       // Specify destination where file will be unzipped
       String destinationDirectory = "c:\temp\";

       File sourceZipFile = new File(inFileName);
       File unzipDestinationDirectory = new File(destinationDirectory);

       // Open Zip file for reading
       ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);

       // Create an enumeration of the entries in the zip file
       Enumeration zipFileEntries = zipFile.entries();

       // Process each entry
       while (zipFileEntries.hasMoreElements())
         // grab a zip file entry
         ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

         String currentEntry = entry.getName();
         System.out.println("Extracting: " + entry);

         File destFile =
           new File(unzipDestinationDirectory, currentEntry);

         // grab file's parent directory structure
         File destinationParent = destFile.getParentFile();

         // create the parent directory structure if needed

         // extract file if not a directory
         if (!entry.isDirectory())
           BufferedInputStream is =
             new BufferedInputStream(zipFile.getInputStream(entry));
           int currentByte;
           // establish buffer for writing file
           byte data[] = new byte[BUFFER];

           // write the current file to disk
           FileOutputStream fos = new FileOutputStream(destFile);
           BufferedOutputStream dest =
           new BufferedOutputStream(fos, BUFFER);

           // read and write until last byte is encountered
           while ((currentByte = is.read(data, 0, BUFFER)) != -1)
             dest.write(data, 0, currentByte);
     catch (IOException ioe)

The ZipUncompressExample class unzips a file called example.zip and extracts it to a destination directory called d:temp. We first open the source zip file. From our ZipFile object, we can use the entires() method to obtain an Enumeration which contains ZipEntry objects. These individual ZipEntry objects represent the entries which make up the Zip file.

I then use a while loop to cycle through the entries of our Zip file. As we process each ZipEntry object, we have to check and see if the zip entry in question has the needed housing directory structure in place. If the needed directories are not created, they are established. We use the getParentFile method of the File class to obtain the parent directory structure. If the current zip entry is not a directory (i.e., a file), we write the file to disk in the corresponding housing directory. This process is continued until each entry in the Zip file is processed.

>>> More Java & J2EE Articles          >>> More By Kulvir Singh Bhogal

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: