Last active
April 6, 2018 14:43
-
-
Save mdellavo/555e9b98104f06c995c2b170d006bdf6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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