Java Ungzip Byte Array unGzip(byte[] str, String charset)

Here you can find the source of unGzip(byte[] str, String charset)

Description

un Gzip

License

Apache License

Declaration

public static String unGzip(byte[] str, String charset) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import java.util.zip.GZIPInputStream;

public class Main {

    public static String unGzip(byte[] str, String charset) throws IOException {
        if (str == null || str.length == 0) {
            return null;
        }/*  w  w  w  . j ava 2  s . c o  m*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(str);
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        out.flush();
        String body = out.toString(charset);
        gunzip.close();
        out.close();
        in.close();

        return body;
    }
}

Related

  1. ungzip(byte[] bytes)
  2. unGZip(byte[] bytes)
  3. unGzip(byte[] data)
  4. unGZip(byte[] data)
  5. ungzip(byte[] in)
  6. ungzipBestEffort(byte[] in)
  7. ungzipBuffer(byte[] bufInput)
  8. ungzipPayload(byte[] compressed)