Java Data Compression
I ran a test to compress one million consecutive integers using various forms of compression. The results are as follows:
None 4000027 Deflate 2006803 Filtered 1391833 BZip2 427067 Lzma 255040
<geshi lang="java"> public class IntegerCompression extends TestCase {
private enum CompressMethod { None, Deflate, Filtered, BZip2 }
private static int SIZE = 1000000;
private int[] getData() { int[] data = new int[SIZE]; for (int i = 0; i < SIZE; i++) { data[i] = i; } return data; }
public void testDeflate() throws IOException {
for (CompressMethod m : CompressMethod.values()) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream( compress(byteArrayOutputStream, m)); objectOutputStream.writeObject(getData()); objectOutputStream.close(); System.out.println(m + "\t" + byteArrayOutputStream.size()); } }
public void testLzma() throws IOException { Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream); oos.writeObject(getData()); oos.close(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); byteArrayOutputStream = new ByteArrayOutputStream(); encoder.Code(byteArrayInputStream, byteArrayOutputStream, null); System.out.println("Lzma\t" + byteArrayOutputStream.size()); }
private OutputStream compress(OutputStream stream, CompressMethod m) throws IOException { switch (m) { case None: return stream; case Deflate: return new DeflaterOutputStream(stream, new Deflater(Deflater.DEFAULT_COMPRESSION, true)); case Filtered: return new DeflaterOutputStream(stream, new Deflater(Deflater.FILTERED , true)); case BZip2: return new CBZip2OutputStream(stream); } throw new IllegalArgumentException(); }
} </geshi>