decompress byte array - Android File Input Output

Android examples for File Input Output:Byte Array Compress

Description

decompress byte array

Demo Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

public class Main {
    public static final int BUFFER_SIZE = 1024;

    public static byte[] decompress(byte[] bytes) throws IOException {

        GZIPInputStream gzip = null;
        ByteArrayOutputStream baos = null;
        try {/*from ww w  . ja v  a 2s . com*/
            baos = new ByteArrayOutputStream();

            gzip = new GZIPInputStream(new ByteArrayInputStream(bytes));

            int len = 0;
            byte data[] = new byte[BUFFER_SIZE];

            while ((len = gzip.read(data, 0, BUFFER_SIZE)) != -1) {
                baos.write(data, 0, len);
            }

            gzip.close();
            baos.flush();

            return baos.toByteArray();

        } finally {
            if (gzip != null) {
                gzip.close();
            }
            if (baos != null) {
                baos.close();
            }
        }
    }
}

Related Tutorials