Java Zip Byte Array zip(byte[] source)

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

Description

zip

License

Open Source License

Declaration

public static byte[] zip(byte[] source) 

Method Source Code

//package com.java2s;
/**/*from   w  w  w  . j ava 2  s  .  co  m*/
 * Classe di utilita' per la serializzazione degli oggetti.
 * 
 * <br><br>License:    GNU General Public License<br>
 * 
 * @author     Pier Paolo Ciarravano  
 * @version     Vers. 0.98 (29/09/2009) 
 */

import java.io.ByteArrayOutputStream;

import java.util.zip.Deflater;

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

    public static byte[] zip(byte[] source) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Deflater deflater = new Deflater();
        deflater.setInput(source);
        deflater.finish();

        byte[] buffer = new byte[BUFFER_SIZE_FOR_ZIP];
        int writeByte = 0;
        //int tot = 0;
        while ((writeByte = deflater.deflate(buffer)) != 0) {
            //tot += writeByte;
            //System.out.println(writeByte + " -> " + tot);
            baos.write(buffer, 0, writeByte);
        }
        deflater.end();
        return baos.toByteArray();
    }
}

Related

  1. zip(byte[] data)
  2. zip(byte[] datas)
  3. zip(byte[] in)
  4. zip(byte[] input)
  5. zip(byte[] input)
  6. zip(byte[] uncompressedBytes)
  7. zipByte(byte[] data)
  8. zipBytes(byte[] input)
  9. zipBytes(final Map streams)