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.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;

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

public class Main {
    public static String compressString(String str, String encoding) {
        String compressedString = "";

        try {
            byte[] input = str.getBytes(encoding);
            Deflater d = new Deflater();
            d.setLevel(Deflater.BEST_COMPRESSION);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            DeflaterOutputStream dout = new DeflaterOutputStream(out, d);
            dout.write(input);
            dout.close();
            compressedString = new String(Hex.encodeHex(out.toByteArray()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return compressedString;
    }
}