Java Ungzip Byte Array ungzipBestEffort(byte[] in)

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

Description

Returns an gunzipped copy of the input array.

License

Open Source License

Declaration

public static final byte[] ungzipBestEffort(byte[] in) 

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 a v a2s.  co m
     * Returns an gunzipped copy of the input array. If the gzipped input has
     * been truncated or corrupted, a best-effort attempt is made to unzip as
     * much as possible. If no data can be extracted <code>null</code> is
     * returned.
     */
    public static final byte[] ungzipBestEffort(byte[] in) {
        return ungzipBestEffort(in, Integer.MAX_VALUE);
    }

    /**
     * Returns an gunzipped copy of the input array, truncated to
     * <code>sizeLimit</code> bytes, if necessary. If the gzipped input has
     * been truncated or corrupted, a best-effort attempt is made to unzip as
     * much as possible. If no data can be extracted <code>null</code> is
     * returned.
     */
    public static final byte[] ungzipBestEffort(byte[] in, int sizeLimit) {
        try {
            // 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];
            int written = 0;
            while (true)
                try {
                    int size = inStream.read(buf);
                    if (size <= 0)
                        break;
                    if (written + size > sizeLimit) {
                        outStream.write(buf, 0, sizeLimit - written);
                        break;
                    }
                    outStream.write(buf, 0, size);
                    written += size;
                } catch (Exception e) {
                    break;
                }
            try {
                outStream.close();
            } catch (IOException e) {
            }

            return outStream.toByteArray();

        } catch (IOException e) {
            return null;
        }
    }

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

Related

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