blog rss feed

Reading ZIP, GZIP and JAR files with Java inbuilt classes

Keywords:

Last editor: Dave Cherry, last modified: Oct 25, 2009

Reading a zip file using ZipInputStream

Java provides support for reading zip files in the form of ZipInputStream. This class provides an API where you can iterate over all the items in a given zip file, reading the data from the archive for each file. In order to do this first you must create the ZipInputStream instance giving the file that you wish to expand. Then you iterate using the getNextEntry method on the stream, which returns the header data for each entry in turn. Importantly this does not contain the data, which is actually read from the stream separately.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipReader
{
    // Expands the zip file passed as argument 1, into the
    // directory provided in argument 2
    public static void main(String args[]) throws Exception
    {
        if(args.length != 2)
        {
            System.err.println("zipreader zipfile outputdir");
            return;
        }

        // create a buffer to improve copy performance later.
        byte[] buffer = new byte[2048];

        // open the zip file stream
        InputStream theFile = new FileInputStream(args[0]);
        ZipInputStream stream = new ZipInputStream(theFile);
        String outdir = args[1];

        try
        {

            // now iterate through each item in the stream. The get next
            // entry call will return a ZipEntry for each file in the
            // stream
            ZipEntry entry;
            while((entry = stream.getNextEntry())!=null)
            {
                String s = String.format("Entry: %s len %d added %TD",
                                entry.getName(), entry.getSize(),
                                new Date(entry.getTime()));
                System.out.println(s);

                // Once we get the entry from the stream, the stream is
                // positioned read to read the raw data, and we keep
                // reading until read returns 0 or less.
                String outpath = outdir + "/" + entry.getName();
                FileOutputStream output = null;
                try
                {
                    output = new FileOutputStream(outpath);
                    int len = 0;
                    while ((len = stream.read(buffer)) > 0)
                    {
                        output.write(buffer, 0, len);
                    }
                }
                finally
                {
                    // we must always close the output file
                    if(output!=null) output.close();
                }
            }
        }
        finally
        {
            // we must always close the zip file.
            stream.close();
        }
    }
}

So what just happened?

We opened a zip file as a stream, and then we iterated over the files contained in the zip file. For each file entry found we output some of its characteristics and then saved the file to disk. Note that we read the file from the same stream object that we retreived the ZipEntry.

1 2 3 >>

Please leave a comment



Search

Blog calendar

blog: previous month September 2010 blog: next month
su mo tu we th fr sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30