Java GZip gzipDecompress(byte[] compressed)

Here you can find the source of gzipDecompress(byte[] compressed)

Description

gzip Decompress

License

Open Source License

Declaration

public static String gzipDecompress(byte[] compressed) throws IOException 

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.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;

public class Main {
    public static String gzipDecompress(byte[] compressed) throws IOException {
        byte[] bytes = gzipDecompressBytes(compressed);
        return new String(bytes, StandardCharsets.UTF_8);
    }//from   w w w.  j  ava 2 s .co m

    public static byte[] gzipDecompressBytes(byte[] compressed) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        final int BUFFER_SIZE = 32;
        ByteArrayInputStream is = new ByteArrayInputStream(compressed);
        GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
        byte[] data = new byte[BUFFER_SIZE];
        int bytesRead;
        while ((bytesRead = gis.read(data)) != -1) {
            os.write(data, 0, bytesRead);
        }
        gis.close();
        return os.toByteArray();
    }
}

Related

  1. gzip(final T o)
  2. gzip(String s)
  3. gzip(String str)
  4. gzipFileReader(String file)
  5. gzipReader(final InputStream inputStream)
  6. gzipString(String src, byte default_value[])