Example usage for java.util.zip Deflater deflate

List of usage examples for java.util.zip Deflater deflate

Introduction

In this page you can find the example usage for java.util.zip Deflater deflate.

Prototype

public int deflate(byte[] output, int off, int len) 

Source Link

Document

Compresses the input data and fills specified buffer with compressed data.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {
    Deflater def = new Deflater();
    byte[] input = new byte[1024];
    byte[] output = new byte[1024];

    FileInputStream fin = new FileInputStream("a.dat");
    FileOutputStream fout = new FileOutputStream("b.dat");

    int numRead = fin.read(input);

    def.setInput(input, 0, numRead);/*from w ww. java  2 s . com*/

    while (!def.needsInput()) {
        int numCompressedBytes = def.deflate(output, 0, output.length);
        if (numCompressedBytes > 0) {
            fout.write(output, 0, numCompressedBytes);
        }
    }
    def.finish();
    fin.close();
    fout.flush();
    fout.close();
    def.reset();
}