I used a counter and kept it under or equal to 8. At the end of my reading of the text file, I looked at my counter, and added zero bits into the rest bits that are not filling my byte. At the end, I wrote my counter into the bitstream as the last byte.
When I read my compressed file for decompressing, I saved the last two bytes, and used my last byte, which is just my counter, to tell me how to interpret the second last byte, which has zero's that should be thrown away. The only caution is that you have to keep the bits that are not found in the tree yet at the third last byte. Then you can combine those bits and finish searching.
There might be more intelligent ways of doing this. I am intersted in knowing other people's methods. |