Try using DeflatorOutputStream, everything you write to it is compressed automatically. InflatorInputStream does the reverse.
Write to
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;
public class CompressTest {
public static void main(String args[]) throws IOException {
String str = readFile(args[0]);
compressText(str);
}
public static String readFile(String fileName) throws IOException {
File file = new File(fileName);
byte[] bytes = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bytes);
fis.close();
return new String(bytes, 0);
}
public static void compressText(String text) {
try {
byte[] stringBytes = text.getBytes();
long start = System.currentTimeMillis();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(baos);
dos.write(stringBytes);
dos.close();
byte[] bytes = baos.toByteArray();
long end = System.currentTimeMillis();
double compressionRatio = (double) text.length() / bytes.length;
double runTime = (end - start) / 1000.0;
System.out.println("Compression ratio=" + compressionRatio + ", compression time=" + runTime + " secs");
} catch (IOException e) {
e.printStackTrace();
}
}
}
I ran this for the king james version of the bible. A big text file from ftp://unboundftp.biola.edu/pub/kjv.zip and got the following results for the text file.
Compression ratio=3.2839847108115876, compression time=1.047 secs
That is about 1 second to compress a 5 MB of text (on a 1.8 GHz AMD)
My arithmetic compressor did around 30kB/s - 300kB/s IIRC
It was on computer underclocked to 1.8 GHz
I consider compressing 700MB avi under an hour as very nice for an arithmetic compressor, so... You have the answer.
Static "huffman" tree is faster, but not as much interesting.
Raghar