2 thoughts on “Compress and Decompress with Java (using zip)”

  1. I’ve just been down the ‘decompress with Java’ road just recently also. Thought perhaps your readers might find the code I hacked useful in some situations. One is for simple ZIP decompression, the other for GZ

    import java.io.*;
    import java.util.*;
    import java.util.zip.*;

    /* UnZIP a file archive. */
    /* Parameter 1: the directory containing the ZIP file. This will also */
    /* be the output directory. */
    /* Parameter 2: the ZIP file name. */
    /* */

    public class UnZIPFile {

    public static void main(String[] args) {

    String dir = args[0];
    String infile = dir + “/” + args[1];
    String outfile;

    ZipInputStream zin;
    FileOutputStream fout;
    ZipEntry ze;
    int length;
    byte [] outBuf = new byte[512];

    try {
    zin = new ZipInputStream(
    new BufferedInputStream(new FileInputStream(infile)));
    System.out.println(“Processing ZIP archive: ” + infile);
    while((ze = zin.getNextEntry()) != null) {
    System.out.println(” Processing entry: ” + ze);
    outfile = dir + “/” + ze;
    fout = new FileOutputStream(outfile);
    while((length = zin.read(outBuf)) != -1) {
    fout.write(outBuf,0,length);
    }
    fout.close();
    System.out.println(” Entry extracted OK to: ” + outfile);
    }
    zin.close();
    System.exit(0); // (remove for servlets!)
    }

    catch (Exception e) {
    System.err.println(“UnZIPFile failed.”);
    System.err.println(e);
    System.exit(-1);
    }
    }
    }

    import java.io.*;
    import java.util.*;
    import java.util.zip.*;
    import java.util.StringTokenizer;

    /* UnGZIP a file archive. */
    /* Parameter 1: the directory containing the GZIP file. This will also */
    /* be the output directory. */
    /* Parameter 2: the GZIP file name. */
    /* Parameter 3: (optional) output file name */
    /* */

    public class UnGZIPFile {

    public static void main(String[] args) {

    String dir = args[0];
    String fileName = args[1];
    String infile = dir + “/” + fileName;
    String outfile;

    GZIPInputStream gzin;
    FileOutputStream fout;
    int length;
    byte [] outBuf = new byte[512];

    if (args.length == 3) { // output file name specified as a parameter
    outfile = dir + “/” + args[2];
    } else { // output file is input file name w/o extension
    StringTokenizer st = new StringTokenizer(fileName, “.”);
    int tokenCount = st.countTokens();
    if (tokenCount == 1) { // no extension, use ‘txt’…
    outfile = dir + “/” + fileName + “.txt”;
    } else {
    outfile = dir + “/”;
    for (int i = 1; i

  2. Your code doesn’t take in account ZIP files that have directory entries after the file entries.

    What happens, for example, if I get a file entry “images/color.jpg” and the directory “images” doesn’t exist yet ?

    I think a better option is to get the entry Enumeration from the ZipFile (with entries method) and then take different actions depending whether the entry is a directory or not (verify that with the isDirectory method).

    If the entry is a directory, then we simply create it. If the entry is a file, first we get the directory part from its name and create the directory. Then we get the InputStream from the ZipEntry and write to the file.

Leave a Reply

Your email address will not be published. Required fields are marked *