Decompresses the input stream using GZIP. - Java File Path IO

Java examples for File Path IO:GZIP

Description

Decompresses the input stream using GZIP.

Demo Code


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

import sun.misc.BASE64Decoder;

public class Main {
    /**// w  w w .  j  av  a 2s .c om
     * Decompresses the input stream using GZIP.
     *
     * @param inputStream Stream those content should be decompressed.
     * @return Decompressed bytes
     * @throws IOException
     */
    public static byte[] gunzip(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int count;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPInputStream gzipIn = new GZIPInputStream(inputStream);

        while ((count = gzipIn.read(buffer)) != -1) {
            baos.write(buffer, 0, count);
        }

        gzipIn.close();

        return baos.toByteArray();
    }

    /**
     * Decompresses the input byte array using GZIP.
     *
     * @param binaryInput Array of bytes that should be decompressed.
     * @return Decompressed bytes
     * @throws IOException
     */
    public static byte[] gunzip(byte[] binaryInput) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(binaryInput);
        return gunzip(bais);
    }

    /**
     * Decompresses the input byte array using GZIP.
     *
     * @param base64Input BASE64 encoded bytes to be decompressed.
     * @return Decompressed bytes
     * @exception IOException
     */
    public static byte[] gunzip(String base64Input) throws IOException {
        BASE64Decoder base64Decoder = new BASE64Decoder();
        byte[] binaryInput = base64Decoder.decodeBuffer(base64Input);
        return gunzip(binaryInput);
    }
}

Related Tutorials