Decompress data bytes with gzip algorithm. - Java File Path IO

Java examples for File Path IO:GZIP

Description

Decompress data bytes with gzip algorithm.

Demo Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

public class Main {
    /**//from   w  ww.  j  av a  2s .com
     * Decompress data bytes with gzip algorithm.
     * 
     * @param data
     * @return data decompressed.
     * @throws IOException
     */
    public static byte[] ungzip(byte[] data) throws IOException {
        if (data == null) {
            return data;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(data);

        GZIPInputStream gis = null;
        try {
            gis = new GZIPInputStream(in);
            byte[] buffer = new byte[1024];
            int n;
            while ((n = gis.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
        } finally {
            if (gis != null) {
                gis.close();
            }
        }

        return out.toByteArray();
    }
}

Related Tutorials