Skip to content

Commit

Permalink
Improve comments in unpack_with_zlib_module
Browse files Browse the repository at this point in the history
  • Loading branch information
alchzh committed Aug 15, 2024
1 parent 9753ca5 commit d954073
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions ofrak_core/ofrak/core/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ async def unpack(self, resource: Resource, config=None):

@staticmethod
async def unpack_with_zlib_module(data: bytes) -> bytes:
chunks = []

# We use zlib.decompressobj instead of the gzip module to decompress
# because of a bug that causes gzip raise BadGzipFile if there's
# trailing garbage after a compressed file instead of correctly ignoring it
# https://github.com/python/cpython/issues/68489
# wbits > 16 handles the gzip header and footer
# We need to create a zlib.Decompress object in order to use this
# parameter in Python < 3.11
decompressor = zlib.decompressobj(wbits=16 + zlib.MAX_WBITS)

# gzip files can consist of multiple members, so we need to read them in
# a loop and concatenate them in the end. \037\213 are magic bytes
# indicating the start of a gzip header.
chunks = []
while data.startswith(b"\037\213"):
chunks.append(decompressor.decompress(data))
if decompressor.eof:
Expand Down

0 comments on commit d954073

Please sign in to comment.