1 # Tests of the full ZIP64 functionality of zipfile
2 # The support.requires call is the only reason for keeping this separate
4 from test
import support
6 # XXX(nnorwitz): disable this test by looking for extra largfile resource
7 # which doesn't exist. This test takes over 30 minutes to run in general
8 # and requires more disk space than most of the buildbots.
11 'test requires loads of disk-space bytes and a long time to run'
14 # We can test part of the module without zlib.
20 import zipfile
, os
, unittest
24 from io
import StringIO
25 from tempfile
import TemporaryFile
27 from test
.support
import TESTFN
, run_unittest
29 TESTFN2
= TESTFN
+ "2"
31 # How much time in seconds can pass before we print a 'Still working' message.
32 _PRINT_WORKING_MSG_INTERVAL
= 5 * 60
34 class TestsWithSourceFile(unittest
.TestCase
):
37 line_gen
= ("Test of zipfile line %d." % i
for i
in range(1000000))
38 self
.data
= '\n'.join(line_gen
).encode('ascii')
40 # And write it to a file.
41 fp
= open(TESTFN
, "wb")
45 def zipTest(self
, f
, compression
):
46 # Create the ZIP archive.
47 zipfp
= zipfile
.ZipFile(f
, "w", compression
, allowZip64
=True)
49 # It will contain enough copies of self.data to reach about 6GB of
51 filecount
= 6*1024**3 // len(self
.data
)
53 next_time
= time
.time() + _PRINT_WORKING_MSG_INTERVAL
54 for num
in range(filecount
):
55 zipfp
.writestr("testfn%d" % num
, self
.data
)
56 # Print still working message since this test can be really slow
57 if next_time
<= time
.time():
58 next_time
= time
.time() + _PRINT_WORKING_MSG_INTERVAL
60 ' zipTest still writing %d of %d, be patient...' %
61 (num
, filecount
)), file=sys
.__stdout
__)
62 sys
.__stdout
__.flush()
65 # Read the ZIP archive
66 zipfp
= zipfile
.ZipFile(f
, "r", compression
)
67 for num
in range(filecount
):
68 self
.assertEqual(zipfp
.read("testfn%d" % num
), self
.data
)
69 # Print still working message since this test can be really slow
70 if next_time
<= time
.time():
71 next_time
= time
.time() + _PRINT_WORKING_MSG_INTERVAL
73 ' zipTest still reading %d of %d, be patient...' %
74 (num
, filecount
)), file=sys
.__stdout
__)
75 sys
.__stdout
__.flush()
79 # Try the temp file first. If we do TESTFN2 first, then it hogs
80 # gigabytes of disk space for the duration of the test.
81 for f
in TemporaryFile(), TESTFN2
:
82 self
.zipTest(f
, zipfile
.ZIP_STORED
)
85 def testDeflated(self
):
86 # Try the temp file first. If we do TESTFN2 first, then it hogs
87 # gigabytes of disk space for the duration of the test.
88 for f
in TemporaryFile(), TESTFN2
:
89 self
.zipTest(f
, zipfile
.ZIP_DEFLATED
)
92 for fname
in TESTFN
, TESTFN2
:
93 if os
.path
.exists(fname
):
97 class OtherTests(unittest
.TestCase
):
98 def testMoreThan64kFiles(self
):
99 # This test checks that more than 64k files can be added to an archive,
100 # and that the resulting archive can be read properly by ZipFile
101 zipf
= zipfile
.ZipFile(TESTFN
, mode
="w")
103 numfiles
= (1 << 16) * 3//2
104 for i
in range(numfiles
):
105 zipf
.writestr("foo%08d" % i
, "%d" % (i
**3 % 57))
106 self
.assertEqual(len(zipf
.namelist()), numfiles
)
109 zipf2
= zipfile
.ZipFile(TESTFN
, mode
="r")
110 self
.assertEqual(len(zipf2
.namelist()), numfiles
)
111 for i
in range(numfiles
):
112 content
= zipf2
.read("foo%08d" % i
).decode('ascii')
113 self
.assertEqual(content
, "%d" % (i
**3 % 57))
117 support
.unlink(TESTFN
)
118 support
.unlink(TESTFN2
)
121 run_unittest(TestsWithSourceFile
, OtherTests
)
123 if __name__
== "__main__":