Java Ungzip Byte Array ungzipBuffer(byte[] bufInput)

Here you can find the source of ungzipBuffer(byte[] bufInput)

Description

DeCompress the byte array using the gzip algorithm

License

Open Source License

Declaration

public static byte[] ungzipBuffer(byte[] bufInput) 

Method Source Code

//package com.java2s;
/** ***************************************************************
Util.java/*from  w w  w .j a  v  a2  s .  co m*/
Copyright (C) 2001  Brendon Upson 
http://www.wnc.net.au info@wnc.net.au
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
    
 *************************************************************** */

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

import java.util.zip.GZIPInputStream;

public class Main {
    /**
     * DeCompress the byte array using the gzip algorithm
     *
     */
    public static byte[] ungzipBuffer(byte[] bufInput) {
        byte bufReturn[] = null;
        if (bufInput == null || bufInput.length == 0)
            return bufInput;

        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(bufInput.length * 2); //double the input because we are decompressing
            ByteArrayInputStream bais = new ByteArrayInputStream(bufInput);
            GZIPInputStream gzis = new GZIPInputStream(bais);
            byte bufTemp[] = new byte[65535];
            while (gzis.available() > 0) {
                int iRead = gzis.read(bufTemp);
                if (iRead > 0)
                    baos.write(bufTemp, 0, iRead); //append to the buffer
            }
            gzis.close();
            bufReturn = baos.toByteArray();
        } catch (Exception ie) {
            return null;
        }

        return bufReturn;
    }
}

Related

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