Wednesday, March 14, 2012

How to uncompress a file in the gzip format in java?


This example decompresses a gzip compressed file with java.util.zip.GZIPInputStream.

public static boolean Uncompression(String infname, String outfname){
    
    GZIPInputStream in = null;
    OutputStream out = null;
    
    try {
        in = new GZIPInputStream(new FileInputStream(infname));
        out = new FileOutputStream(outfname);
        byte[] buf = new byte[65536];
        int len;
        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        return true;
    } catch (IOException e) {
        if ( in != null ) {
            try {
                in.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if ( out != null ) {
            try {
                out.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return false;
    }
}

No comments:

Post a Comment