From b3260f08cf9066367f63dccbac60798d555e52b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Tue, 1 May 2012 08:38:01 +0200 Subject: Detect unsupported compression types. --- Lib/test/test_zipfile.py | 11 +++++++++++ Lib/zipfile.py | 21 +++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index c6039cc..dcf8f74 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -992,6 +992,17 @@ class OtherTests(unittest.TestCase): caught.""" self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1) + def test_unsupported_compression(self): + # data is declared as shrunk, but actually deflated + data = (b'PK\x03\x04.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00' + b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01' + b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00' + b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00' + b'/\x00\x00\x00!\x00\x00\x00\x00\x00') + with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf: + self.assertRaises(NotImplementedError, zipf.open, 'x') + def test_null_byte_in_filename(self): """Check that a filename containing a null byte is properly terminated.""" diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 1cdfceb..ff7f30f 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -504,12 +504,29 @@ def _get_compressor(compress_type): def _get_decompressor(compress_type): - if compress_type == ZIP_DEFLATED: + if compress_type == ZIP_STORED: + return None + elif compress_type == ZIP_DEFLATED: return zlib.decompressobj(-15) elif compress_type == ZIP_BZIP2: return bz2.BZ2Decompressor() else: - return None + unknown_compressors = { + 1: 'shrink', + 2: 'reduce', + 3: 'reduce', + 4: 'reduce', + 5: 'reduce', + 6: 'implode', + 9: 'enhanced deflate', + 10: 'implode', + 14: 'lzma', + } + descr = unknown_compressors.get(compress_type) + if descr: + raise NotImplementedError("compression type %d (%s)" % (compress_type, descr)) + else: + raise NotImplementedError("compression type %d" % (compress_type,)) class ZipExtFile(io.BufferedIOBase): -- cgit v0.12