Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] input = "this is a test".getBytes();

        Deflater compressor = new Deflater();
        compressor.setLevel(Deflater.BEST_COMPRESSION);

        compressor.setInput(input);
        compressor.finish();

        ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

        byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            int count = compressor.deflate(buf);
            bos.write(buf, 0, count);
        }
        bos.close();

        byte[] compressedData = bos.toByteArray();
        Inflater decompressor = new Inflater();
        decompressor.setInput(compressedData);

        bos = new ByteArrayOutputStream(compressedData.length);

        buf = new byte[1024];
        while (!decompressor.finished()) {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        }
        bos.close();

        byte[] decompressedData = bos.toByteArray();
        System.out.println(new String(decompressedData));
    }
}