Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayInputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import java.util.zip.GZIPInputStream;

public class Main {
    /**
     * Descomprime uma string utilizando o GZIP
     * 
     * @param str
     * @param encoding
     * @return
     */
    public static String gzipDecompress(byte[] bytes, String encoding) {
        String decompressedString = "";

        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            GZIPInputStream gzip = new GZIPInputStream(bais);
            Reader reader = new InputStreamReader(gzip, encoding);
            StringBuffer sbuf = new StringBuffer();
            char[] buffer = new char[32 * 1024];
            int nread;
            while ((nread = reader.read(buffer)) >= 0) {
                sbuf.append(buffer, 0, nread);
            }
            decompressedString = sbuf.toString();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return decompressedString;
    }
}