Java tutorial
//package com.java2s; /** * Copyright (c) 2010 Martin Geisse * * This file is distributed under the terms of the MIT license. */ import java.io.ByteArrayOutputStream; import java.util.zip.Deflater; public class Main { /** * DEFLATEs the specified input data. * * @param data the input data * @param dictionary the dictionary, or null if none * @return the compressed data */ public static byte[] deflate(byte[] data, byte[] dictionary) { Deflater deflater = new Deflater(8, true); if (dictionary != null) { deflater.setDictionary(dictionary); } deflater.setInput(data); deflater.finish(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[256]; while (!deflater.finished()) { int n = deflater.deflate(buffer); byteArrayOutputStream.write(buffer, 0, n); } byte[] result = byteArrayOutputStream.toByteArray(); return result; } }