Created
April 1, 2014 15:15
-
-
Save w-vi/9916230 to your computer and use it in GitHub Desktop.
How to call zlib to get the gzip inflate / deflate ... definitely not sure if it works in general but so far haven't found where it doesn't.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zlib | |
""" | |
zlib.compressobj(...) ⇒ deflateInit(...) | |
compressobj.compress(...) ⇒ deflate(...) | |
zlib.decompressobj(...) ⇒ inflateInit(...) | |
decompressobj.decompress(...) ⇒ inflate(...) | |
""" | |
def deflate(data, compresslevel=9): | |
compress = zlib.compressobj( | |
compresslevel, # level: 0-9 | |
zlib.DEFLATED, # method: must be DEFLATED | |
16 + zlib.MAX_WBITS, # window size in bits: | |
# -15..-8: negate, suppress header | |
# 8..15: normal | |
# 16..30: subtract 16, gzip header | |
zlib.DEF_MEM_LEVEL, # mem level: 1..8/9 | |
0 # strategy: | |
# 0 = Z_DEFAULT_STRATEGY | |
# 1 = Z_FILTERED | |
# 2 = Z_HUFFMAN_ONLY | |
# 3 = Z_RLE | |
# 4 = Z_FIXED | |
) | |
deflated = compress.compress(data) | |
deflated += compress.flush() | |
return deflated | |
def inflate(data): | |
decompress = zlib.decompressobj( | |
16 + zlib.MAX_WBITS # see above | |
) | |
inflated = decompress.decompress(data) | |
inflated += decompress.flush() | |
return inflated | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment