Search code examples
pythonzipzlibdeflate

How to get zlib (or another library) to create Type 01 DEFLATE block


I'm writing a DEFLATE uncompressor (in Python), and wanting to test it with Type 01 blocks (i.e. with the fixed Huffman code in of 3.2.6. of RFC 1951.

I know I can create them myself, but I would like to test it with blocks created by some other code, e.g. zlib. How can I do this? So far from my testing, zlib has always created Type 02 blocks (i.e. dynamic Huffman codes), or Type 00 blocks (not compressed).

(This is ultimately to be used in an unZIP code)


Solution

  • Ah found out how: passing strategy=zlib.Z_FIXED to compressobj

    import zlib
    
    compressobj = zlib.compressobj(wbits=-zlib.MAX_WBITS, strategy=zlib.Z_FIXED)
    compressed_stream = compressobj.compress(b'Some data') + compressobj.flush()