// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.zip.*;

public class Unzip {
    public static byte[] zipData(byte[] input) {
        Deflater comp = new Deflater();
        comp.setInput(input);
        comp.finish();
        byte[] tmp = new byte[2*input.length];
        int n = comp.deflate(tmp);
        byte[] b = new byte[comp.getTotalOut()];
        System.arraycopy(tmp, 0, b, 0, n);
        return b;
    }
    public static byte[] unzipData(byte[] input) throws DataFormatException {
        Inflater decomp = new Inflater();
        decomp.setInput(input);
        byte[] tmp = new byte[20*input.length];
        int n = decomp.inflate(tmp);
        byte[] b = new byte[decomp.getTotalOut()];
        System.arraycopy(tmp, 0, b, 0, n);
        return b;
    }
    public static void main(String[] args) throws Exception {
        String s = "Dette er en test - test test test test test !";
        byte[] b1 = s.getBytes();
        byte[] b2 = zipData(b1);
        byte[] b3 = unzipData(b2);
        System.out.println(b1.length + "->" + b2.length + "->" + b3.length);
        System.out.println(new String(b3));
        byte[] b4 = { 0x00, 0x01, 0x02, 0x03, 0x04 };
        byte[] b5 = unzipData(b4);
        System.out.println(new String(b5));
    }
}
