Compress a Byte Array in Java

Description

The following code shows how to compress a Byte Array.

Example


//  ww  w .j  ava 2s .c  om
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.zip.Deflater;

public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] input = "www.java2s.com".getBytes();

    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    compressor.setInput(input);
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
      int count = compressor.deflate(buf);
      bos.write(buf, 0, count);
    }
    bos.close();
    byte[] compressedData = bos.toByteArray();
    System.out.println(Arrays.toString(compressedData));
    
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip