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;

import org.apache.commons.codec.binary.Base64;

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

        try {
            byte[] bytes = Base64.decodeBase64(str.getBytes(encoding));
            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;
    }
}