% hzip foo.txt foo.txt.hz HZIP Version 0.1: Compressed foo.txt to foo.txt.hz % hunzip foo.txt.hz foo2.txt HUNZIP Version 0.1: Uncompressed foo.txt.hz to foo2.txt % java Hzip foo.txt foo.txt.hz HZIP Version 0.1: Compressed foo.txt to foo.txt.hz % java Hunzip foo.txt.hz foo2.txt HUNZIP Version 0.1: Uncompressed foo.txt.hz to foo2.txtNotice that foo.txt and foo2.txt should be identical and the compressed (foo.txt.hz) file should certainly (or typically) be significantly smaller in size.
Helpful Tip: To do this, you obviously need to write BITs to a file and not just bytes. To make this space efficient, you need to be careful about how it is written. I am providing a simple Java code class that let's you read bits from a stream and another that lets you write bits to a stream as well as a sample class illustrating how they are used. It is by no means perfect as I don't provide every check needed. In particular, it will read until EOF which has to be on a byte alignment. So, if your compressed code has bits that aren't a multiple of 8 (most likely) you will be writing a few extra bits. This is unavoidable (in fact, you'll also be using a whole block more than needed as that is how files are stored). That is not the problem. The trick is when reading the file back to know when the end of compression occurs. The best way - near the beginning indicate the number of bits to read, or characters to read, or something along that line. Store that as an integer value - just a sequence of 16 bits.
Here is the sample code