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

public class Main {
    public static byte[] compressInZlib(byte[] originalData) throws Exception {
        return compressInZlib(originalData, 0, originalData.length);
    }

    public static byte[] compressInZlib(byte[] originalData, int offset, int length) throws IOException {

        Deflater compresser = new Deflater();
        compresser.setInput(originalData, offset, length);
        compresser.finish();

        ByteArrayOutputStream bos = new ByteArrayOutputStream(length);

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

        byte[] compressData = bos.toByteArray();
        bos.close();

        return compressData;
    }
}