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 |
| 17 | |
| 18 | from StringIO import StringIO |
| 19 | from tempfile import TemporaryFile |
| 20 | |
| 21 | from test.test_support import TESTFN, run_unittest |
| 22 | |
| 23 | TESTFN2 = TESTFN + "2" |
| 24 | |
| 25 | class TestsWithSourceFile(unittest.TestCase): |
| 26 | def setUp(self): |
| 27 | line_gen = ("Test of zipfile line %d." % i for i in range(0, 1000000)) |
| 28 | self.data = '\n'.join(line_gen) |
| 29 | |
| 30 | # Make a source file with some lines |
| 31 | fp = open(TESTFN, "wb") |
| 32 | fp.write(self.data) |
| 33 | fp.close() |
| 34 | |
| 35 | def zipTest(self, f, compression): |
| 36 | # Create the ZIP archive |
| 37 | filecount = int(((1 << 32) / len(self.data)) * 1.5) |
| 38 | zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True) |
| 39 | |
| 40 | for num in range(filecount): |
| 41 | zipfp.writestr("testfn%d"%(num,), self.data) |
| 42 | zipfp.close() |
| 43 | |
| 44 | # Read the ZIP archive |
| 45 | zipfp = zipfile.ZipFile(f, "r", compression) |
| 46 | for num in range(filecount): |
| 47 | self.assertEqual(zipfp.read("testfn%d"%(num,)), self.data) |
| 48 | zipfp.close() |
| 49 | |
| 50 | def testStored(self): |
| 51 | for f in (TESTFN2, TemporaryFile()): |
| 52 | self.zipTest(f, zipfile.ZIP_STORED) |
| 53 | |
| 54 | if zlib: |
| 55 | def testDeflated(self): |
| 56 | for f in (TESTFN2, TemporaryFile()): |
| 57 | self.zipTest(f, zipfile.ZIP_DEFLATED) |
| 58 | |
| 59 | def tearDown(self): |
| 60 | os.remove(TESTFN) |
| 61 | os.remove(TESTFN2) |
| 62 | |
| 63 | def test_main(): |
| 64 | run_unittest(TestsWithSourceFile) |
| 65 | |
| 66 | if __name__ == "__main__": |
| 67 | test_main() |