Java Ungzip Byte Array ungzip(byte[] in)

Here you can find the source of ungzip(byte[] in)

Description

Returns an gunzipped copy of the input array.

License

Open Source License

Exception

Parameter Description
IOException if the input cannot be properly decompressed

Declaration

public static final byte[] ungzip(byte[] in) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.util.zip.GZIPInputStream;

import java.util.zip.ZipFile;

public class Main {
    private static final int EXPECTED_COMPRESSION_RATIO = 5;
    private static final int BUF_SIZE = 4096;

    /**//from   ww w  .  j av a 2 s .c  o m
     * Returns an gunzipped copy of the input array.
     *
     * @throws IOException if the input cannot be properly decompressed
     */
    public static final byte[] ungzip(byte[] in) throws IOException {
        // decompress using GZIPInputStream
        ByteArrayOutputStream outStream = new ByteArrayOutputStream(
                EXPECTED_COMPRESSION_RATIO * in.length);

        GZIPInputStream inStream = new GZIPInputStream(
                new ByteArrayInputStream(in));

        byte[] buf = new byte[BUF_SIZE];
        while (true) {
            int size = inStream.read(buf);
            if (size <= 0)
                break;
            outStream.write(buf, 0, size);
        }
        outStream.close();

        return outStream.toByteArray();
    }

    /**
     * Closes zip file safely.
     */
    public static void close(ZipFile zipFile) {
        if (zipFile != null)
            try {
                zipFile.close();
            } catch (IOException ioex) {
                // ignore
            }
    }
}

Related

  1. ungzip(byte[] buff)
  2. ungzip(byte[] bytes)
  3. unGZip(byte[] bytes)
  4. unGzip(byte[] data)
  5. unGZip(byte[] data)
  6. unGzip(byte[] str, String charset)
  7. ungzipBestEffort(byte[] in)
  8. ungzipBuffer(byte[] bufInput)
  9. ungzipPayload(byte[] compressed)