Skip to content

Instantly share code, notes, and snippets.

@Suor
Last active August 29, 2015 14:23
Show Gist options
  • Save Suor/4d6bb599a5f3c367c432 to your computer and use it in GitHub Desktop.
Save Suor/4d6bb599a5f3c367c432 to your computer and use it in GitHub Desktop.
Ungzip stream on the fly in Python 2
import gzip, zlib
def ungzip_stream(fd):
plain_fd = gzip.GzipFile(fileobj=fd, mode="r")
# NOTE: this is what GzipFile does on new file start,
# we do that directly as GzipFile tries to seek() before it
# and fd could be unseekable().
plain_fd._init_read()
plain_fd._read_gzip_header()
plain_fd.decompress = zlib.decompressobj(-zlib.MAX_WBITS)
plain_fd._new_member = False
return plain_fd
@Suor
Copy link
Author

Suor commented Jul 3, 2015

This doesn't always work, I wrote proper solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment