Android Zip Byte Array zipByte(byte[] data)

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

Description

zip Byte

Declaration

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

Method Source Code

//package com.java2s;
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();//from   w w  w . j  a v a2s .  co  m
        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[] data, String fileName, String path)