26 lines
758 B
Python
26 lines
758 B
Python
|
"""
|
||
|
Defines utility methods for working with gzip streams.
|
||
|
"""
|
||
|
|
||
|
import zlib
|
||
|
|
||
|
# Window size for decompressing GZIP streams.
|
||
|
# This results in ZLIB automatically detecting the GZIP headers.
|
||
|
# http://stackoverflow.com/questions/3122145/zlib-error-error-3-while-decompressing-incorrect-header-check/22310760#22310760
|
||
|
ZLIB_GZIP_WINDOW = zlib.MAX_WBITS | 32
|
||
|
|
||
|
def calculate_size_handler():
|
||
|
""" Returns an object and a SocketReader handler. The handler will gunzip the data it receives,
|
||
|
adding the size found to the object.
|
||
|
"""
|
||
|
uncompressed_size_info = {
|
||
|
'size': 0
|
||
|
}
|
||
|
|
||
|
decompressor = zlib.decompressobj(ZLIB_GZIP_WINDOW)
|
||
|
|
||
|
def fn(buf):
|
||
|
uncompressed_size_info['size'] += len(decompressor.decompress(buf))
|
||
|
|
||
|
return uncompressed_size_info, fn
|