Skip to content

Instantly share code, notes, and snippets.

@mdellavo
Last active April 6, 2018 14:43
Show Gist options
  • Save mdellavo/555e9b98104f06c995c2b170d006bdf6 to your computer and use it in GitHub Desktop.
Save mdellavo/555e9b98104f06c995c2b170d006bdf6 to your computer and use it in GitHub Desktop.
import zlib
class ZipStream(object):
GZIP_MAGIC = 32 + zlib.MAX_WBITS
CHUNK_SIZE = 4 * 4096
def __init__(self, f):
self.decompress = zlib.decompressobj(self.GZIP_MAGIC)
self.file = f
self.buffer = ""
def read(self, bytes):
# fill decompress obj
while len(self.buffer) < bytes:
chunk = self.file.read(self.CHUNK_SIZE)
if not chunk:
self.buffer += self.decompress.flush()
break
self.buffer += self.decompress(chunk)
rv = self.buffer[:bytes]
self.buffer = self.buffer[bytes:]
return rv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment