Java Uncompress Byte Array unzip(byte[] in)

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

Description

Returns an gunzipped copy of the input array.

License

Apache License

Exception

Parameter Description
IOException if the input cannot be properly decompressed

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import java.io.IOException;

import java.util.zip.GZIPInputStream;

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

    /**//from  ww  w .jav a 2  s  .  com
     * Returns an gunzipped copy of the input array.  
     * @throws IOException if the input cannot be properly decompressed
     */
    public static final byte[] unzip(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();
    }
}

Related

  1. unZip(byte[] contents)
  2. unzip(byte[] data)
  3. unZip(byte[] data)
  4. unzip(byte[] data)
  5. unzip(byte[] datas)
  6. unZip(byte[] input)
  7. unzip(byte[] output)
  8. unzip(byte[] src)
  9. unzip(byte[] zipData, File directory)