Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 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 |
| 5 | test_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. |
| 11 | try: |
| 12 | import zlib |
| 13 | except ImportError: |
| 14 | zlib = None |
| 15 | |
| 16 | import zipfile, os, unittest |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 17 | import time |
| 18 | import sys |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 19 | |
| 20 | from StringIO import StringIO |
| 21 | from tempfile import TemporaryFile |
| 22 | |
| 23 | from test.test_support import TESTFN, run_unittest |
| 24 | |
| 25 | TESTFN2 = TESTFN + "2" |
| 26 | |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 27 | # How much time in seconds can pass before we print a 'Still working' message. |
| 28 | _PRINT_WORKING_MSG_INTERVAL = 5 * 60 |
| 29 | |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 30 | class 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 Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 45 | next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 46 | for num in range(filecount): |
| 47 | zipfp.writestr("testfn%d"%(num,), self.data) |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 48 | # 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 Norwitz | bda1418 | 2006-06-15 10:24:49 +0000 | [diff] [blame^] | 51 | print >>sys.__stdout__, ( |
| 52 | ' zipTest still writing %d of %d, be patient...' % |
| 53 | (num, filecount)) |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 54 | sys.__stdout__.flush() |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 55 | 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 Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 61 | # 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 Norwitz | bda1418 | 2006-06-15 10:24:49 +0000 | [diff] [blame^] | 64 | print >>sys.__stdout__, ( |
| 65 | ' zipTest still reading %d of %d, be patient...' % |
| 66 | (num, filecount)) |
Neal Norwitz | 643ad19 | 2006-06-15 09:57:03 +0000 | [diff] [blame] | 67 | sys.__stdout__.flush() |
Ronald Oussoren | 143cefb | 2006-06-15 08:14:18 +0000 | [diff] [blame] | 68 | 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 | |
| 83 | def test_main(): |
| 84 | run_unittest(TestsWithSourceFile) |
| 85 | |
| 86 | if __name__ == "__main__": |
| 87 | test_main() |