Java Zip Byte Array zip(byte[] bytes)

Here you can find the source of zip(byte[] bytes)

Description

Compresses the given byte array using the standard Java deflater (which uses the zlib compression library internally).

License

Open Source License

Declaration

public static byte[] zip(byte[] bytes) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.util.zip.Deflater;

public class Main {
    /**//from w ww. j a  va  2s. c om
     * Compresses the given byte array using the standard Java deflater (which
     * uses the zlib compression library internally).
     */
    public static byte[] zip(byte[] bytes) throws IOException {
        if (bytes == null)
            return null;
        if (bytes.length == 0)
            return new byte[0];
        Deflater deflater = new Deflater();
        deflater.setInput(bytes);
        ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length);
        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer);
            out.write(buffer, 0, count);
        }
        out.close();
        return out.toByteArray();
    }
}

Related

  1. doZip(byte[] data)
  2. zip(byte[] data)
  3. zip(byte[] datas)
  4. zip(byte[] in)
  5. zip(byte[] input)