Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.util.zip.GZIPOutputStream;

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

public class Main {
    /**
     * Comprime uma string utilizando o GZIP
     * 
     * @param str
     * @param encoding
     * @return
     */
    public static String gzipCompressString(String str, String encoding) {
        try {
            if (str == null || str.length() == 0) {
                return null;
            }
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(out);
            Writer writer = new OutputStreamWriter(gzip, encoding);
            writer.write(str);
            writer.close();
            byte[] compressedBytes = out.toByteArray();
            return new String(Base64.encodeBase64(compressedBytes), encoding);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}