Question

I've made a program which generate a Huffman's Codes from input file. Now I don't know how to end output file(which should be compressed based on the input file and generated codes). Lets say that we have 43 bits to save so how end the file? I can simply add 5 zeros in the end, but then codes could be wrong. Any ideas? PS Sorry for my bad English.

Was it helpful?

Solution

There are two ways to solve this that I can think of:

  • Put the length of the uncompressed data in front of the compressed data. Then, when you're decompressing, count how many characters you have decompressed and stop after the right number.
  • Put a special end symbol into your huffman tree that you only use at the end of the file. Then, when uncompressing, stop when you see that symbol.

OTHER TIPS

From wikipedia:

In any case, since the compressed data can include unused "trailing bits" the decompressor must be able to determine when to stop producing output. This can be accomplished by either transmitting the length of the decompressed data along with the compression model or by defining a special code symbol to signify the end of input (the latter method can adversely affect code length optimality, however).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top