28 lines
772 B
Python
28 lines
772 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
|
|
|
|
class SizeInfo(object):
|
|
def __init__(self):
|
|
self.size = 0
|
|
|
|
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.
|
|
"""
|
|
|
|
size_info = SizeInfo()
|
|
|
|
decompressor = zlib.decompressobj(ZLIB_GZIP_WINDOW)
|
|
|
|
def fn(buf):
|
|
size_info.size += len(decompressor.decompress(buf))
|
|
|
|
return size_info, fn
|