1 # Tests of the full ZIP64 functionality of zipfile
2 # The test_support.requires call is the only reason for keeping this separate
4 from test
import test_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 tempfile
import TemporaryFile
26 from test
.test_support
import TESTFN
, run_unittest
28 TESTFN2
= TESTFN
+ "2"
30 # How much time in seconds can pass before we print a 'Still working' message.
31 _PRINT_WORKING_MSG_INTERVAL
= 5 * 60
33 class TestsWithSourceFile(unittest
.TestCase
):
36 # xrange() is important here -- don't want to create immortal space
38 line_gen
= ("Test of zipfile line %d." % i
for i
in xrange(1000000))
39 self
.data
= '\n'.join(line_gen
)
41 # And write it to a file.
42 fp
= open(TESTFN
, "wb")
46 def zipTest(self
, f
, compression
):
47 # Create the ZIP archive.
48 zipfp
= zipfile
.ZipFile(f
, "w", compression
, allowZip64
=True)
50 # It will contain enough copies of self.data to reach about 6GB of
52 filecount
= 6*1024**3 // len(self
.data
)
54 next_time
= time
.time() + _PRINT_WORKING_MSG_INTERVAL
55 for num
in range(filecount
):
56 zipfp
.writestr("testfn%d" % num
, self
.data
)
57 # Print still working message since this test can be really slow
58 if next_time
<= time
.time():
59 next_time
= time
.time() + _PRINT_WORKING_MSG_INTERVAL
60 print >>sys
.__stdout
__, (
61 ' zipTest still writing %d of %d, be patient...' %
63 sys
.__stdout
__.flush()
66 # Read the ZIP archive
67 zipfp
= zipfile
.ZipFile(f
, "r", compression
)
68 for num
in range(filecount
):
69 self
.assertEqual(zipfp
.read("testfn%d" % num
), self
.data
)
70 # Print still working message since this test can be really slow
71 if next_time
<= time
.time():
72 next_time
= time
.time() + _PRINT_WORKING_MSG_INTERVAL
73 print >>sys
.__stdout
__, (
74 ' zipTest still reading %d of %d, be patient...' %
76 sys
.__stdout
__.flush()
80 # Try the temp file first. If we do TESTFN2 first, then it hogs
81 # gigabytes of disk space for the duration of the test.
82 for f
in TemporaryFile(), TESTFN2
:
83 self
.zipTest(f
, zipfile
.ZIP_STORED
)
86 def testDeflated(self
):
87 # Try the temp file first. If we do TESTFN2 first, then it hogs
88 # gigabytes of disk space for the duration of the test.
89 for f
in TemporaryFile(), TESTFN2
:
90 self
.zipTest(f
, zipfile
.ZIP_DEFLATED
)
93 for fname
in TESTFN
, TESTFN2
:
94 if os
.path
.exists(fname
):
98 class OtherTests(unittest
.TestCase
):
99 def testMoreThan64kFiles(self
):
100 # This test checks that more than 64k files can be added to an archive,
101 # and that the resulting archive can be read properly by ZipFile
102 zipf
= zipfile
.ZipFile(TESTFN
, mode
="w")
104 numfiles
= (1 << 16) * 3/2
105 for i
in xrange(numfiles
):
106 zipf
.writestr("foo%08d" % i
, "%d" % (i
**3 % 57))
107 self
.assertEqual(len(zipf
.namelist()), numfiles
)
110 zipf2
= zipfile
.ZipFile(TESTFN
, mode
="r")
111 self
.assertEqual(len(zipf2
.namelist()), numfiles
)
112 for i
in xrange(numfiles
):
113 self
.assertEqual(zipf2
.read("foo%08d" % i
), "%d" % (i
**3 % 57))
117 test_support
.unlink(TESTFN
)
118 test_support
.unlink(TESTFN2
)
121 run_unittest(TestsWithSourceFile
, OtherTests
)
123 if __name__
== "__main__":