This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/util/gzipstream.py

26 lines
758 B
Python
Raw Normal View History

"""
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