Search code examples
pythonzlibhuffman-code

How do I specify the compression type in zlib?


I have this code which uses zlib to compresses a binary string:

f = open("compressed.bin", "wb")
x = zlib.compress(b"Some text to be compressed")
f.write(x)
f.close()

The data always gets compressed with fixed Huffman codes, regardless of the input string, whereas I want the data to be compressed with dynamic Huffman codes. I even tried using a string with several repetitions (such as 100 'A's, followed by 100 'B's, followed by 100 'A's, and so on) but it also ends up compressed with fixed codes.

Edit: the result is the same if I specify level=9.

How do I specify that the data should be compressed with dynamic codes regardless of which method achieves better compression?


Solution

  • Give it more data. At least a few 10's of K.

    Fixed coding is only used when a fixed block would be smaller than a dynamic block for the same data. That is generally only the case when a small amount of data is being compressed, or for the last deflate block where only a small amount of data remains.