Java Zip Byte Array zipByte(byte[] data)

Here you can find the source of zipByte(byte[] data)

Description

zip Byte

License

Apache License

Declaration

public static byte[] zipByte(byte[] data) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.util.zip.Deflater;

public class Main {
    private final static int CacheSize = 1024;

    public static byte[] zipByte(byte[] data) {
        Deflater compresser = new Deflater();
        compresser.reset();//ww w .  j ava 2  s  . c om
        compresser.setInput(data);
        compresser.finish();
        byte result[] = new byte[0];
        ByteArrayOutputStream o = new ByteArrayOutputStream(1);

        try {
            byte[] buf = new byte[CacheSize];
            int got = 0;
            while (!compresser.finished()) {
                got = compresser.deflate(buf);
                o.write(buf, 0, got);
            }
            result = o.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                o.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            compresser.end();
        }
        return result;
    }
}

Related

  1. zip(byte[] in)
  2. zip(byte[] input)
  3. zip(byte[] input)
  4. zip(byte[] source)
  5. zip(byte[] uncompressedBytes)
  6. zipBytes(byte[] input)
  7. zipBytes(final Map streams)
  8. zipCompress(byte[] input)