tests of the full zip64 functionality of zipfile the support requires call is the only reason for keeping this separate from testzipfile xxxnnorwitz disable this test by looking for extralargefile resource which doesn t exist this test takes over 30 minutes to run in general and requires more disk space than most of the buildbots how much time in seconds can pass before we print a still working message create test data create the zip archive it will contain enough copies of self data to reach about 6 gib of raw data to store print still working message since this test can be really slow read the zip archive print still working message since this test can be really slow check that testzip thinks the archive is valid try the temp file first if we do testfn2 first then it hogs gigabytes of disk space for the duration of the test try the temp file first if we do testfn2 first then it hogs gigabytes of disk space for the duration of the test this test checks that more than 64k files can be added to an archive and that the resulting archive can be read properly by zipfile tests of the full zip64 functionality of zipfile the support requires call is the only reason for keeping this separate from test_zipfile xxx nnorwitz disable this test by looking for extralargefile resource which doesn t exist this test takes over 30 minutes to run in general and requires more disk space than most of the buildbots how much time in seconds can pass before we print a still working message create test data create the zip archive it will contain enough copies of self data to reach about 6 gib of raw data to store print still working message since this test can be really slow read the zip archive print still working message since this test can be really slow check that testzip thinks the archive is valid try the temp file first if we do testfn2 first then it hogs gigabytes of disk space for the duration of the test try the temp file first if we do testfn2 first then it hogs gigabytes of disk space for the duration of the test this test checks that more than 64k files can be added to an archive and that the resulting archive can be read properly by zipfile
from test import support




support.requires(
        'extralargefile',
        'test requires loads of disk-space bytes and a long time to run'
    )

import zipfile, unittest
import time
import sys

from tempfile import TemporaryFile

from test.support import os_helper
from test.support import requires_zlib

TESTFN = os_helper.TESTFN
TESTFN2 = TESTFN + "2"


_PRINT_WORKING_MSG_INTERVAL = 60

class TestsWithSourceFile(unittest.TestCase):
    def setUp(self):
        
        line_gen = ("Test of zipfile line %d." % i for i in range(1000000))
        self.data = '\n'.join(line_gen).encode('ascii')

    def zipTest(self, f, compression):
        
        with zipfile.ZipFile(f, "w", compression) as zipfp:

            
            
            filecount = 6*1024**3 // len(self.data)

            next_time = time.monotonic() + _PRINT_WORKING_MSG_INTERVAL
            for num in range(filecount):
                zipfp.writestr("testfn%d" % num, self.data)
                
                if next_time <= time.monotonic():
                    next_time = time.monotonic() + _PRINT_WORKING_MSG_INTERVAL
                    print((
                    '  zipTest still writing %d of %d, be patient...' %
                    (num, filecount)), file=sys.__stdout__)
                    sys.__stdout__.flush()

        
        with zipfile.ZipFile(f, "r", compression) as zipfp:
            for num in range(filecount):
                self.assertEqual(zipfp.read("testfn%d" % num), self.data)
                
                if next_time <= time.monotonic():
                    next_time = time.monotonic() + _PRINT_WORKING_MSG_INTERVAL
                    print((
                    '  zipTest still reading %d of %d, be patient...' %
                    (num, filecount)), file=sys.__stdout__)
                    sys.__stdout__.flush()

            
            self.assertIsNone(zipfp.testzip())

    def testStored(self):
        
        
        with TemporaryFile() as f:
            self.zipTest(f, zipfile.ZIP_STORED)
            self.assertFalse(f.closed)
        self.zipTest(TESTFN2, zipfile.ZIP_STORED)

    @requires_zlib()
    def testDeflated(self):
        
        
        with TemporaryFile() as f:
            self.zipTest(f, zipfile.ZIP_DEFLATED)
            self.assertFalse(f.closed)
        self.zipTest(TESTFN2, zipfile.ZIP_DEFLATED)

    def tearDown(self):
        os_helper.unlink(TESTFN2)


class OtherTests(unittest.TestCase):
    def testMoreThan64kFiles(self):
        
        
        with zipfile.ZipFile(TESTFN, mode="w", allowZip64=True) as zipf:
            zipf.debug = 100
            numfiles = (1 << 16) * 3//2
            for i in range(numfiles):
                zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
            self.assertEqual(len(zipf.namelist()), numfiles)

        with zipfile.ZipFile(TESTFN, mode="r") as zipf2:
            self.assertEqual(len(zipf2.namelist()), numfiles)
            for i in range(numfiles):
                content = zipf2.read("foo%08d" % i).decode('ascii')
                self.assertEqual(content, "%d" % (i**3 % 57))

    def testMoreThan64kFilesAppend(self):
        with zipfile.ZipFile(TESTFN, mode="w", allowZip64=False) as zipf:
            zipf.debug = 100
            numfiles = (1 << 16) - 1
            for i in range(numfiles):
                zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
            self.assertEqual(len(zipf.namelist()), numfiles)
            with self.assertRaises(zipfile.LargeZipFile):
                zipf.writestr("foo%08d" % numfiles, b'')
            self.assertEqual(len(zipf.namelist()), numfiles)

        with zipfile.ZipFile(TESTFN, mode="a", allowZip64=False) as zipf:
            zipf.debug = 100
            self.assertEqual(len(zipf.namelist()), numfiles)
            with self.assertRaises(zipfile.LargeZipFile):
                zipf.writestr("foo%08d" % numfiles, b'')
            self.assertEqual(len(zipf.namelist()), numfiles)

        with zipfile.ZipFile(TESTFN, mode="a", allowZip64=True) as zipf:
            zipf.debug = 100
            self.assertEqual(len(zipf.namelist()), numfiles)
            numfiles2 = (1 << 16) * 3//2
            for i in range(numfiles, numfiles2):
                zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
            self.assertEqual(len(zipf.namelist()), numfiles2)

        with zipfile.ZipFile(TESTFN, mode="r") as zipf2:
            self.assertEqual(len(zipf2.namelist()), numfiles2)
            for i in range(numfiles2):
                content = zipf2.read("foo%08d" % i).decode('ascii')
                self.assertEqual(content, "%d" % (i**3 % 57))

    def tearDown(self):
        os_helper.unlink(TESTFN)
        os_helper.unlink(TESTFN2)

if __name__ == "__main__":
    unittest.main()