blob: b842cf29d9493f82440409ccb1ff3b1a5cf2b05d [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(
Tim Peters84b0f582006-06-15 18:04:40 +00006 'largefile',
Ronald Oussoren143cefb2006-06-15 08:14:18 +00007 '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):
Tim Peters84b0f582006-06-15 18:04:40 +000032 # Create test data.
33 # xrange() is important here -- don't want to create immortal space
34 # for a million ints.
35 line_gen = ("Test of zipfile line %d." % i for i in xrange(1000000))
Ronald Oussoren143cefb2006-06-15 08:14:18 +000036 self.data = '\n'.join(line_gen)
37
Tim Peters84b0f582006-06-15 18:04:40 +000038 # And write it to a file.
Ronald Oussoren143cefb2006-06-15 08:14:18 +000039 fp = open(TESTFN, "wb")
40 fp.write(self.data)
41 fp.close()
42
43 def zipTest(self, f, compression):
Tim Peters84b0f582006-06-15 18:04:40 +000044 # Create the ZIP archive.
Ronald Oussoren143cefb2006-06-15 08:14:18 +000045 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
46
Tim Peters84b0f582006-06-15 18:04:40 +000047 # It will contain enough copies of self.data to reach about 6GB of
48 # raw data to store.
49 filecount = 6*1024**2 // len(self.data)
50
Neal Norwitz643ad192006-06-15 09:57:03 +000051 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Ronald Oussoren143cefb2006-06-15 08:14:18 +000052 for num in range(filecount):
Tim Peters84b0f582006-06-15 18:04:40 +000053 zipfp.writestr("testfn%d" % num, self.data)
Neal Norwitz643ad192006-06-15 09:57:03 +000054 # Print still working message since this test can be really slow
55 if next_time <= time.time():
56 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Neal Norwitzbda14182006-06-15 10:24:49 +000057 print >>sys.__stdout__, (
58 ' zipTest still writing %d of %d, be patient...' %
59 (num, filecount))
Neal Norwitz643ad192006-06-15 09:57:03 +000060 sys.__stdout__.flush()
Ronald Oussoren143cefb2006-06-15 08:14:18 +000061 zipfp.close()
62
63 # Read the ZIP archive
64 zipfp = zipfile.ZipFile(f, "r", compression)
65 for num in range(filecount):
Tim Peters84b0f582006-06-15 18:04:40 +000066 self.assertEqual(zipfp.read("testfn%d" % num), self.data)
Neal Norwitz643ad192006-06-15 09:57:03 +000067 # Print still working message since this test can be really slow
68 if next_time <= time.time():
69 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
Neal Norwitzbda14182006-06-15 10:24:49 +000070 print >>sys.__stdout__, (
71 ' zipTest still reading %d of %d, be patient...' %
72 (num, filecount))
Neal Norwitz643ad192006-06-15 09:57:03 +000073 sys.__stdout__.flush()
Ronald Oussoren143cefb2006-06-15 08:14:18 +000074 zipfp.close()
75
76 def testStored(self):
Tim Peters84b0f582006-06-15 18:04:40 +000077 # Try the temp file first. If we do TESTFN2 first, then it hogs
78 # gigabytes of disk space for the duration of the test.
79 for f in TemporaryFile(), TESTFN2:
Ronald Oussoren143cefb2006-06-15 08:14:18 +000080 self.zipTest(f, zipfile.ZIP_STORED)
81
82 if zlib:
83 def testDeflated(self):
Tim Peters84b0f582006-06-15 18:04:40 +000084 # Try the temp file first. If we do TESTFN2 first, then it hogs
85 # gigabytes of disk space for the duration of the test.
86 for f in TemporaryFile(), TESTFN2:
Ronald Oussoren143cefb2006-06-15 08:14:18 +000087 self.zipTest(f, zipfile.ZIP_DEFLATED)
88
89 def tearDown(self):
Tim Peters84b0f582006-06-15 18:04:40 +000090 for fname in TESTFN, TESTFN2:
91 if os.path.exists(fname):
92 os.remove(fname)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000093
94def test_main():
95 run_unittest(TestsWithSourceFile)
96
97if __name__ == "__main__":
98 test_main()