@@ -605,7 +605,9 @@ class TestZip64InSmallFiles(unittest.TestCase):
605605
606606 def setUp (self ):
607607 self ._limit = zipfile .ZIP64_LIMIT
608- zipfile .ZIP64_LIMIT = 5
608+ self ._filecount_limit = zipfile .ZIP_FILECOUNT_LIMIT
609+ zipfile .ZIP64_LIMIT = 1000
610+ zipfile .ZIP_FILECOUNT_LIMIT = 9
609611
610612 line_gen = ("Test of zipfile line %d." % i
611613 for i in range (0 , FIXEDTEST_SIZE ))
@@ -709,8 +711,63 @@ def test_absolute_arcnames(self):
709711 with zipfile .ZipFile (TESTFN2 , "r" , zipfile .ZIP_STORED ) as zipfp :
710712 self .assertEqual (zipfp .namelist (), ["absolute" ])
711713
714+ def test_too_many_files (self ):
715+ # This test checks that more than 64k files can be added to an archive,
716+ # and that the resulting archive can be read properly by ZipFile
717+ zipf = zipfile .ZipFile (TESTFN , mode = "w" , allowZip64 = True )
718+ zipf .debug = 100
719+ numfiles = 15
720+ for i in range (numfiles ):
721+ zipf .writestr ("foo%08d" % i , "%d" % (i ** 3 % 57 ))
722+ self .assertEqual (len (zipf .namelist ()), numfiles )
723+ zipf .close ()
724+
725+ zipf2 = zipfile .ZipFile (TESTFN , mode = "r" )
726+ self .assertEqual (len (zipf2 .namelist ()), numfiles )
727+ for i in range (numfiles ):
728+ content = zipf2 .read ("foo%08d" % i )
729+ self .assertEqual (content , "%d" % (i ** 3 % 57 ))
730+ zipf2 .close ()
731+
732+ def test_too_many_files_append (self ):
733+ zipf = zipfile .ZipFile (TESTFN , mode = "w" , allowZip64 = False )
734+ zipf .debug = 100
735+ numfiles = 9
736+ for i in range (numfiles ):
737+ zipf .writestr ("foo%08d" % i , "%d" % (i ** 3 % 57 ))
738+ self .assertEqual (len (zipf .namelist ()), numfiles )
739+ with self .assertRaises (zipfile .LargeZipFile ):
740+ zipf .writestr ("foo%08d" % numfiles , b'' )
741+ self .assertEqual (len (zipf .namelist ()), numfiles )
742+ zipf .close ()
743+
744+ zipf = zipfile .ZipFile (TESTFN , mode = "a" , allowZip64 = False )
745+ zipf .debug = 100
746+ self .assertEqual (len (zipf .namelist ()), numfiles )
747+ with self .assertRaises (zipfile .LargeZipFile ):
748+ zipf .writestr ("foo%08d" % numfiles , b'' )
749+ self .assertEqual (len (zipf .namelist ()), numfiles )
750+ zipf .close ()
751+
752+ zipf = zipfile .ZipFile (TESTFN , mode = "a" , allowZip64 = True )
753+ zipf .debug = 100
754+ self .assertEqual (len (zipf .namelist ()), numfiles )
755+ numfiles2 = 15
756+ for i in range (numfiles , numfiles2 ):
757+ zipf .writestr ("foo%08d" % i , "%d" % (i ** 3 % 57 ))
758+ self .assertEqual (len (zipf .namelist ()), numfiles2 )
759+ zipf .close ()
760+
761+ zipf2 = zipfile .ZipFile (TESTFN , mode = "r" )
762+ self .assertEqual (len (zipf2 .namelist ()), numfiles2 )
763+ for i in range (numfiles2 ):
764+ content = zipf2 .read ("foo%08d" % i )
765+ self .assertEqual (content , "%d" % (i ** 3 % 57 ))
766+ zipf2 .close ()
767+
712768 def tearDown (self ):
713769 zipfile .ZIP64_LIMIT = self ._limit
770+ zipfile .ZIP_FILECOUNT_LIMIT = self ._filecount_limit
714771 unlink (TESTFN )
715772 unlink (TESTFN2 )
716773
0 commit comments