move sections
[python/dscho.git] / Lib / test / test_zipfile64.py
bloba6f3dca4b9ce99f2d0bd1d13b1a858cb93fe6ec1
1 # Tests of the full ZIP64 functionality of zipfile
2 # The test_support.requires call is the only reason for keeping this separate
3 # from test_zipfile
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.
9 test_support.requires(
10 'extralargefile',
11 'test requires loads of disk-space bytes and a long time to run'
14 # We can test part of the module without zlib.
15 try:
16 import zlib
17 except ImportError:
18 zlib = None
20 import zipfile, os, unittest
21 import time
22 import sys
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):
34 def setUp(self):
35 # Create test data.
36 # xrange() is important here -- don't want to create immortal space
37 # for a million ints.
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")
43 fp.write(self.data)
44 fp.close()
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
51 # raw data to store.
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...' %
62 (num, filecount))
63 sys.__stdout__.flush()
64 zipfp.close()
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...' %
75 (num, filecount))
76 sys.__stdout__.flush()
77 zipfp.close()
79 def testStored(self):
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)
85 if zlib:
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)
92 def tearDown(self):
93 for fname in TESTFN, TESTFN2:
94 if os.path.exists(fname):
95 os.remove(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")
103 zipf.debug = 100
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)
108 zipf.close()
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))
114 zipf.close()
116 def tearDown(self):
117 test_support.unlink(TESTFN)
118 test_support.unlink(TESTFN2)
120 def test_main():
121 run_unittest(TestsWithSourceFile, OtherTests)
123 if __name__ == "__main__":
124 test_main()