Showing posts with label Inflater. Show all posts
Showing posts with label Inflater. Show all posts

Tuesday, June 28, 2016

Java code to compress & decompress of string using deflater & inflater

Code snippet for compress & decompress of string...

package com.pritom.kumar;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/**
 * Created by pritom on 27/06/2016.
 */
public class CompressString {
    public static final String encoding = "UTF-8";

    public static void main (String[] args) throws Exception {
        String s = "123456789 a s d f g h j k l ; ' q w e r t y u i o p [ ] \\ 1 2 3 4 5 6 7 8 9 0 - = as df gh jk l; zx cv bn m, ./ qw y ui op []" +
                "qwe rty uio p[] asd fgh jkl zxc vbn m,. 123 456 789 1234 5678 90-= qwer tyui op[] asdf ghjk l;zx cvbn m,./" +
                "12345 67890 qwert yuiop asdfg hjklz xcvbn m,khg er xcgvdst 453 gd fyrt634 5 dg e653 545u7r ydf dgfsd fsart sdgdsg" +
                "sd fsdjf;sjduw[r sldfjsl;djrf2345203v sdfs'wse[rftu 2[3r50 j;lgjks'drf 2354-0u sadfas';dfmka][ ert02u9wsd's;lad fa'sdfjasdgf" +
                "4 wr[w09u jsd' af32 'QWRJE ajfsdj :ejwkiiuUUSJ W[9230 U'SFJKS JFJSF 'ASDP;GF SDFAW435]2 JKSD';GDALSJ GF'SDAGJFMA'L 2UWQ 'PS;DGJM" +
                "ju=[0a9 u0wr w435r]2345ru]2tu  s'adgjfa'sgf2uq]34u]23'g jfq4323o4 23w 52]5-2t4ewsjtrg'qwpe[fa7 2   03r/ dshgf" +
                "djl3r4 sjdf ;wq3r q3ra[d0aw'tj3 wr[w09u asd fgh zxc er gd swser9[0345r]2 3dfjsdfsd ]23-qg';gvas 'dgawejrt ]23trwasdg;mas'fgsnkd gasf " +
                "[w90rtusdmgkf zxc sadfs fa fsart sdgdsg wr JFJSF GF fgh xcgvdst dfmka SDAGJFMA 2345ru wse[rftu 2[3r50 j;l af32 sdfjasdgf jks'drf 2354-" +
                "djrf2345203v KSD';GDALSJ GF'";

        String compressed = compress(s);
        Integer savedLength = s.length() - compressed.length();
        Double saveRatio = (new Double(savedLength) * 100) / s.length();
        String ratioString = saveRatio.toString() + "00000000";
        ratioString = ratioString.substring(0, ratioString.indexOf(".") + 4);
        println("Original_String_Length=" + s.length());
        println("Compressed_String_Length=" + compressed.length() + ", Compression_Ratio=" + ratioString + "%");

        String decompressed = decompress(decodeBase64(compressed));
        println("Decompressed_String_Length=" + decompressed.length() + " == Original_String_Length (" + s.length() + ")");
        println("Original_String == Decompressed_String=" + (s.equals(decompressed) ? "True" : "False"));
    }

    public static String compress(String str) throws Exception {
        return compress(str.getBytes(encoding));
    }

    public static String compress(byte[] bytes) throws Exception {
        Deflater deflater = new Deflater();
        deflater.setInput(bytes);
        deflater.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
        byte[] buffer = new byte[1024];
        while(!deflater.finished()) {
            int count = deflater.deflate(buffer);
            bos.write(buffer, 0, count);
        }
        bos.close();
        byte[] output = bos.toByteArray();
        return encodeBase64(output);
    }

    public static String decompress(byte[] bytes) throws Exception {
        Inflater inflater = new Inflater();
        inflater.setInput(bytes);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            bos.write(buffer, 0, count);
        }
        bos.close();
        byte[] output = bos.toByteArray();
        return new String(output);
    }

    public static String encodeBase64(byte[] bytes) throws Exception {
        BASE64Encoder base64Encoder = new BASE64Encoder();
        return base64Encoder.encodeBuffer(bytes).replace("\r\n", "").replace("\n", "");
    }

    public static byte[] decodeBase64(String str) throws Exception {
        BASE64Decoder base64Decoder = new BASE64Decoder();
        return base64Decoder.decodeBuffer(str);
    }

    public static void println(Object o) {
        System.out.println("" + o);
    }
}

Output would be like this...

Original_String_Length=1000
Compressed_String_Length=824, Compression_Ratio=17.600%
Decompressed_String_Length=1000 == Original_String_Length (1000)
Original_String == Decompressed_String=True

** More big string more compression ratio
** More similar words more compression ratio

Ideone link