blob: 7e134bdcdeafc5a6d7dace2c71149f42eeda4677 [file] [log] [blame]
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001# We can test part of the module without zlib.
Guido van Rossum368f04a2000-04-10 13:23:04 +00002try:
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00003 import zlib
4except ImportError:
5 zlib = None
Ezio Melotti74c96ec2009-07-08 22:24:06 +00006
7import io
8import os
9import shutil
10import struct
11import zipfile
12import unittest
13
Tim Petersa45cacf2004-08-20 03:47:14 +000014
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000015from tempfile import TemporaryFile
Guido van Rossumd8faa362007-04-27 19:54:29 +000016from random import randint, random
Ezio Melotti74c96ec2009-07-08 22:24:06 +000017from unittest import skipUnless
Tim Petersa19a1682001-03-29 04:36:09 +000018
Ezio Melotti76430242009-07-11 18:28:48 +000019from test.support import TESTFN, run_unittest, findfile, unlink
Guido van Rossum368f04a2000-04-10 13:23:04 +000020
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000021TESTFN2 = TESTFN + "2"
Martin v. Löwis59e47792009-01-24 14:10:07 +000022TESTFNDIR = TESTFN + "d"
Guido van Rossumb5a755e2007-07-18 18:15:48 +000023FIXEDTEST_SIZE = 1000
Guido van Rossum368f04a2000-04-10 13:23:04 +000024
Christian Heimes790c8232008-01-07 21:14:23 +000025SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
26 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
27 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
28 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
29
Ezio Melotti76430242009-07-11 18:28:48 +000030
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000031class TestsWithSourceFile(unittest.TestCase):
32 def setUp(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +000033 self.line_gen = (bytes("Zipfile test line %d. random float: %f" %
Guido van Rossum9c627722007-08-27 18:31:48 +000034 (i, random()), "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +000035 for i in range(FIXEDTEST_SIZE))
36 self.data = b'\n'.join(self.line_gen) + b'\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000037
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000038 # Make a source file with some lines
39 fp = open(TESTFN, "wb")
40 fp.write(self.data)
41 fp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000042
Ezio Melottiafd0d112009-07-15 17:17:17 +000043 def make_test_archive(self, f, compression):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000044 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000045 with zipfile.ZipFile(f, "w", compression) as zipfp:
46 zipfp.write(TESTFN, "another.name")
47 zipfp.write(TESTFN, TESTFN)
48 zipfp.writestr("strfile", self.data)
Tim Peters7d3bad62001-04-04 18:56:49 +000049
Ezio Melottiafd0d112009-07-15 17:17:17 +000050 def zip_test(self, f, compression):
51 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +000052
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000053 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000054 with zipfile.ZipFile(f, "r", compression) as zipfp:
55 self.assertEqual(zipfp.read(TESTFN), self.data)
56 self.assertEqual(zipfp.read("another.name"), self.data)
57 self.assertEqual(zipfp.read("strfile"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000059 # Print the ZIP directory
60 fp = io.StringIO()
61 zipfp.printdir(file=fp)
62 directory = fp.getvalue()
63 lines = directory.splitlines()
64 self.assertEquals(len(lines), 4) # Number of files + header
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000066 self.assertTrue('File Name' in lines[0])
67 self.assertTrue('Modified' in lines[0])
68 self.assertTrue('Size' in lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000070 fn, date, time, size = lines[1].split()
71 self.assertEquals(fn, 'another.name')
72 # XXX: timestamp is not tested
73 self.assertEquals(size, str(len(self.data)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000075 # Check the namelist
76 names = zipfp.namelist()
77 self.assertEquals(len(names), 3)
78 self.assertTrue(TESTFN in names)
79 self.assertTrue("another.name" in names)
80 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000081
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000082 # Check infolist
83 infos = zipfp.infolist()
84 names = [ i.filename for i in infos ]
85 self.assertEquals(len(names), 3)
86 self.assertTrue(TESTFN in names)
87 self.assertTrue("another.name" in names)
88 self.assertTrue("strfile" in names)
89 for i in infos:
90 self.assertEquals(i.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000091
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000092 # check getinfo
93 for nm in (TESTFN, "another.name", "strfile"):
94 info = zipfp.getinfo(nm)
95 self.assertEquals(info.filename, nm)
96 self.assertEquals(info.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000098 # Check that testzip doesn't raise an exception
99 zipfp.testzip()
Tim Peters7d3bad62001-04-04 18:56:49 +0000100
Ezio Melottiafd0d112009-07-15 17:17:17 +0000101 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000102 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000103 self.zip_test(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000104
Ezio Melottiafd0d112009-07-15 17:17:17 +0000105 def zip_open_test(self, f, compression):
106 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000107
108 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000109 with zipfile.ZipFile(f, "r", compression) as zipfp:
110 zipdata1 = []
111 zipopen1 = zipfp.open(TESTFN)
112 while True:
113 read_data = zipopen1.read(256)
114 if not read_data:
115 break
116 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000117
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000118 zipdata2 = []
119 zipopen2 = zipfp.open("another.name")
120 while True:
121 read_data = zipopen2.read(256)
122 if not read_data:
123 break
124 zipdata2.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000125
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000126 self.assertEqual(b''.join(zipdata1), self.data)
127 self.assertEqual(b''.join(zipdata2), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000128
Ezio Melottiafd0d112009-07-15 17:17:17 +0000129 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000130 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000131 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000132
Ezio Melottiafd0d112009-07-15 17:17:17 +0000133 def test_open_via_zip_info(self):
Georg Brandlb533e262008-05-25 18:19:30 +0000134 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000135 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
136 zipfp.writestr("name", "foo")
137 zipfp.writestr("name", "bar")
Georg Brandlb533e262008-05-25 18:19:30 +0000138
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000139 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
140 infos = zipfp.infolist()
141 data = b""
142 for info in infos:
143 data += zipfp.open(info).read()
144 self.assertTrue(data == b"foobar" or data == b"barfoo")
145 data = b""
146 for info in infos:
147 data += zipfp.read(info)
148 self.assertTrue(data == b"foobar" or data == b"barfoo")
Georg Brandlb533e262008-05-25 18:19:30 +0000149
Ezio Melottiafd0d112009-07-15 17:17:17 +0000150 def zip_random_open_test(self, f, compression):
151 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000152
153 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000154 with zipfile.ZipFile(f, "r", compression) as zipfp:
155 zipdata1 = []
156 zipopen1 = zipfp.open(TESTFN)
157 while True:
158 read_data = zipopen1.read(randint(1, 1024))
159 if not read_data:
160 break
161 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000162
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000163 self.assertEqual(b''.join(zipdata1), self.data)
164
Guido van Rossumd8faa362007-04-27 19:54:29 +0000165
Ezio Melottiafd0d112009-07-15 17:17:17 +0000166 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000167 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000168 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000169
Ezio Melottiafd0d112009-07-15 17:17:17 +0000170 def zip_readline_test(self, f, compression):
171 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000172
173 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000174 with zipfile.ZipFile(f, "r") as zipfp:
175 zipopen = zipfp.open(TESTFN)
176 for line in self.line_gen:
177 linedata = zipopen.readline()
178 self.assertEqual(linedata, line + '\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000179
Ezio Melottiafd0d112009-07-15 17:17:17 +0000180 def zip_readlines_test(self, f, compression):
181 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000182
183 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000184 with zipfile.ZipFile(f, "r") as zipfp:
185 ziplines = zipfp.open(TESTFN).readlines()
186 for line, zipline in zip(self.line_gen, ziplines):
187 self.assertEqual(zipline, line + '\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000188
Ezio Melottiafd0d112009-07-15 17:17:17 +0000189 def zip_iterlines_test(self, f, compression):
190 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000191
192 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000193 with zipfile.ZipFile(f, "r") as zipfp:
194 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
195 self.assertEqual(zipline, line + '\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000196
Ezio Melottiafd0d112009-07-15 17:17:17 +0000197 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000198 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000199 self.zip_readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000200
Ezio Melottiafd0d112009-07-15 17:17:17 +0000201 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000202 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000203 self.zip_readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000204
Ezio Melottiafd0d112009-07-15 17:17:17 +0000205 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000206 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000207 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000208
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000209 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000210 def test_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000211 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000212 self.zip_test(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000213
Guido van Rossumd8faa362007-04-27 19:54:29 +0000214
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000215 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000216 def test_open_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000217 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000218 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000219
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000220 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000221 def test_random_open_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000222 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000223 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000224
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000225 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000226 def test_readline_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000227 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000228 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000229
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000230 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000231 def test_readlines_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000232 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000233 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000234
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000235 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000236 def test_iterlines_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000237 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000238 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000239
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000240 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000241 def test_low_compression(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000242 # Checks for cases where compressed data is larger than original
243 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000244 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
245 zipfp.writestr("strfile", '12')
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000246
247 # Get an open object for strfile
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000248 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
249 openobj = zipfp.open("strfile")
250 self.assertEqual(openobj.read(1), b'1')
251 self.assertEqual(openobj.read(1), b'2')
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000252
Ezio Melottiafd0d112009-07-15 17:17:17 +0000253 def test_absolute_arcnames(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000254 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
255 zipfp.write(TESTFN, "/absolute")
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000256
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000257 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
258 self.assertEqual(zipfp.namelist(), ["absolute"])
Tim Peters32cbc962006-02-20 21:42:18 +0000259
Ezio Melottiafd0d112009-07-15 17:17:17 +0000260 def test_append_to_zip_file(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000261 # Test appending to an existing zipfile
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000262 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
263 zipfp.write(TESTFN, TESTFN)
264
265 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
266 zipfp.writestr("strfile", self.data)
267 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000268
Ezio Melottiafd0d112009-07-15 17:17:17 +0000269 def test_append_to_non_zip_file(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000270 # Test appending to an existing file that is not a zipfile
271 # NOTE: this test fails if len(d) < 22 because of the first
272 # line "fpin.seek(-22, 2)" in _EndRecData
Guido van Rossum9c627722007-08-27 18:31:48 +0000273 d = b'I am not a ZipFile!'*10
Guido van Rossum814661e2007-07-18 22:07:29 +0000274 f = open(TESTFN2, 'wb')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000275 f.write(d)
276 f.close()
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000277 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
278 zipfp.write(TESTFN, TESTFN)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000279
Guido van Rossum814661e2007-07-18 22:07:29 +0000280 f = open(TESTFN2, 'rb')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000281 f.seek(len(d))
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000282 with zipfile.ZipFile(f, "r") as zipfp:
283 self.assertEqual(zipfp.namelist(), [TESTFN])
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000284
Ezio Melottiafd0d112009-07-15 17:17:17 +0000285 def test_write_default_name(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000286 # Check that calling ZipFile.write without arcname specified produces the expected result
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000287 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
288 zipfp.write(TESTFN)
289 self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read())
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000290
Ezio Melotti78ea2022009-09-12 18:41:20 +0000291 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000292 def test_per_file_compression(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000293 # Check that files within a Zip archive can have different compression options
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000294 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
295 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
296 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
297 sinfo = zipfp.getinfo('storeme')
298 dinfo = zipfp.getinfo('deflateme')
299 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
300 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000301
Ezio Melottiafd0d112009-07-15 17:17:17 +0000302 def test_write_to_readonly(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000303 # Check that trying to call write() on a readonly ZipFile object
304 # raises a RuntimeError
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000305 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
306 zipfp.writestr("somefile.txt", "bogus")
307
308 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
309 self.assertRaises(RuntimeError, zipfp.write, TESTFN)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000310
Ezio Melottiafd0d112009-07-15 17:17:17 +0000311 def test_extract(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000312 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
313 for fpath, fdata in SMALL_TEST_DATA:
314 zipfp.writestr(fpath, fdata)
Christian Heimes790c8232008-01-07 21:14:23 +0000315
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000316 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
317 for fpath, fdata in SMALL_TEST_DATA:
318 writtenfile = zipfp.extract(fpath)
Christian Heimes790c8232008-01-07 21:14:23 +0000319
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000320 # make sure it was written to the right place
321 if os.path.isabs(fpath):
322 correctfile = os.path.join(os.getcwd(), fpath[1:])
323 else:
324 correctfile = os.path.join(os.getcwd(), fpath)
325 correctfile = os.path.normpath(correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000326
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000327 self.assertEqual(writtenfile, correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000328
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000329 # make sure correct data is in correct file
330 self.assertEqual(fdata.encode(), open(writtenfile, "rb").read())
Christian Heimes790c8232008-01-07 21:14:23 +0000331
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000332 os.remove(writtenfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000333
334 # remove the test file subdirectories
335 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
336
Ezio Melottiafd0d112009-07-15 17:17:17 +0000337 def test_extract_all(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000338 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
339 for fpath, fdata in SMALL_TEST_DATA:
340 zipfp.writestr(fpath, fdata)
Christian Heimes790c8232008-01-07 21:14:23 +0000341
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000342 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
343 zipfp.extractall()
344 for fpath, fdata in SMALL_TEST_DATA:
345 if os.path.isabs(fpath):
346 outfile = os.path.join(os.getcwd(), fpath[1:])
347 else:
348 outfile = os.path.join(os.getcwd(), fpath)
Christian Heimes790c8232008-01-07 21:14:23 +0000349
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000350 self.assertEqual(fdata.encode(), open(outfile, "rb").read())
Christian Heimes790c8232008-01-07 21:14:23 +0000351
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000352 os.remove(outfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000353
354 # remove the test file subdirectories
355 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
356
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000357 def zip_test_writestr_permissions(self, f, compression):
358 # Make sure that writestr creates files with mode 0600,
359 # when it is passed a name rather than a ZipInfo instance.
360
Ezio Melottiafd0d112009-07-15 17:17:17 +0000361 self.make_test_archive(f, compression)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000362 with zipfile.ZipFile(f, "r") as zipfp:
363 zinfo = zipfp.getinfo('strfile')
364 self.assertEqual(zinfo.external_attr, 0o600 << 16)
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000365
Ezio Melottiafd0d112009-07-15 17:17:17 +0000366 def test_writestr_permissions(self):
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000367 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
368 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
369
Gregory P. Smithb0d9ca92009-07-07 05:06:04 +0000370 def test_writestr_extended_local_header_issue1202(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000371 with zipfile.ZipFile(TESTFN2, 'w') as orig_zip:
372 for data in 'abcdefghijklmnop':
373 zinfo = zipfile.ZipInfo(data)
374 zinfo.flag_bits |= 0x08 # Include an extended local header.
375 orig_zip.writestr(zinfo, data)
376
377 def test_close(self):
378 """Check that the zipfile is closed after the 'with' block."""
379 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
380 for fpath, fdata in SMALL_TEST_DATA:
381 zipfp.writestr(fpath, fdata)
382 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
383 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
384
385 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
386 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
387 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
388
389 def test_close_on_exception(self):
390 """Check that the zipfile is closed if an exception is raised in the
391 'with' block."""
392 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
393 for fpath, fdata in SMALL_TEST_DATA:
394 zipfp.writestr(fpath, fdata)
395
396 try:
397 with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
398 raise zipfile.BadZipfile()
399 except zipfile.BadZipfile:
400 self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
Gregory P. Smithb0d9ca92009-07-07 05:06:04 +0000401
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000402 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000403 unlink(TESTFN)
404 unlink(TESTFN2)
405
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000406
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000407class TestZip64InSmallFiles(unittest.TestCase):
408 # These tests test the ZIP64 functionality without using large files,
409 # see test_zipfile64 for proper tests.
410
411 def setUp(self):
412 self._limit = zipfile.ZIP64_LIMIT
413 zipfile.ZIP64_LIMIT = 5
414
Guido van Rossum9c627722007-08-27 18:31:48 +0000415 line_gen = (bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000416 for i in range(0, FIXEDTEST_SIZE))
417 self.data = b'\n'.join(line_gen)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000418
419 # Make a source file with some lines
420 fp = open(TESTFN, "wb")
421 fp.write(self.data)
422 fp.close()
423
Ezio Melottiafd0d112009-07-15 17:17:17 +0000424 def large_file_exception_test(self, f, compression):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000425 with zipfile.ZipFile(f, "w", compression) as zipfp:
426 self.assertRaises(zipfile.LargeZipFile,
427 zipfp.write, TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000428
Ezio Melottiafd0d112009-07-15 17:17:17 +0000429 def large_file_exception_test2(self, f, compression):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000430 with zipfile.ZipFile(f, "w", compression) as zipfp:
431 self.assertRaises(zipfile.LargeZipFile,
432 zipfp.writestr, "another.name", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000433
Ezio Melottiafd0d112009-07-15 17:17:17 +0000434 def test_large_file_exception(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000435 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000436 self.large_file_exception_test(f, zipfile.ZIP_STORED)
437 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000438
Ezio Melottiafd0d112009-07-15 17:17:17 +0000439 def zip_test(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000440 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000441 with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
442 zipfp.write(TESTFN, "another.name")
443 zipfp.write(TESTFN, TESTFN)
444 zipfp.writestr("strfile", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000445
446 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000447 with zipfile.ZipFile(f, "r", compression) as zipfp:
448 self.assertEqual(zipfp.read(TESTFN), self.data)
449 self.assertEqual(zipfp.read("another.name"), self.data)
450 self.assertEqual(zipfp.read("strfile"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000451
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000452 # Print the ZIP directory
453 fp = io.StringIO()
454 zipfp.printdir(fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000455
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000456 directory = fp.getvalue()
457 lines = directory.splitlines()
458 self.assertEquals(len(lines), 4) # Number of files + header
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000459
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000460 self.assertTrue('File Name' in lines[0])
461 self.assertTrue('Modified' in lines[0])
462 self.assertTrue('Size' in lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000463
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000464 fn, date, time, size = lines[1].split()
465 self.assertEquals(fn, 'another.name')
466 # XXX: timestamp is not tested
467 self.assertEquals(size, str(len(self.data)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000468
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000469 # Check the namelist
470 names = zipfp.namelist()
471 self.assertEquals(len(names), 3)
472 self.assertTrue(TESTFN in names)
473 self.assertTrue("another.name" in names)
474 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000475
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000476 # Check infolist
477 infos = zipfp.infolist()
478 names = [ i.filename for i in infos ]
479 self.assertEquals(len(names), 3)
480 self.assertTrue(TESTFN in names)
481 self.assertTrue("another.name" in names)
482 self.assertTrue("strfile" in names)
483 for i in infos:
484 self.assertEquals(i.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000485
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000486 # check getinfo
487 for nm in (TESTFN, "another.name", "strfile"):
488 info = zipfp.getinfo(nm)
489 self.assertEquals(info.filename, nm)
490 self.assertEquals(info.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000491
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000492 # Check that testzip doesn't raise an exception
493 zipfp.testzip()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000494
Ezio Melottiafd0d112009-07-15 17:17:17 +0000495 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000496 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000497 self.zip_test(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000498
Ezio Melotti76430242009-07-11 18:28:48 +0000499 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000500 def test_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000501 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000502 self.zip_test(f, zipfile.ZIP_DEFLATED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000503
Ezio Melottiafd0d112009-07-15 17:17:17 +0000504 def test_absolute_arcnames(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000505 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
506 allowZip64=True) as zipfp:
507 zipfp.write(TESTFN, "/absolute")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000508
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000509 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
510 self.assertEqual(zipfp.namelist(), ["absolute"])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000511
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000512 def tearDown(self):
513 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti76430242009-07-11 18:28:48 +0000514 unlink(TESTFN)
515 unlink(TESTFN2)
516
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000517
518class PyZipFileTests(unittest.TestCase):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000519 def test_write_pyfile(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000520 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
521 fn = __file__
522 if fn.endswith('.pyc') or fn.endswith('.pyo'):
523 fn = fn[:-1]
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000524
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000525 zipfp.writepy(fn)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000526
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000527 bn = os.path.basename(fn)
528 self.assertTrue(bn not in zipfp.namelist())
529 self.assertTrue(bn + 'o' in zipfp.namelist() or
530 bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000531
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000532 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
533 fn = __file__
534 if fn.endswith('.pyc') or fn.endswith('.pyo'):
535 fn = fn[:-1]
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000536
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000537 zipfp.writepy(fn, "testpackage")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000538
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000539 bn = "%s/%s"%("testpackage", os.path.basename(fn))
540 self.assertTrue(bn not in zipfp.namelist())
541 self.assertTrue(bn + 'o' in zipfp.namelist() or
542 bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000543
Ezio Melottiafd0d112009-07-15 17:17:17 +0000544 def test_write_python_package(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000545 import email
546 packagedir = os.path.dirname(email.__file__)
547
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000548 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
549 zipfp.writepy(packagedir)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000550
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000551 # Check for a couple of modules at different levels of the hieararchy
552 names = zipfp.namelist()
553 self.assertTrue('email/__init__.pyo' in names or
554 'email/__init__.pyc' in names)
555 self.assertTrue('email/mime/text.pyo' in names or
556 'email/mime/text.pyc' in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000557
Ezio Melottiafd0d112009-07-15 17:17:17 +0000558 def test_write_python_directory(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000559 os.mkdir(TESTFN2)
560 try:
561 fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000562 fp.write("print(42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000563 fp.close()
564
565 fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000566 fp.write("print(42 * 42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000567 fp.close()
568
569 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
570 fp.write("bla bla bla\n")
571 fp.close()
572
573 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
574 zipfp.writepy(TESTFN2)
575
576 names = zipfp.namelist()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000577 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
578 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
579 self.assertTrue('mod2.txt' not in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000580
581 finally:
582 shutil.rmtree(TESTFN2)
583
Ezio Melottiafd0d112009-07-15 17:17:17 +0000584 def test_write_non_pyfile(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000585 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
586 open(TESTFN, 'w').write('most definitely not a python file')
587 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
588 os.remove(TESTFN)
589
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000590
591
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000592class OtherTests(unittest.TestCase):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000593 def test_unicode_filenames(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000594 with zipfile.ZipFile(TESTFN, "w") as zf:
595 zf.writestr("foo.txt", "Test for unicode filename")
596 zf.writestr("\xf6.txt", "Test for unicode filename")
597
598 with zipfile.ZipFile(TESTFN, "r") as zf:
599 self.assertEqual(zf.filelist[0].filename, "foo.txt")
600 self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000601
Ezio Melottiafd0d112009-07-15 17:17:17 +0000602 def test_create_non_existent_file_for_append(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000603 if os.path.exists(TESTFN):
604 os.unlink(TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000605
Thomas Wouterscf297e42007-02-23 15:07:44 +0000606 filename = 'testfile.txt'
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000607 content = b'hello, world. this is some content.'
Guido van Rossumd8faa362007-04-27 19:54:29 +0000608
Thomas Wouterscf297e42007-02-23 15:07:44 +0000609 try:
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000610 with zipfile.ZipFile(TESTFN, 'a') as zf:
611 zf.writestr(filename, content)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000612 except IOError:
613 self.fail('Could not append data to a non-existent zip file.')
614
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000615 self.assertTrue(os.path.exists(TESTFN))
Thomas Wouterscf297e42007-02-23 15:07:44 +0000616
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000617 with zipfile.ZipFile(TESTFN, 'r') as zf:
618 self.assertEqual(zf.read(filename), content)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000619
Ezio Melottiafd0d112009-07-15 17:17:17 +0000620 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000621 # This test checks that the ZipFile constructor closes the file object
622 # it opens if there's an error in the file. If it doesn't, the traceback
623 # holds a reference to the ZipFile object and, indirectly, the file object.
624 # On Windows, this causes the os.unlink() call to fail because the
625 # underlying file is still open. This is SF bug #412214.
626 #
627 fp = open(TESTFN, "w")
628 fp.write("this is not a legal zip file\n")
629 fp.close()
630 try:
631 zf = zipfile.ZipFile(TESTFN)
632 except zipfile.BadZipfile:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000633 pass
634
Ezio Melottiafd0d112009-07-15 17:17:17 +0000635 def test_is_zip_erroneous_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000636 # This test checks that the is_zipfile function correctly identifies
637 # a file that is not a zip file
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000638
639 # - passing a filename
640 with open(TESTFN, "w") as fp:
641 fp.write("this is not a legal zip file\n")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000642 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000643 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000644 # - passing a file object
645 with open(TESTFN, "rb") as fp:
646 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000647 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000648 # - passing a file-like object
649 fp = io.BytesIO()
650 fp.write(b"this is not a legal zip file\n")
651 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000652 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000653 fp.seek(0,0)
654 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000655 self.assertTrue(not chk)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000656
Ezio Melottiafd0d112009-07-15 17:17:17 +0000657 def test_is_zip_valid_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000658 # This test checks that the is_zipfile function correctly identifies
659 # a file that is a zip file
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000660
661 # - passing a filename
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000662 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
663 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
664
Guido van Rossumd8faa362007-04-27 19:54:29 +0000665 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000666 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000667 # - passing a file object
668 with open(TESTFN, "rb") as fp:
669 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000670 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000671 fp.seek(0,0)
672 zip_contents = fp.read()
673 # - passing a file-like object
674 fp = io.BytesIO()
675 fp.write(zip_contents)
676 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000677 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000678 fp.seek(0,0)
679 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000680 self.assertTrue(chk)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000681
Ezio Melottiafd0d112009-07-15 17:17:17 +0000682 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000683 # make sure we don't raise an AttributeError when a partially-constructed
684 # ZipFile instance is finalized; this tests for regression on SF tracker
685 # bug #403871.
686
687 # The bug we're testing for caused an AttributeError to be raised
688 # when a ZipFile instance was created for a file that did not
689 # exist; the .fp member was not initialized but was needed by the
690 # __del__() method. Since the AttributeError is in the __del__(),
691 # it is ignored, but the user should be sufficiently annoyed by
692 # the message on the output that regression will be noticed
693 # quickly.
694 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
695
Amaury Forgeot d'Arcbc347802009-07-28 22:18:57 +0000696 def test_empty_file_raises_BadZipFile(self):
697 f = open(TESTFN, 'w')
698 f.close()
699 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
700
701 f = open(TESTFN, 'w')
702 f.write("short file")
703 f.close()
704 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
705
Ezio Melottiafd0d112009-07-15 17:17:17 +0000706 def test_closed_zip_raises_RuntimeError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000707 # Verify that testzip() doesn't swallow inappropriate exceptions.
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000708 data = io.BytesIO()
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000709 with zipfile.ZipFile(data, mode="w") as zipf:
710 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000711
712 # This is correct; calling .read on a closed ZipFile should throw
713 # a RuntimeError, and so should calling .testzip. An earlier
714 # version of .testzip would swallow this exception (and any other)
715 # and report that the first file in the archive was corrupt.
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000716 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
717 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000718 self.assertRaises(RuntimeError, zipf.testzip)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000719 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Guido van Rossum814661e2007-07-18 22:07:29 +0000720 open(TESTFN, 'w').write('zipfile test data')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000721 self.assertRaises(RuntimeError, zipf.write, TESTFN)
722
Ezio Melottiafd0d112009-07-15 17:17:17 +0000723 def test_bad_constructor_mode(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000724 # Check that bad modes passed to ZipFile constructor are caught
725 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
726
Ezio Melottiafd0d112009-07-15 17:17:17 +0000727 def test_bad_open_mode(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000728 # Check that bad modes passed to ZipFile.open are caught
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000729 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
730 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
731
732 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000733 # read the data to make sure the file is there
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000734 zipf.read("foo.txt")
735 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000736
Ezio Melottiafd0d112009-07-15 17:17:17 +0000737 def test_read0(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000738 # Check that calling read(0) on a ZipExtFile object returns an empty
739 # string and doesn't advance file pointer
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000740 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
741 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
742 # read the data to make sure the file is there
743 f = zipf.open("foo.txt")
744 for i in range(FIXEDTEST_SIZE):
745 self.assertEqual(f.read(0), b'')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000746
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000747 self.assertEqual(f.read(), b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000748
Ezio Melottiafd0d112009-07-15 17:17:17 +0000749 def test_open_non_existent_item(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000750 # Check that attempting to call open() for an item that doesn't
751 # exist in the archive raises a RuntimeError
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000752 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
753 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000754
Ezio Melottiafd0d112009-07-15 17:17:17 +0000755 def test_bad_compression_mode(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000756 # Check that bad compression methods passed to ZipFile.open are caught
757 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
758
Ezio Melottiafd0d112009-07-15 17:17:17 +0000759 def test_null_byte_in_filename(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000760 # Check that a filename containing a null byte is properly terminated
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000761 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
762 zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
763 self.assertEqual(zipf.namelist(), ['foo.txt'])
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000764
Ezio Melottiafd0d112009-07-15 17:17:17 +0000765 def test_struct_sizes(self):
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000766 # check that ZIP internal structure sizes are calculated correctly
767 self.assertEqual(zipfile.sizeEndCentDir, 22)
768 self.assertEqual(zipfile.sizeCentralDir, 46)
769 self.assertEqual(zipfile.sizeEndCentDir64, 56)
770 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
771
Ezio Melottiafd0d112009-07-15 17:17:17 +0000772 def test_comments(self):
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000773 # This test checks that comments on the archive are handled properly
774
775 # check default comment is empty
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000776 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
777 self.assertEqual(zipf.comment, b'')
778 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
779
780 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
781 self.assertEqual(zipfr.comment, b'')
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000782
783 # check a simple short comment
784 comment = b'Bravely taking to his feet, he beat a very brave retreat.'
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000785 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
786 zipf.comment = comment
787 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
788 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
789 self.assertEqual(zipf.comment, comment)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000790
791 # check a comment of max length
792 comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
793 comment2 = comment2.encode("ascii")
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000794 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
795 zipf.comment = comment2
796 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
797
798 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
799 self.assertEqual(zipfr.comment, comment2)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000800
801 # check a comment that is too long is truncated
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000802 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
803 zipf.comment = comment2 + b'oops'
804 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
805 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
806 self.assertEqual(zipfr.comment, comment2)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000807
Guido van Rossumd8faa362007-04-27 19:54:29 +0000808 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000809 unlink(TESTFN)
810 unlink(TESTFN2)
811
Thomas Wouterscf297e42007-02-23 15:07:44 +0000812
813class DecryptionTests(unittest.TestCase):
814 # This test checks that ZIP decryption works. Since the library does not
815 # support encryption at the moment, we use a pre-generated encrypted
816 # ZIP file
817
818 data = (
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000819 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
820 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
821 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
822 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
823 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
824 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
825 b'\x00\x00L\x00\x00\x00\x00\x00' )
Christian Heimesfdab48e2008-01-20 09:06:41 +0000826 data2 = (
827 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
828 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
829 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
830 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
831 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
832 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
833 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
834 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Thomas Wouterscf297e42007-02-23 15:07:44 +0000835
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000836 plain = b'zipfile.py encryption test'
Christian Heimesfdab48e2008-01-20 09:06:41 +0000837 plain2 = b'\x00'*512
Thomas Wouterscf297e42007-02-23 15:07:44 +0000838
839 def setUp(self):
840 fp = open(TESTFN, "wb")
841 fp.write(self.data)
842 fp.close()
843 self.zip = zipfile.ZipFile(TESTFN, "r")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000844 fp = open(TESTFN2, "wb")
845 fp.write(self.data2)
846 fp.close()
847 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000848
849 def tearDown(self):
850 self.zip.close()
851 os.unlink(TESTFN)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000852 self.zip2.close()
853 os.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000854
Ezio Melottiafd0d112009-07-15 17:17:17 +0000855 def test_no_password(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000856 # Reading the encrypted file without password
857 # must generate a RunTime exception
858 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000859 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000860
Ezio Melottiafd0d112009-07-15 17:17:17 +0000861 def test_bad_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000862 self.zip.setpassword(b"perl")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000863 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000864 self.zip2.setpassword(b"perl")
865 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000866
Ezio Melotti78ea2022009-09-12 18:41:20 +0000867 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000868 def test_good_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000869 self.zip.setpassword(b"python")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000870 self.assertEquals(self.zip.read("test.txt"), self.plain)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000871 self.zip2.setpassword(b"12345")
872 self.assertEquals(self.zip2.read("zero"), self.plain2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000873
Guido van Rossumd8faa362007-04-27 19:54:29 +0000874
875class TestsWithRandomBinaryFiles(unittest.TestCase):
876 def setUp(self):
877 datacount = randint(16, 64)*1024 + randint(1, 1024)
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000878 self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
879 for i in range(datacount))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000880
881 # Make a source file with some lines
882 fp = open(TESTFN, "wb")
883 fp.write(self.data)
884 fp.close()
885
886 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000887 unlink(TESTFN)
888 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000889
Ezio Melottiafd0d112009-07-15 17:17:17 +0000890 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000891 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000892 with zipfile.ZipFile(f, "w", compression) as zipfp:
893 zipfp.write(TESTFN, "another.name")
894 zipfp.write(TESTFN, TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000895
Ezio Melottiafd0d112009-07-15 17:17:17 +0000896 def zip_test(self, f, compression):
897 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000898
899 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000900 with zipfile.ZipFile(f, "r", compression) as zipfp:
901 testdata = zipfp.read(TESTFN)
902 self.assertEqual(len(testdata), len(self.data))
903 self.assertEqual(testdata, self.data)
904 self.assertEqual(zipfp.read("another.name"), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000905
Ezio Melottiafd0d112009-07-15 17:17:17 +0000906 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000907 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000908 self.zip_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000909
Ezio Melottiafd0d112009-07-15 17:17:17 +0000910 def zip_open_test(self, f, compression):
911 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000912
913 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000914 with zipfile.ZipFile(f, "r", compression) as zipfp:
915 zipdata1 = []
916 zipopen1 = zipfp.open(TESTFN)
917 while True:
918 read_data = zipopen1.read(256)
919 if not read_data:
920 break
921 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000922
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000923 zipdata2 = []
924 zipopen2 = zipfp.open("another.name")
925 while True:
926 read_data = zipopen2.read(256)
927 if not read_data:
928 break
929 zipdata2.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000930
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000931 testdata1 = b''.join(zipdata1)
932 self.assertEqual(len(testdata1), len(self.data))
933 self.assertEqual(testdata1, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000934
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000935 testdata2 = b''.join(zipdata2)
936 self.assertEqual(len(testdata1), len(self.data))
937 self.assertEqual(testdata1, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000938
Ezio Melottiafd0d112009-07-15 17:17:17 +0000939 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000940 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000941 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000942
Ezio Melottiafd0d112009-07-15 17:17:17 +0000943 def zip_random_open_test(self, f, compression):
944 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000945
946 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000947 with zipfile.ZipFile(f, "r", compression) as zipfp:
948 zipdata1 = []
949 zipopen1 = zipfp.open(TESTFN)
950 while True:
951 read_data = zipopen1.read(randint(1, 1024))
952 if not read_data:
953 break
954 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000955
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000956 testdata = b''.join(zipdata1)
957 self.assertEqual(len(testdata), len(self.data))
958 self.assertEqual(testdata, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000959
Ezio Melottiafd0d112009-07-15 17:17:17 +0000960 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000961 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000962 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000963
Ezio Melotti76430242009-07-11 18:28:48 +0000964
Ezio Melotti78ea2022009-09-12 18:41:20 +0000965@skipUnless(zlib, "requires zlib")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000966class TestsWithMultipleOpens(unittest.TestCase):
967 def setUp(self):
968 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000969 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
970 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
971 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000972
Ezio Melottiafd0d112009-07-15 17:17:17 +0000973 def test_same_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000974 # Verify that (when the ZipFile is in control of creating file objects)
975 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000976 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
977 zopen1 = zipf.open('ones')
978 zopen2 = zipf.open('ones')
979 data1 = zopen1.read(500)
980 data2 = zopen2.read(500)
981 data1 += zopen1.read(500)
982 data2 += zopen2.read(500)
983 self.assertEqual(data1, data2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000984
Ezio Melottiafd0d112009-07-15 17:17:17 +0000985 def test_different_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000986 # Verify that (when the ZipFile is in control of creating file objects)
987 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000988 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
989 zopen1 = zipf.open('ones')
990 zopen2 = zipf.open('twos')
991 data1 = zopen1.read(500)
992 data2 = zopen2.read(500)
993 data1 += zopen1.read(500)
994 data2 += zopen2.read(500)
995 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
996 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000997
Ezio Melottiafd0d112009-07-15 17:17:17 +0000998 def test_interleaved(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000999 # Verify that (when the ZipFile is in control of creating file objects)
1000 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001001 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1002 zopen1 = zipf.open('ones')
1003 data1 = zopen1.read(500)
1004 zopen2 = zipf.open('twos')
1005 data2 = zopen2.read(500)
1006 data1 += zopen1.read(500)
1007 data2 += zopen2.read(500)
1008 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1009 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001010
1011 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001012 unlink(TESTFN2)
1013
Guido van Rossumd8faa362007-04-27 19:54:29 +00001014
Martin v. Löwis59e47792009-01-24 14:10:07 +00001015class TestWithDirectory(unittest.TestCase):
1016 def setUp(self):
1017 os.mkdir(TESTFN2)
1018
Ezio Melottiafd0d112009-07-15 17:17:17 +00001019 def test_extract_dir(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001020 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
1021 zipf.extractall(TESTFN2)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001022 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1023 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1024 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1025
Ezio Melottiafd0d112009-07-15 17:17:17 +00001026 def test_bug_6050(self):
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001027 # Extraction should succeed if directories already exist
1028 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottiafd0d112009-07-15 17:17:17 +00001029 self.test_extract_dir()
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001030
Ezio Melottiafd0d112009-07-15 17:17:17 +00001031 def test_store_dir(self):
Martin v. Löwis59e47792009-01-24 14:10:07 +00001032 os.mkdir(os.path.join(TESTFN2, "x"))
1033 zipf = zipfile.ZipFile(TESTFN, "w")
1034 zipf.write(os.path.join(TESTFN2, "x"), "x")
1035 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1036
1037 def tearDown(self):
1038 shutil.rmtree(TESTFN2)
1039 if os.path.exists(TESTFN):
Ezio Melotti76430242009-07-11 18:28:48 +00001040 unlink(TESTFN)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001041
Guido van Rossumd8faa362007-04-27 19:54:29 +00001042
1043class UniversalNewlineTests(unittest.TestCase):
1044 def setUp(self):
Guido van Rossum9c627722007-08-27 18:31:48 +00001045 self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001046 for i in range(FIXEDTEST_SIZE)]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001047 self.seps = ('\r', '\r\n', '\n')
1048 self.arcdata, self.arcfiles = {}, {}
1049 for n, s in enumerate(self.seps):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001050 b = s.encode("ascii")
1051 self.arcdata[s] = b.join(self.line_gen) + b
Guido van Rossumd8faa362007-04-27 19:54:29 +00001052 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001053 f = open(self.arcfiles[s], "wb")
1054 try:
1055 f.write(self.arcdata[s])
1056 finally:
1057 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001058
Ezio Melottiafd0d112009-07-15 17:17:17 +00001059 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001060 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001061 with zipfile.ZipFile(f, "w", compression) as zipfp:
1062 for fn in self.arcfiles.values():
1063 zipfp.write(fn, fn)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001064
Ezio Melottiafd0d112009-07-15 17:17:17 +00001065 def read_test(self, f, compression):
1066 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001067
1068 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001069 with zipfile.ZipFile(f, "r") as zipfp:
1070 for sep, fn in self.arcfiles.items():
1071 zipdata = zipfp.open(fn, "rU").read()
1072 self.assertEqual(self.arcdata[sep], zipdata)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001073
Ezio Melottiafd0d112009-07-15 17:17:17 +00001074 def readline_test(self, f, compression):
1075 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001076
1077 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001078 with zipfile.ZipFile(f, "r") as zipfp:
1079 for sep, fn in self.arcfiles.items():
1080 zipopen = zipfp.open(fn, "rU")
1081 for line in self.line_gen:
1082 linedata = zipopen.readline()
1083 self.assertEqual(linedata, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001084
Ezio Melottiafd0d112009-07-15 17:17:17 +00001085 def readlines_test(self, f, compression):
1086 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001087
1088 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001089 with zipfile.ZipFile(f, "r") as zipfp:
1090 for sep, fn in self.arcfiles.items():
1091 ziplines = zipfp.open(fn, "rU").readlines()
1092 for line, zipline in zip(self.line_gen, ziplines):
1093 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001094
Ezio Melottiafd0d112009-07-15 17:17:17 +00001095 def iterlines_test(self, f, compression):
1096 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001097
1098 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001099 with zipfile.ZipFile(f, "r") as zipfp:
1100 for sep, fn in self.arcfiles.items():
1101 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
1102 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001103
Ezio Melottiafd0d112009-07-15 17:17:17 +00001104 def test_read_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001105 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001106 self.read_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001107
Ezio Melottiafd0d112009-07-15 17:17:17 +00001108 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001109 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001110 self.readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001111
Ezio Melottiafd0d112009-07-15 17:17:17 +00001112 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001113 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001114 self.readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001115
Ezio Melottiafd0d112009-07-15 17:17:17 +00001116 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001117 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001118 self.iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001119
Ezio Melotti76430242009-07-11 18:28:48 +00001120 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001121 def test_read_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001122 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001123 self.read_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001124
Ezio Melotti76430242009-07-11 18:28:48 +00001125 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001126 def test_readline_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001127 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001128 self.readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001129
Ezio Melotti76430242009-07-11 18:28:48 +00001130 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001131 def test_readlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001132 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001133 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001134
Ezio Melotti76430242009-07-11 18:28:48 +00001135 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001136 def test_iterlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001137 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001138 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001139
1140 def tearDown(self):
1141 for sep, fn in self.arcfiles.items():
1142 os.remove(fn)
Ezio Melotti76430242009-07-11 18:28:48 +00001143 unlink(TESTFN)
1144 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001145
1146
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001147def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +00001148 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1149 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti76430242009-07-11 18:28:48 +00001150 TestWithDirectory, UniversalNewlineTests,
1151 TestsWithRandomBinaryFiles)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001152
1153if __name__ == "__main__":
1154 test_main()