From 0293c804101f76796e1bf826717edd3417b2c431 Mon Sep 17 00:00:00 2001 From: Amaury Forgeot d'Arc Date: Sat, 17 Jan 2009 16:46:35 +0000 Subject: [PATCH] Merged revisions 68661 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r68661 | amaury.forgeotdarc | 2009-01-17 17:40:17 +0100 (Sat, 17 Jan 2009) | 5 lines #3997: zipfiles generated with more than 65536 files could not be opened with other applications. Reviewed by Martin, will backport to 2.6 and 3.0 ........ --- Lib/zipfile.py | 20 +++++++++++++------- Misc/NEWS | 3 +++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Lib/zipfile.py b/Lib/zipfile.py index bb8fc53de4d..933c957afd1 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -26,7 +26,7 @@ class LargeZipFile(Exception): error = BadZipfile # The exception raised by this module -ZIP64_LIMIT= (1 << 31) - 1 +ZIP64_LIMIT = (1 << 31) - 1 ZIP_FILECOUNT_LIMIT = 1 << 16 ZIP_MAX_COMMENT = (1 << 16) - 1 @@ -1175,19 +1175,26 @@ class ZipFile: pos2 = self.fp.tell() # Write end-of-zip-archive record + centDirCount = count + centDirSize = pos2 - pos1 centDirOffset = pos1 - if pos1 > ZIP64_LIMIT: + if (centDirCount >= ZIP_FILECOUNT_LIMIT or + centDirOffset > ZIP64_LIMIT or + centDirSize > ZIP64_LIMIT): # Need to write the ZIP64 end-of-archive records zip64endrec = struct.pack( structEndArchive64, stringEndArchive64, - 44, 45, 45, 0, 0, count, count, pos2 - pos1, pos1) + 44, 45, 45, 0, 0, centDirCount, centDirCount, + centDirSize, centDirOffset) self.fp.write(zip64endrec) zip64locrec = struct.pack( structEndArchive64Locator, stringEndArchive64Locator, 0, pos2, 1) self.fp.write(zip64locrec) - centDirOffset = 0xFFFFFFFF + centDirCount = min(centDirCount, 0xFFFF) + centDirSize = min(centDirSize, 0xFFFFFFFF) + centDirOffset = min(centDirOffset, 0xFFFFFFFF) # check for valid comment length if len(self.comment) >= ZIP_MAX_COMMENT: @@ -1197,9 +1204,8 @@ class ZipFile: self.comment = self.comment[:ZIP_MAX_COMMENT] endrec = struct.pack(structEndArchive, stringEndArchive, - 0, 0, count % ZIP_FILECOUNT_LIMIT, - count % ZIP_FILECOUNT_LIMIT, pos2 - pos1, - centDirOffset, len(self.comment)) + 0, 0, centDirCount, centDirCount, + centDirSize, centDirOffset, len(self.comment)) self.fp.write(endrec) self.fp.write(self.comment) self.fp.flush() diff --git a/Misc/NEWS b/Misc/NEWS index 416dd49e3b5..ca72fcfadb9 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -73,6 +73,9 @@ Core and Builtins Library ------- +- Issue #3997: zipfiles generated with more than 65536 files could not be + opened with other applications. + - Issue 4816: itertools.combinations() and itertools.product were raising a ValueError for values of *r* larger than the input iterable. They now correctly return an empty iterator.