Index: Lib/zipfile.py =================================================================== --- Lib/zipfile.py (revision 82848) +++ Lib/zipfile.py (working copy) @@ -492,6 +492,15 @@ self.mode = mode self.name = zipinfo.filename + if hasattr(zipinfo, 'CRC'): + self._expected_crc = zipinfo.CRC + else: + self._expected_crc = None + self._file_size = zipinfo.file_size + self._running_crc = crc32(b'') & 0xffffffff + self._crc_offset = 0 + + def readline(self, limit=-1): """Read and return a line from the stream. @@ -613,6 +622,20 @@ self._readbuffer = self._readbuffer[self._offset:] + data self._offset = 0 + # Update the CRC. Don't checksum more than self._file_size bytes. + max_crc_offset = min(self._file_size, len(self._readbuffer)) + if max_crc_offset > self._crc_offset: + self._running_crc = crc32( + self._readbuffer[self._crc_offset:max_crc_offset], + self._running_crc) & 0xffffffff + self._crc_offset = max_crc_offset + + #Check the CRC if we're at the end of the file + if (self._crc_offset == self._file_size and + self._expected_crc is not None and + self._running_crc != self._expected_crc): + raise BadZipfile("Bad CRC-32 for file %r" % self.name) + # Read from buffer. data = self._readbuffer[self._offset: self._offset + n] self._offset += len(data) Index: Lib/test/test_zipfile.py =================================================================== --- Lib/test/test_zipfile.py (revision 82848) +++ Lib/test/test_zipfile.py (working copy) @@ -662,6 +662,17 @@ class OtherTests(unittest.TestCase): + zip_with_bad_crc = ( + b'PK\003\004\024\0\0\0\0\0 \213\212;:r' + b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af' + b'ilehello,AworldP' + b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:' + b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0' + b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi' + b'lePK\005\006\0\0\0\0\001\0\001\0003\000' + b'\0\0/\0\0\0\0\0') + + def test_unicode_filenames(self): with zipfile.ZipFile(TESTFN, "w") as zf: zf.writestr("foo.txt", "Test for unicode filename") @@ -875,6 +886,24 @@ with zipfile.ZipFile(TESTFN, mode="r") as zipfr: self.assertEqual(zipfr.comment, comment2) + def test_testzip_returns_file_with_bad_crc(self): + """Tests that files with bad CRCs return their name from testzip.""" + with open(TESTFN, 'wb') as fp: + fp.write(self.zip_with_bad_crc) + + with zipfile.ZipFile(TESTFN, mode="r") as zipf: + #testzip returns the name of the first corrupt file, or None + self.assertEqual('afile', zipf.testzip()) + + def test_read_bad_crc_raises_BadZipfile(self): + """Tests that files with bad CRCs raise a BadZipfile exception when read.""" + with open(TESTFN, 'wb') as fp: + fp.write(self.zip_with_bad_crc) + + with zipfile.ZipFile(TESTFN, mode="r") as zipf: + with zipf.open('afile', 'r') as corrupt_file: + self.assertRaises(zipfile.BadZipfile, corrupt_file.read) + def tearDown(self): unlink(TESTFN) unlink(TESTFN2)