Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a5fc4c

Browse files
committed
#14313: zipfile now raises NotImplementedError when the compression type is unknown.
1 parent a69be28 commit 6a5fc4c

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

Lib/test/test_zipfile.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,17 @@ def test_bad_compression_mode(self):
922922
caught."""
923923
self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
924924

925+
def test_unsupported_compression(self):
926+
# data is declared as shrunk, but actually deflated
927+
data = (b'PK\x03\x04.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00'
928+
b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01'
929+
b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00'
930+
b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
931+
b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00'
932+
b'/\x00\x00\x00!\x00\x00\x00\x00\x00')
933+
with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
934+
self.assertRaises(NotImplementedError, zipf.open, 'x')
935+
925936
def test_null_byte_in_filename(self):
926937
"""Check that a filename containing a null byte is properly
927938
terminated."""

Lib/zipfile.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,28 @@ def __call__(self, c):
461461
self._UpdateKeys(c)
462462
return c
463463

464+
465+
compressor_names = {
466+
0: 'store',
467+
1: 'shrink',
468+
2: 'reduce',
469+
3: 'reduce',
470+
4: 'reduce',
471+
5: 'reduce',
472+
6: 'implode',
473+
7: 'tokenize',
474+
8: 'deflate',
475+
9: 'deflate64',
476+
10: 'implode',
477+
12: 'bzip2',
478+
14: 'lzma',
479+
18: 'terse',
480+
19: 'lz77',
481+
97: 'wavpack',
482+
98: 'ppmd',
483+
}
484+
485+
464486
class ZipExtFile(io.BufferedIOBase):
465487
"""File-like object for reading an archive member.
466488
Is returned by ZipFile.open().
@@ -487,6 +509,12 @@ def __init__(self, fileobj, mode, zipinfo, decrypter=None,
487509

488510
if self._compress_type == ZIP_DEFLATED:
489511
self._decompressor = zlib.decompressobj(-15)
512+
elif self._compress_type != ZIP_STORED:
513+
descr = compressor_names.get(self._compress_type)
514+
if descr:
515+
raise NotImplementedError("compression type %d (%s)" % (self._compress_type, descr))
516+
else:
517+
raise NotImplementedError("compression type %d" % (self._compress_type,))
490518
self._unconsumed = b''
491519

492520
self._readbuffer = b''

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ Core and Builtins
164164
Library
165165
-------
166166

167+
- Issue #14313: zipfile now raises NotImplementedError when the compression
168+
type is unknown.
169+
167170
- Issue #16408: Fix file descriptors not being closed in error conditions
168171
in the zipfile module. Patch by Serhiy Storchaka.
169172

0 commit comments

Comments
 (0)