decompress Response byte array with Gzip - Android File Input Output

Android examples for File Input Output:Zip File

Description

decompress Response byte array with Gzip

Demo Code


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import com.android.volley.NetworkResponse;

public class Main{
    /*from w w  w. ja  va 2  s  .  co  m*/
    public static byte[] decompressResponse(byte[] compressed)
            throws IOException {
        ByteArrayOutputStream baos = null;
        try {
            int size;
            ByteArrayInputStream memstream = new ByteArrayInputStream(
                    compressed);
            GZIPInputStream gzip = new GZIPInputStream(memstream);
            final int buffSize = 256;
            byte[] tempBuffer = new byte[buffSize];
            baos = new ByteArrayOutputStream();
            while ((size = gzip.read(tempBuffer, 0, buffSize)) != -1) {
                baos.write(tempBuffer, 0, size);
            }
            return baos.toByteArray();
        } finally {
            if (baos != null) {
                baos.close();
            }
        }
    }
}

Related Tutorials