Reputation: 16998
How can I store a Huffman Encoded bit stream as a binary file?
Upvotes: 1
Views: 784
Reputation: 373002
To store the encoding, you'll need to have three things:
There are many ways to solve each of these problems. You could explicitly store the bit patterns for each character in a table, or could just use the same encoding table for all streams you compress. As for how to detect the end of the stream, one option might be to use a pseudo-EOF character to terminate this stream. To do this, as you build up the Huffman encoding tree, add to it a new character with multiplicity one that will serve as a sentinel delineating the end of the stream. On writing the result, you write this character at the end so that you can tell where the stream ends even if it doesn't use a number of bits that fits perfectly into a byte.
To store the actual contents I'd suggest buffering the encoded representation into a bit vector that automatically flushes into a file stream on a multiple of eight bits. Of course, this isn't the only way to do this, so go with whatever works best.
Upvotes: 3