GZip Decompress byte array - Java java.lang

Java examples for java.lang:byte Array Compress

Description

GZip Decompress byte array

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 {
    public static void main(String[] argv) throws Exception {
        byte[] compressedData = new byte[] { 34, 35, 36, 37, 37, 37, 67,
                68, 69 };/*from   w ww . j  av  a  2  s . c om*/
        System.out.println(java.util.Arrays
                .toString(GZipDecompress(compressedData)));
    }

    public static byte[] GZipDecompress(byte[] compressedData) {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                compressedData);

        try {
            GZIPInputStream gzipInputStream = new GZIPInputStream(
                    byteArrayInputStream);

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = gzipInputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, length);
            }
            gzipInputStream.close();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials