diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-09-23 21:34:24 +0300 |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-09-23 21:34:24 +0300 |
commit | cfbb394488aba172199b684c7266240280c0ebd2 (patch) | |
tree | a11f63cd7c335fc29dc3dc137b70c3c5f61e51c7 /Lib/zipfile.py | |
parent | 8aa8c47db2f0205dec61d50274817cf6b0b15e0a (diff) | |
download | cpython-git-cfbb394488aba172199b684c7266240280c0ebd2.tar.gz |
Issue #21866: ZipFile.close() no longer writes ZIP64 central directory
records if allowZip64 is false.
Diffstat (limited to 'Lib/zipfile.py')
-rw-r--r-- | Lib/zipfile.py | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/Lib/zipfile.py b/Lib/zipfile.py index c57765753f..e5b7818635 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -50,7 +50,7 @@ error = BadZipfile = BadZipFile # Pre-3.2 compatibility names ZIP64_LIMIT = (1 << 31) - 1 -ZIP_FILECOUNT_LIMIT = 1 << 16 +ZIP_FILECOUNT_LIMIT = (1 << 16) - 1 ZIP_MAX_COMMENT = (1 << 16) - 1 # constants for Zip file compression methods @@ -1304,13 +1304,17 @@ class ZipFile: raise RuntimeError( "Attempt to write ZIP archive that was already closed") _check_compression(zinfo.compress_type) - if zinfo.file_size > ZIP64_LIMIT: - if not self._allowZip64: - raise LargeZipFile("Filesize would require ZIP64 extensions") - if zinfo.header_offset > ZIP64_LIMIT: - if not self._allowZip64: - raise LargeZipFile( - "Zipfile size would require ZIP64 extensions") + if not self._allowZip64: + requires_zip64 = None + if len(self.filelist) >= ZIP_FILECOUNT_LIMIT: + requires_zip64 = "Files count" + elif zinfo.file_size > ZIP64_LIMIT: + requires_zip64 = "Filesize" + elif zinfo.header_offset > ZIP64_LIMIT: + requires_zip64 = "Zipfile size" + if requires_zip64: + raise LargeZipFile(requires_zip64 + + " would require ZIP64 extensions") def write(self, filename, arcname=None, compress_type=None): """Put the bytes from filename into the archive under the name @@ -1464,10 +1468,8 @@ class ZipFile: try: if self.mode in ("w", "a") and self._didModify: # write ending records - count = 0 pos1 = self.fp.tell() for zinfo in self.filelist: # write central directory - count = count + 1 dt = zinfo.date_time dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2] dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2) @@ -1531,13 +1533,21 @@ class ZipFile: pos2 = self.fp.tell() # Write end-of-zip-archive record - centDirCount = count + centDirCount = len(self.filelist) centDirSize = pos2 - pos1 centDirOffset = pos1 - if (centDirCount >= ZIP_FILECOUNT_LIMIT or - centDirOffset > ZIP64_LIMIT or - centDirSize > ZIP64_LIMIT): + requires_zip64 = None + if centDirCount > ZIP_FILECOUNT_LIMIT: + requires_zip64 = "Files count" + elif centDirOffset > ZIP64_LIMIT: + requires_zip64 = "Central directory offset" + elif centDirSize > ZIP64_LIMIT: + requires_zip64 = "Central directory size" + if requires_zip64: # Need to write the ZIP64 end-of-archive records + if not self._allowZip64: + raise LargeZipFile(requires_zip64 + + " would require ZIP64 extensions") zip64endrec = struct.pack( structEndArchive64, stringEndArchive64, 44, 45, 45, 0, 0, centDirCount, centDirCount, |