blob: b3f5763fbdc19053bdb9e558298483f6017ffe0d [file] [log] [blame]
Ronald Oussoren143cefb2006-06-15 08:14:18 +00001# 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
4from test import test_support
5test_support.requires(
6 'largefile',
7 'test requires loads of disk-space bytes and a long time to run'
8 )
9
10# We can test part of the module without zlib.
11try:
12 import zlib
13except ImportError:
14 zlib = None
15
16import zipfile, os, unittest
Neal Norwitz643ad192006-06-15 09:57:03 +000017import time
18import sys
Ronald Oussoren143cefb2006-06-15 08:14:18 +000019
20from StringIO import StringIO
21from tempfile import TemporaryFile
22
23from test.test_support import TESTFN, run_unittest
24
25TESTFN2 = TESTFN + "2"
26
Neal Norwitz643ad192006-06-15 09:57:03 +000027# How much time in seconds can pass before we print a 'Still working' message.
28_PRINT_WORKING_MSG_INTERVAL = 5 * 60
29
Ronald Oussoren143cefb2006-06-15 08:14:18 +000030class TestsWithSourceFile(unittest.TestCase):
31 def setUp(self):
32 line_gen = ("Test of zipfile line %d." % i for i in range(0, 1000000))
33 self.data = '\n'.join(line_gen)
34
35 # Make a source file with some lines
36 fp = open(TESTFN, "wb")
37 fp.write(self.data)
38 fp.close()
39
40 def zipTest(self, f, compression):
41 # Create the ZIP archive
42 filecount = int(((1 << 32) / len(self.data)) * 1.5)
43 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
44
Neal Norwitz643ad192006-06-15 09:57:03 +000045 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Ronald Oussoren143cefb2006-06-15 08:14:18 +000046 for num in range(filecount):
47 zipfp.writestr("testfn%d"%(num,), self.data)
Neal Norwitz643ad192006-06-15 09:57:03 +000048 # Print still working message since this test can be really slow
49 if next_time <= time.time():
50 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Neal Norwitzbda14182006-06-15 10:24:49 +000051 print >>sys.__stdout__, (
52 ' zipTest still writing %d of %d, be patient...' %
53 (num, filecount))
Neal Norwitz643ad192006-06-15 09:57:03 +000054 sys.__stdout__.flush()
Ronald Oussoren143cefb2006-06-15 08:14:18 +000055 zipfp.close()
56
57 # Read the ZIP archive
58 zipfp = zipfile.ZipFile(f, "r", compression)
59 for num in range(filecount):
60 self.assertEqual(zipfp.read("testfn%d"%(num,)), self.data)
Neal Norwitz643ad192006-06-15 09:57:03 +000061 # Print still working message since this test can be really slow
62 if next_time <= time.time():
63 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Neal Norwitzbda14182006-06-15 10:24:49 +000064 print >>sys.__stdout__, (
65 ' zipTest still reading %d of %d, be patient...' %
66 (num, filecount))
Neal Norwitz643ad192006-06-15 09:57:03 +000067 sys.__stdout__.flush()
Ronald Oussoren143cefb2006-06-15 08:14:18 +000068 zipfp.close()
69
70 def testStored(self):
71 for f in (TESTFN2, TemporaryFile()):
72 self.zipTest(f, zipfile.ZIP_STORED)
73
74 if zlib:
75 def testDeflated(self):
76 for f in (TESTFN2, TemporaryFile()):
77 self.zipTest(f, zipfile.ZIP_DEFLATED)
78
79 def tearDown(self):
80 os.remove(TESTFN)
81 os.remove(TESTFN2)
82
83def test_main():
84 run_unittest(TestsWithSourceFile)
85
86if __name__ == "__main__":
87 test_main()