[3.14] gh-132983: Fix small issues with zstd support in zipfile (GH-133723) (#133974)

gh-132983: Fix small issues with zstd support in zipfile (GH-133723)
(cherry picked from commit 35f47d0589)

Co-authored-by: Carey Metcalfe <carey@cmetcalfe.ca>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Emma Smith <emma@emmatyping.dev>
This commit is contained in:
Miss Islington (bot) 2025-05-13 18:10:59 +02:00 committed by GitHub
parent 24847d04b2
commit b94a63c0f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -38,8 +38,8 @@ except ImportError:
__all__ = ["BadZipFile", "BadZipfile", "error", __all__ = ["BadZipFile", "BadZipfile", "error",
"ZIP_STORED", "ZIP_DEFLATED", "ZIP_BZIP2", "ZIP_LZMA", "ZIP_STORED", "ZIP_DEFLATED", "ZIP_BZIP2", "ZIP_LZMA",
"is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile", "ZIP_ZSTANDARD", "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile",
"Path"] "LargeZipFile", "Path"]
class BadZipFile(Exception): class BadZipFile(Exception):
pass pass
@ -812,11 +812,11 @@ def _get_compressor(compress_type, compresslevel=None):
if compresslevel is not None: if compresslevel is not None:
return bz2.BZ2Compressor(compresslevel) return bz2.BZ2Compressor(compresslevel)
return bz2.BZ2Compressor() return bz2.BZ2Compressor()
# compresslevel is ignored for ZIP_LZMA and ZIP_ZSTANDARD # compresslevel is ignored for ZIP_LZMA
elif compress_type == ZIP_LZMA: elif compress_type == ZIP_LZMA:
return LZMACompressor() return LZMACompressor()
elif compress_type == ZIP_ZSTANDARD: elif compress_type == ZIP_ZSTANDARD:
return zstd.ZstdCompressor() return zstd.ZstdCompressor(level=compresslevel)
else: else:
return None return None
@ -1352,7 +1352,8 @@ class ZipFile:
mode: The mode can be either read 'r', write 'w', exclusive create 'x', mode: The mode can be either read 'r', write 'w', exclusive create 'x',
or append 'a'. or append 'a'.
compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib), compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib),
ZIP_BZIP2 (requires bz2) or ZIP_LZMA (requires lzma). ZIP_BZIP2 (requires bz2), ZIP_LZMA (requires lzma), or
ZIP_ZSTANDARD (requires compression.zstd).
allowZip64: if True ZipFile will create files with ZIP64 extensions when allowZip64: if True ZipFile will create files with ZIP64 extensions when
needed, otherwise it will raise an exception when this would needed, otherwise it will raise an exception when this would
be necessary. be necessary.
@ -1361,6 +1362,9 @@ class ZipFile:
When using ZIP_STORED or ZIP_LZMA this keyword has no effect. When using ZIP_STORED or ZIP_LZMA this keyword has no effect.
When using ZIP_DEFLATED integers 0 through 9 are accepted. When using ZIP_DEFLATED integers 0 through 9 are accepted.
When using ZIP_BZIP2 integers 1 through 9 are accepted. When using ZIP_BZIP2 integers 1 through 9 are accepted.
When using ZIP_ZSTANDARD integers -7 though 22 are common,
see the CompressionParameter enum in compression.zstd for
details.
""" """
@ -2093,6 +2097,8 @@ class ZipFile:
min_version = max(BZIP2_VERSION, min_version) min_version = max(BZIP2_VERSION, min_version)
elif zinfo.compress_type == ZIP_LZMA: elif zinfo.compress_type == ZIP_LZMA:
min_version = max(LZMA_VERSION, min_version) min_version = max(LZMA_VERSION, min_version)
elif zinfo.compress_type == ZIP_ZSTANDARD:
min_version = max(ZSTANDARD_VERSION, min_version)
extract_version = max(min_version, zinfo.extract_version) extract_version = max(min_version, zinfo.extract_version)
create_version = max(min_version, zinfo.create_version) create_version = max(min_version, zinfo.create_version)