-
Notifications
You must be signed in to change notification settings - Fork 2
Better ZIP deflate support #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import random | ||
| import struct | ||
| import zipfile | ||
| import zlib | ||
|
|
||
| import inflate64 | ||
| from pytest import mark, raises | ||
|
|
||
| from vecorel_cli.conversion.base import BaseConverter | ||
| from vecorel_cli.conversion.deflate64 import ZIP_DEFLATED64, deflate64_zip | ||
|
|
||
| # Compressible content fits one read; random bytes need many, which exercises the | ||
| # streaming decompressor protocol of ZipExtFile. | ||
| CONTENTS = { | ||
| "compressible": b"deflate64 " * 20_000, | ||
| "incompressible": random.Random(64).randbytes(2_000_000), | ||
| } | ||
|
|
||
|
|
||
| def _write_deflate64_zip(path, data, name="member.txt"): | ||
| """A single-member ZIP compressed with Deflate64, which zipfile cannot write.""" | ||
| deflater = inflate64.Deflater() | ||
| compressed = deflater.deflate(data) + deflater.flush() | ||
| crc = zlib.crc32(data) | ||
| encoded = name.encode() | ||
| sizes = (crc, len(compressed), len(data), len(encoded)) | ||
| local = struct.pack("<4s5H3L2H", b"PK\x03\x04", 21, 0, ZIP_DEFLATED64, 0, 0, *sizes, 0) | ||
| central = struct.pack( | ||
| "<4s6H3L5H2L", b"PK\x01\x02", 21, 21, 0, ZIP_DEFLATED64, 0, 0, *sizes, 0, 0, 0, 0, 0, 0 | ||
| ) | ||
| offset = len(local) + len(encoded) + len(compressed) | ||
| end = struct.pack("<4s4H2LH", b"PK\x05\x06", 0, 0, 1, 1, len(central) + len(encoded), offset, 0) | ||
| path.write_bytes(local + encoded + compressed + central + encoded + end) | ||
| return path | ||
|
|
||
|
|
||
| @mark.parametrize("kind", CONTENTS) | ||
| def test_zipfile_reads_deflate64_only_inside_the_block(tmp_path, kind): | ||
| archive = _write_deflate64_zip(tmp_path / "a.zip", CONTENTS[kind]) | ||
| with raises(NotImplementedError): | ||
| zipfile.ZipFile(archive).read("member.txt") | ||
| with deflate64_zip(): | ||
| # read() verifies the CRC, so a truncated or corrupt stream fails here | ||
| assert zipfile.ZipFile(archive).read("member.txt") == CONTENTS[kind] | ||
| with raises(NotImplementedError): | ||
| zipfile.ZipFile(archive).read("member.txt") | ||
|
|
||
|
|
||
| def test_overlapping_blocks_restore_zipfile_whatever_order_they_exit_in(): | ||
| original = zipfile._get_decompressor | ||
| first, second = deflate64_zip(), deflate64_zip() | ||
| first.__enter__() | ||
| second.__enter__() | ||
| first.__exit__(None, None, None) | ||
| assert zipfile._get_decompressor is not original # the second block still needs it | ||
| second.__exit__(None, None, None) | ||
| assert zipfile._get_decompressor is original | ||
|
|
||
|
|
||
| @mark.parametrize("kind", CONTENTS) | ||
| def test_download_files_extracts_deflate64_archives(tmp_path, kind): | ||
| archive = _write_deflate64_zip(tmp_path / "source.zip", CONTENTS[kind]) | ||
| paths = BaseConverter().download_files( | ||
| {str(archive): ["member.txt"]}, cache_folder=str(tmp_path / "cache") | ||
| ) | ||
| assert len(paths) == 1 | ||
| with open(paths[0][0], "rb") as f: | ||
| assert f.read() == CONTENTS[kind] |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import threading | ||
| import zipfile | ||
| from contextlib import contextmanager | ||
|
|
||
| import inflate64 | ||
|
|
||
| # Deflate64 ("enhanced deflate", ZIP method 9) is what Windows writes for large archives, | ||
| # and python's zipfile cannot read it. inflate64, a dependency of py7zr, decodes it. | ||
| ZIP_DEFLATED64 = 9 | ||
|
|
||
| _lock = threading.Lock() | ||
| _users = 0 | ||
| _original_get_decompressor = zipfile._get_decompressor | ||
|
|
||
|
|
||
| class _Deflate64Decompressor: | ||
| """The interface zipfile.ZipExtFile expects of a decompressor other than zlib's | ||
| (Python 3.11 to 3.14): decompress(data) and eof.""" | ||
|
|
||
| def __init__(self): | ||
| self._inflater = inflate64.Inflater() | ||
|
|
||
| @property | ||
| def eof(self): | ||
| return self._inflater.eof | ||
|
|
||
| def decompress(self, data): | ||
| return self._inflater.inflate(data) | ||
|
|
||
|
|
||
| def _get_decompressor(compress_type): | ||
| if compress_type == ZIP_DEFLATED64: | ||
| return _Deflate64Decompressor() | ||
| return _original_get_decompressor(compress_type) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def deflate64_zip(): | ||
| """Let zipfile read Deflate64 members for the duration of the block. | ||
|
|
||
| The hook is process-global, so it is counted: installed by the first block to enter | ||
| and restored by the last to exit, whatever order overlapping blocks exit in.""" | ||
| global _users | ||
| with _lock: | ||
| if _users == 0: | ||
| zipfile._get_decompressor = _get_decompressor | ||
| _users += 1 | ||
| try: | ||
| yield | ||
| finally: | ||
| with _lock: | ||
| _users -= 1 | ||
| if _users == 0: | ||
| zipfile._get_decompressor = _original_get_decompressor | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.