blob: d4bb4d2c75bfd7b309243dea7d129becb80a50ec [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
45 zipfp = zipfile.ZipFile(f, "w", compression)
Skip Montanaro7a98be22007-08-16 14:35:24 +000046 zipfp.write(TESTFN, "another.name")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000047 zipfp.write(TESTFN, TESTFN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000048 zipfp.writestr("strfile", self.data)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000049 zipfp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000050
Ezio Melottiafd0d112009-07-15 17:17:17 +000051 def zip_test(self, f, compression):
52 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +000053
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000054 # Read the ZIP archive
55 zipfp = zipfile.ZipFile(f, "r", compression)
56 self.assertEqual(zipfp.read(TESTFN), self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +000057 self.assertEqual(zipfp.read("another.name"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058 self.assertEqual(zipfp.read("strfile"), self.data)
59
60 # Print the ZIP directory
Guido van Rossumd6ca5462007-05-22 01:29:33 +000061 fp = io.StringIO()
62 zipfp.printdir(file=fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063 directory = fp.getvalue()
64 lines = directory.splitlines()
65 self.assertEquals(len(lines), 4) # Number of files + header
66
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000067 self.assertTrue('File Name' in lines[0])
68 self.assertTrue('Modified' in lines[0])
69 self.assertTrue('Size' in lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070
71 fn, date, time, size = lines[1].split()
72 self.assertEquals(fn, 'another.name')
73 # XXX: timestamp is not tested
74 self.assertEquals(size, str(len(self.data)))
75
76 # Check the namelist
77 names = zipfp.namelist()
78 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000079 self.assertTrue(TESTFN in names)
80 self.assertTrue("another.name" in names)
81 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000082
83 # Check infolist
84 infos = zipfp.infolist()
85 names = [ i.filename for i in infos ]
86 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000087 self.assertTrue(TESTFN in names)
88 self.assertTrue("another.name" in names)
89 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000090 for i in infos:
91 self.assertEquals(i.file_size, len(self.data))
92
93 # check getinfo
Skip Montanaro7a98be22007-08-16 14:35:24 +000094 for nm in (TESTFN, "another.name", "strfile"):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095 info = zipfp.getinfo(nm)
96 self.assertEquals(info.filename, nm)
97 self.assertEquals(info.file_size, len(self.data))
98
99 # Check that testzip doesn't raise an exception
100 zipfp.testzip()
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000101 zipfp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +0000102
Ezio Melottiafd0d112009-07-15 17:17:17 +0000103 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000104 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000105 self.zip_test(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000106
Ezio Melottiafd0d112009-07-15 17:17:17 +0000107 def zip_open_test(self, f, compression):
108 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000109
110 # Read the ZIP archive
111 zipfp = zipfile.ZipFile(f, "r", compression)
112 zipdata1 = []
113 zipopen1 = zipfp.open(TESTFN)
114 while 1:
115 read_data = zipopen1.read(256)
116 if not read_data:
117 break
118 zipdata1.append(read_data)
119
120 zipdata2 = []
Skip Montanaro7a98be22007-08-16 14:35:24 +0000121 zipopen2 = zipfp.open("another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000122 while 1:
123 read_data = zipopen2.read(256)
124 if not read_data:
125 break
126 zipdata2.append(read_data)
127
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000128 self.assertEqual(b''.join(zipdata1), self.data)
129 self.assertEqual(b''.join(zipdata2), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000130 zipfp.close()
131
Ezio Melottiafd0d112009-07-15 17:17:17 +0000132 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000133 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000134 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000135
Ezio Melottiafd0d112009-07-15 17:17:17 +0000136 def test_open_via_zip_info(self):
Georg Brandlb533e262008-05-25 18:19:30 +0000137 # Create the ZIP archive
138 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
139 zipfp.writestr("name", "foo")
140 zipfp.writestr("name", "bar")
141 zipfp.close()
142
143 zipfp = zipfile.ZipFile(TESTFN2, "r")
144 infos = zipfp.infolist()
145 data = b""
146 for info in infos:
147 data += zipfp.open(info).read()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000148 self.assertTrue(data == b"foobar" or data == b"barfoo")
Georg Brandlb533e262008-05-25 18:19:30 +0000149 data = b""
150 for info in infos:
151 data += zipfp.read(info)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000152 self.assertTrue(data == b"foobar" or data == b"barfoo")
Georg Brandlb533e262008-05-25 18:19:30 +0000153 zipfp.close()
154
Ezio Melottiafd0d112009-07-15 17:17:17 +0000155 def zip_random_open_test(self, f, compression):
156 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000157
158 # Read the ZIP archive
159 zipfp = zipfile.ZipFile(f, "r", compression)
160 zipdata1 = []
161 zipopen1 = zipfp.open(TESTFN)
162 while 1:
163 read_data = zipopen1.read(randint(1, 1024))
164 if not read_data:
165 break
166 zipdata1.append(read_data)
167
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000168 self.assertEqual(b''.join(zipdata1), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000169 zipfp.close()
170
Ezio Melottiafd0d112009-07-15 17:17:17 +0000171 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000172 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000173 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000174
Ezio Melottiafd0d112009-07-15 17:17:17 +0000175 def zip_readline_test(self, f, compression):
176 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000177
178 # Read the ZIP archive
179 zipfp = zipfile.ZipFile(f, "r")
180 zipopen = zipfp.open(TESTFN)
181 for line in self.line_gen:
182 linedata = zipopen.readline()
183 self.assertEqual(linedata, line + '\n')
184
185 zipfp.close()
186
Ezio Melottiafd0d112009-07-15 17:17:17 +0000187 def zip_readlines_test(self, f, compression):
188 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000189
190 # Read the ZIP archive
191 zipfp = zipfile.ZipFile(f, "r")
192 ziplines = zipfp.open(TESTFN).readlines()
193 for line, zipline in zip(self.line_gen, ziplines):
194 self.assertEqual(zipline, line + '\n')
195
196 zipfp.close()
197
Ezio Melottiafd0d112009-07-15 17:17:17 +0000198 def zip_iterlines_test(self, f, compression):
199 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000200
201 # Read the ZIP archive
202 zipfp = zipfile.ZipFile(f, "r")
203 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
204 self.assertEqual(zipline, line + '\n')
205
206 zipfp.close()
207
Ezio Melottiafd0d112009-07-15 17:17:17 +0000208 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000209 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000210 self.zip_readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000211
Ezio Melottiafd0d112009-07-15 17:17:17 +0000212 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000213 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000214 self.zip_readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000215
Ezio Melottiafd0d112009-07-15 17:17:17 +0000216 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000217 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000218 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
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_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_test(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000224
Guido van Rossumd8faa362007-04-27 19:54:29 +0000225
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000226 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000227 def test_open_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000228 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000229 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000230
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000231 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000232 def test_random_open_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000233 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000234 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000235
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000236 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000237 def test_readline_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000238 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000239 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000240
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000241 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000242 def test_readlines_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000243 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000244 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000245
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000246 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000247 def test_iterlines_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000248 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000249 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000250
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000251 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000252 def test_low_compression(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000253 # Checks for cases where compressed data is larger than original
254 # Create the ZIP archive
255 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
256 zipfp.writestr("strfile", '12')
257 zipfp.close()
258
259 # Get an open object for strfile
260 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED)
261 openobj = zipfp.open("strfile")
262 self.assertEqual(openobj.read(1), b'1')
263 self.assertEqual(openobj.read(1), b'2')
264
Ezio Melottiafd0d112009-07-15 17:17:17 +0000265 def test_absolute_arcnames(self):
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000266 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
267 zipfp.write(TESTFN, "/absolute")
268 zipfp.close()
269
270 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
271 self.assertEqual(zipfp.namelist(), ["absolute"])
272 zipfp.close()
Tim Peters32cbc962006-02-20 21:42:18 +0000273
Ezio Melottiafd0d112009-07-15 17:17:17 +0000274 def test_append_to_zip_file(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000275 # Test appending to an existing zipfile
276 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
277 zipfp.write(TESTFN, TESTFN)
278 zipfp.close()
279 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
280 zipfp.writestr("strfile", self.data)
281 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
282 zipfp.close()
283
Ezio Melottiafd0d112009-07-15 17:17:17 +0000284 def test_append_to_non_zip_file(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000285 # Test appending to an existing file that is not a zipfile
286 # NOTE: this test fails if len(d) < 22 because of the first
287 # line "fpin.seek(-22, 2)" in _EndRecData
Guido van Rossum9c627722007-08-27 18:31:48 +0000288 d = b'I am not a ZipFile!'*10
Guido van Rossum814661e2007-07-18 22:07:29 +0000289 f = open(TESTFN2, 'wb')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000290 f.write(d)
291 f.close()
292 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
293 zipfp.write(TESTFN, TESTFN)
294 zipfp.close()
295
Guido van Rossum814661e2007-07-18 22:07:29 +0000296 f = open(TESTFN2, 'rb')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000297 f.seek(len(d))
298 zipfp = zipfile.ZipFile(f, "r")
299 self.assertEqual(zipfp.namelist(), [TESTFN])
300 zipfp.close()
301 f.close()
302
Ezio Melottiafd0d112009-07-15 17:17:17 +0000303 def test_write_default_name(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000304 # Check that calling ZipFile.write without arcname specified produces the expected result
305 zipfp = zipfile.ZipFile(TESTFN2, "w")
306 zipfp.write(TESTFN)
Guido van Rossum814661e2007-07-18 22:07:29 +0000307 self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read())
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000308 zipfp.close()
309
Ezio Melottiafd0d112009-07-15 17:17:17 +0000310 def test_per_file_compression(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000311 # Check that files within a Zip archive can have different compression options
312 zipfp = zipfile.ZipFile(TESTFN2, "w")
313 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
314 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
315 sinfo = zipfp.getinfo('storeme')
316 dinfo = zipfp.getinfo('deflateme')
317 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
318 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
319 zipfp.close()
320
Ezio Melottiafd0d112009-07-15 17:17:17 +0000321 def test_write_to_readonly(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000322 # Check that trying to call write() on a readonly ZipFile object
323 # raises a RuntimeError
324 zipf = zipfile.ZipFile(TESTFN2, mode="w")
325 zipf.writestr("somefile.txt", "bogus")
326 zipf.close()
327 zipf = zipfile.ZipFile(TESTFN2, mode="r")
328 self.assertRaises(RuntimeError, zipf.write, TESTFN)
329 zipf.close()
330
Ezio Melottiafd0d112009-07-15 17:17:17 +0000331 def test_extract(self):
Christian Heimes790c8232008-01-07 21:14:23 +0000332 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
333 for fpath, fdata in SMALL_TEST_DATA:
334 zipfp.writestr(fpath, fdata)
335 zipfp.close()
336
337 zipfp = zipfile.ZipFile(TESTFN2, "r")
338 for fpath, fdata in SMALL_TEST_DATA:
339 writtenfile = zipfp.extract(fpath)
340
341 # make sure it was written to the right place
342 if os.path.isabs(fpath):
343 correctfile = os.path.join(os.getcwd(), fpath[1:])
344 else:
345 correctfile = os.path.join(os.getcwd(), fpath)
Christian Heimesaf98da12008-01-27 15:18:18 +0000346 correctfile = os.path.normpath(correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000347
348 self.assertEqual(writtenfile, correctfile)
349
350 # make sure correct data is in correct file
351 self.assertEqual(fdata.encode(), open(writtenfile, "rb").read())
352
353 os.remove(writtenfile)
354
355 zipfp.close()
356
357 # remove the test file subdirectories
358 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
359
Ezio Melottiafd0d112009-07-15 17:17:17 +0000360 def test_extract_all(self):
Christian Heimes790c8232008-01-07 21:14:23 +0000361 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
362 for fpath, fdata in SMALL_TEST_DATA:
363 zipfp.writestr(fpath, fdata)
364 zipfp.close()
365
366 zipfp = zipfile.ZipFile(TESTFN2, "r")
367 zipfp.extractall()
368 for fpath, fdata in SMALL_TEST_DATA:
369 if os.path.isabs(fpath):
370 outfile = os.path.join(os.getcwd(), fpath[1:])
371 else:
372 outfile = os.path.join(os.getcwd(), fpath)
373
374 self.assertEqual(fdata.encode(), open(outfile, "rb").read())
375
376 os.remove(outfile)
377
378 zipfp.close()
379
380 # remove the test file subdirectories
381 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
382
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000383 def zip_test_writestr_permissions(self, f, compression):
384 # Make sure that writestr creates files with mode 0600,
385 # when it is passed a name rather than a ZipInfo instance.
386
Ezio Melottiafd0d112009-07-15 17:17:17 +0000387 self.make_test_archive(f, compression)
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000388 zipfp = zipfile.ZipFile(f, "r")
389 zinfo = zipfp.getinfo('strfile')
390 self.assertEqual(zinfo.external_attr, 0o600 << 16)
391
Ezio Melottiafd0d112009-07-15 17:17:17 +0000392 def test_writestr_permissions(self):
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000393 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
394 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
395
Gregory P. Smithb0d9ca92009-07-07 05:06:04 +0000396 def test_writestr_extended_local_header_issue1202(self):
397 orig_zip = zipfile.ZipFile(TESTFN2, 'w')
398 for data in 'abcdefghijklmnop':
399 zinfo = zipfile.ZipInfo(data)
400 zinfo.flag_bits |= 0x08 # Include an extended local header.
401 orig_zip.writestr(zinfo, data)
402 orig_zip.close()
403
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000404 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000405 unlink(TESTFN)
406 unlink(TESTFN2)
407
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000408
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000409class TestZip64InSmallFiles(unittest.TestCase):
410 # These tests test the ZIP64 functionality without using large files,
411 # see test_zipfile64 for proper tests.
412
413 def setUp(self):
414 self._limit = zipfile.ZIP64_LIMIT
415 zipfile.ZIP64_LIMIT = 5
416
Guido van Rossum9c627722007-08-27 18:31:48 +0000417 line_gen = (bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000418 for i in range(0, FIXEDTEST_SIZE))
419 self.data = b'\n'.join(line_gen)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000420
421 # Make a source file with some lines
422 fp = open(TESTFN, "wb")
423 fp.write(self.data)
424 fp.close()
425
Ezio Melottiafd0d112009-07-15 17:17:17 +0000426 def large_file_exception_test(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000427 zipfp = zipfile.ZipFile(f, "w", compression)
428 self.assertRaises(zipfile.LargeZipFile,
Skip Montanaro7a98be22007-08-16 14:35:24 +0000429 zipfp.write, TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000430 zipfp.close()
431
Ezio Melottiafd0d112009-07-15 17:17:17 +0000432 def large_file_exception_test2(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000433 zipfp = zipfile.ZipFile(f, "w", compression)
434 self.assertRaises(zipfile.LargeZipFile,
Skip Montanaro7a98be22007-08-16 14:35:24 +0000435 zipfp.writestr, "another.name", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000436 zipfp.close()
437
Ezio Melottiafd0d112009-07-15 17:17:17 +0000438 def test_large_file_exception(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000439 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000440 self.large_file_exception_test(f, zipfile.ZIP_STORED)
441 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000442
Ezio Melottiafd0d112009-07-15 17:17:17 +0000443 def zip_test(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000444 # Create the ZIP archive
445 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000446 zipfp.write(TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000447 zipfp.write(TESTFN, TESTFN)
448 zipfp.writestr("strfile", self.data)
449 zipfp.close()
450
451 # Read the ZIP archive
452 zipfp = zipfile.ZipFile(f, "r", compression)
453 self.assertEqual(zipfp.read(TESTFN), self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000454 self.assertEqual(zipfp.read("another.name"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000455 self.assertEqual(zipfp.read("strfile"), self.data)
456
457 # Print the ZIP directory
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000458 fp = io.StringIO()
459 zipfp.printdir(fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000460
461 directory = fp.getvalue()
462 lines = directory.splitlines()
463 self.assertEquals(len(lines), 4) # Number of files + header
464
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000465 self.assertTrue('File Name' in lines[0])
466 self.assertTrue('Modified' in lines[0])
467 self.assertTrue('Size' in lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000468
469 fn, date, time, size = lines[1].split()
470 self.assertEquals(fn, 'another.name')
471 # XXX: timestamp is not tested
472 self.assertEquals(size, str(len(self.data)))
473
474 # Check the namelist
475 names = zipfp.namelist()
476 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000477 self.assertTrue(TESTFN in names)
478 self.assertTrue("another.name" in names)
479 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000480
481 # Check infolist
482 infos = zipfp.infolist()
483 names = [ i.filename for i in infos ]
484 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000485 self.assertTrue(TESTFN in names)
486 self.assertTrue("another.name" in names)
487 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000488 for i in infos:
489 self.assertEquals(i.file_size, len(self.data))
490
491 # check getinfo
Skip Montanaro7a98be22007-08-16 14:35:24 +0000492 for nm in (TESTFN, "another.name", "strfile"):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000493 info = zipfp.getinfo(nm)
494 self.assertEquals(info.filename, nm)
495 self.assertEquals(info.file_size, len(self.data))
496
497 # Check that testzip doesn't raise an exception
498 zipfp.testzip()
499
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000500 zipfp.close()
501
Ezio Melottiafd0d112009-07-15 17:17:17 +0000502 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000503 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000504 self.zip_test(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000505
Ezio Melotti76430242009-07-11 18:28:48 +0000506 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000507 def test_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000508 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000509 self.zip_test(f, zipfile.ZIP_DEFLATED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000510
Ezio Melottiafd0d112009-07-15 17:17:17 +0000511 def test_absolute_arcnames(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000512 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True)
513 zipfp.write(TESTFN, "/absolute")
514 zipfp.close()
515
516 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
517 self.assertEqual(zipfp.namelist(), ["absolute"])
518 zipfp.close()
519
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000520 def tearDown(self):
521 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti76430242009-07-11 18:28:48 +0000522 unlink(TESTFN)
523 unlink(TESTFN2)
524
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000525
526class PyZipFileTests(unittest.TestCase):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000527 def test_write_pyfile(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000528 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
529 fn = __file__
530 if fn.endswith('.pyc') or fn.endswith('.pyo'):
531 fn = fn[:-1]
532
533 zipfp.writepy(fn)
534
535 bn = os.path.basename(fn)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000536 self.assertTrue(bn not in zipfp.namelist())
537 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000538 zipfp.close()
539
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000540 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
541 fn = __file__
542 if fn.endswith('.pyc') or fn.endswith('.pyo'):
543 fn = fn[:-1]
544
545 zipfp.writepy(fn, "testpackage")
546
547 bn = "%s/%s"%("testpackage", os.path.basename(fn))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000548 self.assertTrue(bn not in zipfp.namelist())
549 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000550 zipfp.close()
551
Ezio Melottiafd0d112009-07-15 17:17:17 +0000552 def test_write_python_package(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000553 import email
554 packagedir = os.path.dirname(email.__file__)
555
556 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
557 zipfp.writepy(packagedir)
558
559 # Check for a couple of modules at different levels of the hieararchy
560 names = zipfp.namelist()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000561 self.assertTrue('email/__init__.pyo' in names or 'email/__init__.pyc' in names)
562 self.assertTrue('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000563
Ezio Melottiafd0d112009-07-15 17:17:17 +0000564 def test_write_python_directory(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000565 os.mkdir(TESTFN2)
566 try:
567 fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000568 fp.write("print(42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000569 fp.close()
570
571 fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000572 fp.write("print(42 * 42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000573 fp.close()
574
575 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
576 fp.write("bla bla bla\n")
577 fp.close()
578
579 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
580 zipfp.writepy(TESTFN2)
581
582 names = zipfp.namelist()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000583 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
584 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
585 self.assertTrue('mod2.txt' not in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000586
587 finally:
588 shutil.rmtree(TESTFN2)
589
Ezio Melottiafd0d112009-07-15 17:17:17 +0000590 def test_write_non_pyfile(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000591 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
Guido van Rossum814661e2007-07-18 22:07:29 +0000592 open(TESTFN, 'w').write('most definitely not a python file')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000593 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
594 os.remove(TESTFN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000595
596
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000597class OtherTests(unittest.TestCase):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000598 def test_unicode_filenames(self):
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000599 zf = zipfile.ZipFile(TESTFN, "w")
600 zf.writestr("foo.txt", "Test for unicode filename")
Martin v. Löwis1a9f9002008-05-05 17:50:05 +0000601 zf.writestr("\xf6.txt", "Test for unicode filename")
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000602 zf.close()
Martin v. Löwis1a9f9002008-05-05 17:50:05 +0000603 zf = zipfile.ZipFile(TESTFN, "r")
604 self.assertEqual(zf.filelist[0].filename, "foo.txt")
605 self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
606 zf.close()
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000607
Ezio Melottiafd0d112009-07-15 17:17:17 +0000608 def test_create_non_existent_file_for_append(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000609 if os.path.exists(TESTFN):
610 os.unlink(TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000611
Thomas Wouterscf297e42007-02-23 15:07:44 +0000612 filename = 'testfile.txt'
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000613 content = b'hello, world. this is some content.'
Guido van Rossumd8faa362007-04-27 19:54:29 +0000614
Thomas Wouterscf297e42007-02-23 15:07:44 +0000615 try:
616 zf = zipfile.ZipFile(TESTFN, 'a')
617 zf.writestr(filename, content)
618 zf.close()
619 except IOError:
620 self.fail('Could not append data to a non-existent zip file.')
621
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000622 self.assertTrue(os.path.exists(TESTFN))
Thomas Wouterscf297e42007-02-23 15:07:44 +0000623
624 zf = zipfile.ZipFile(TESTFN, 'r')
625 self.assertEqual(zf.read(filename), content)
626 zf.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000627
Ezio Melottiafd0d112009-07-15 17:17:17 +0000628 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000629 # This test checks that the ZipFile constructor closes the file object
630 # it opens if there's an error in the file. If it doesn't, the traceback
631 # holds a reference to the ZipFile object and, indirectly, the file object.
632 # On Windows, this causes the os.unlink() call to fail because the
633 # underlying file is still open. This is SF bug #412214.
634 #
635 fp = open(TESTFN, "w")
636 fp.write("this is not a legal zip file\n")
637 fp.close()
638 try:
639 zf = zipfile.ZipFile(TESTFN)
640 except zipfile.BadZipfile:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000641 pass
642
Ezio Melottiafd0d112009-07-15 17:17:17 +0000643 def test_is_zip_erroneous_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000644 # This test checks that the is_zipfile function correctly identifies
645 # a file that is not a zip file
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000646
647 # - passing a filename
648 with open(TESTFN, "w") as fp:
649 fp.write("this is not a legal zip file\n")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000650 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000651 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000652 # - passing a file object
653 with open(TESTFN, "rb") as fp:
654 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000655 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000656 # - passing a file-like object
657 fp = io.BytesIO()
658 fp.write(b"this is not a legal zip file\n")
659 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000660 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000661 fp.seek(0,0)
662 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000663 self.assertTrue(not chk)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000664
Ezio Melottiafd0d112009-07-15 17:17:17 +0000665 def test_is_zip_valid_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000666 # This test checks that the is_zipfile function correctly identifies
667 # a file that is a zip file
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000668
669 # - passing a filename
Guido van Rossumd8faa362007-04-27 19:54:29 +0000670 zipf = zipfile.ZipFile(TESTFN, mode="w")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000671 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000672 zipf.close()
673 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000674 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000675 # - passing a file object
676 with open(TESTFN, "rb") as fp:
677 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000678 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000679 fp.seek(0,0)
680 zip_contents = fp.read()
681 # - passing a file-like object
682 fp = io.BytesIO()
683 fp.write(zip_contents)
684 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000685 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000686 fp.seek(0,0)
687 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000688 self.assertTrue(chk)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000689
Ezio Melottiafd0d112009-07-15 17:17:17 +0000690 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000691 # make sure we don't raise an AttributeError when a partially-constructed
692 # ZipFile instance is finalized; this tests for regression on SF tracker
693 # bug #403871.
694
695 # The bug we're testing for caused an AttributeError to be raised
696 # when a ZipFile instance was created for a file that did not
697 # exist; the .fp member was not initialized but was needed by the
698 # __del__() method. Since the AttributeError is in the __del__(),
699 # it is ignored, but the user should be sufficiently annoyed by
700 # the message on the output that regression will be noticed
701 # quickly.
702 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
703
Ezio Melottiafd0d112009-07-15 17:17:17 +0000704 def test_closed_zip_raises_RuntimeError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000705 # Verify that testzip() doesn't swallow inappropriate exceptions.
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000706 data = io.BytesIO()
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000707 zipf = zipfile.ZipFile(data, mode="w")
708 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
709 zipf.close()
710
711 # This is correct; calling .read on a closed ZipFile should throw
712 # a RuntimeError, and so should calling .testzip. An earlier
713 # version of .testzip would swallow this exception (and any other)
714 # and report that the first file in the archive was corrupt.
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000715 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
716 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000717 self.assertRaises(RuntimeError, zipf.testzip)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000718 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Guido van Rossum814661e2007-07-18 22:07:29 +0000719 open(TESTFN, 'w').write('zipfile test data')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000720 self.assertRaises(RuntimeError, zipf.write, TESTFN)
721
Ezio Melottiafd0d112009-07-15 17:17:17 +0000722 def test_bad_constructor_mode(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000723 # Check that bad modes passed to ZipFile constructor are caught
724 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
725
Ezio Melottiafd0d112009-07-15 17:17:17 +0000726 def test_bad_open_mode(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000727 # Check that bad modes passed to ZipFile.open are caught
728 zipf = zipfile.ZipFile(TESTFN, mode="w")
729 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
730 zipf.close()
731 zipf = zipfile.ZipFile(TESTFN, mode="r")
732 # read the data to make sure the file is there
733 zipf.read("foo.txt")
734 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
735 zipf.close()
736
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
740 zipf = zipfile.ZipFile(TESTFN, mode="w")
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):
Guido van Rossum814661e2007-07-18 22:07:29 +0000745 self.assertEqual(f.read(0), b'')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000746
Guido van Rossum814661e2007-07-18 22:07:29 +0000747 self.assertEqual(f.read(), b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000748 zipf.close()
749
Ezio Melottiafd0d112009-07-15 17:17:17 +0000750 def test_open_non_existent_item(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000751 # Check that attempting to call open() for an item that doesn't
752 # exist in the archive raises a RuntimeError
753 zipf = zipfile.ZipFile(TESTFN, mode="w")
754 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
755
Ezio Melottiafd0d112009-07-15 17:17:17 +0000756 def test_bad_compression_mode(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000757 # Check that bad compression methods passed to ZipFile.open are caught
758 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
759
Ezio Melottiafd0d112009-07-15 17:17:17 +0000760 def test_null_byte_in_filename(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000761 # Check that a filename containing a null byte is properly terminated
762 zipf = zipfile.ZipFile(TESTFN, mode="w")
Guido van Rossum814661e2007-07-18 22:07:29 +0000763 zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000764 self.assertEqual(zipf.namelist(), ['foo.txt'])
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000765
Ezio Melottiafd0d112009-07-15 17:17:17 +0000766 def test_struct_sizes(self):
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000767 # check that ZIP internal structure sizes are calculated correctly
768 self.assertEqual(zipfile.sizeEndCentDir, 22)
769 self.assertEqual(zipfile.sizeCentralDir, 46)
770 self.assertEqual(zipfile.sizeEndCentDir64, 56)
771 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
772
Ezio Melottiafd0d112009-07-15 17:17:17 +0000773 def test_comments(self):
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000774 # This test checks that comments on the archive are handled properly
775
776 # check default comment is empty
777 zipf = zipfile.ZipFile(TESTFN, mode="w")
778 self.assertEqual(zipf.comment, b'')
779 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
780 zipf.close()
781 zipfr = zipfile.ZipFile(TESTFN, mode="r")
782 self.assertEqual(zipfr.comment, b'')
783 zipfr.close()
784
785 # check a simple short comment
786 comment = b'Bravely taking to his feet, he beat a very brave retreat.'
787 zipf = zipfile.ZipFile(TESTFN, mode="w")
788 zipf.comment = comment
789 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
790 zipf.close()
791 zipfr = zipfile.ZipFile(TESTFN, mode="r")
792 self.assertEqual(zipfr.comment, comment)
793 zipfr.close()
794
795 # check a comment of max length
796 comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
797 comment2 = comment2.encode("ascii")
798 zipf = zipfile.ZipFile(TESTFN, mode="w")
799 zipf.comment = comment2
800 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
801 zipf.close()
802 zipfr = zipfile.ZipFile(TESTFN, mode="r")
803 self.assertEqual(zipfr.comment, comment2)
804 zipfr.close()
805
806 # check a comment that is too long is truncated
807 zipf = zipfile.ZipFile(TESTFN, mode="w")
808 zipf.comment = comment2 + b'oops'
809 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
810 zipf.close()
811 zipfr = zipfile.ZipFile(TESTFN, mode="r")
812 self.assertEqual(zipfr.comment, comment2)
813 zipfr.close()
814
Guido van Rossumd8faa362007-04-27 19:54:29 +0000815 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000816 unlink(TESTFN)
817 unlink(TESTFN2)
818
Thomas Wouterscf297e42007-02-23 15:07:44 +0000819
820class DecryptionTests(unittest.TestCase):
821 # This test checks that ZIP decryption works. Since the library does not
822 # support encryption at the moment, we use a pre-generated encrypted
823 # ZIP file
824
825 data = (
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000826 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
827 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
828 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
829 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
830 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
831 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
832 b'\x00\x00L\x00\x00\x00\x00\x00' )
Christian Heimesfdab48e2008-01-20 09:06:41 +0000833 data2 = (
834 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
835 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
836 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
837 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
838 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
839 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
840 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
841 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Thomas Wouterscf297e42007-02-23 15:07:44 +0000842
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000843 plain = b'zipfile.py encryption test'
Christian Heimesfdab48e2008-01-20 09:06:41 +0000844 plain2 = b'\x00'*512
Thomas Wouterscf297e42007-02-23 15:07:44 +0000845
846 def setUp(self):
847 fp = open(TESTFN, "wb")
848 fp.write(self.data)
849 fp.close()
850 self.zip = zipfile.ZipFile(TESTFN, "r")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000851 fp = open(TESTFN2, "wb")
852 fp.write(self.data2)
853 fp.close()
854 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000855
856 def tearDown(self):
857 self.zip.close()
858 os.unlink(TESTFN)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000859 self.zip2.close()
860 os.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000861
Ezio Melottiafd0d112009-07-15 17:17:17 +0000862 def test_no_password(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000863 # Reading the encrypted file without password
864 # must generate a RunTime exception
865 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000866 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000867
Ezio Melottiafd0d112009-07-15 17:17:17 +0000868 def test_bad_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000869 self.zip.setpassword(b"perl")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000870 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000871 self.zip2.setpassword(b"perl")
872 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000873
Ezio Melottiafd0d112009-07-15 17:17:17 +0000874 def test_good_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000875 self.zip.setpassword(b"python")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000876 self.assertEquals(self.zip.read("test.txt"), self.plain)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000877 self.zip2.setpassword(b"12345")
878 self.assertEquals(self.zip2.read("zero"), self.plain2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000879
Guido van Rossumd8faa362007-04-27 19:54:29 +0000880
881class TestsWithRandomBinaryFiles(unittest.TestCase):
882 def setUp(self):
883 datacount = randint(16, 64)*1024 + randint(1, 1024)
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000884 self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
885 for i in range(datacount))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000886
887 # Make a source file with some lines
888 fp = open(TESTFN, "wb")
889 fp.write(self.data)
890 fp.close()
891
892 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000893 unlink(TESTFN)
894 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000895
Ezio Melottiafd0d112009-07-15 17:17:17 +0000896 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000897 # Create the ZIP archive
898 zipfp = zipfile.ZipFile(f, "w", compression)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000899 zipfp.write(TESTFN, "another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000900 zipfp.write(TESTFN, TESTFN)
901 zipfp.close()
902
Ezio Melottiafd0d112009-07-15 17:17:17 +0000903 def zip_test(self, f, compression):
904 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000905
906 # Read the ZIP archive
907 zipfp = zipfile.ZipFile(f, "r", compression)
908 testdata = zipfp.read(TESTFN)
909 self.assertEqual(len(testdata), len(self.data))
910 self.assertEqual(testdata, self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000911 self.assertEqual(zipfp.read("another.name"), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000912 zipfp.close()
913
Ezio Melottiafd0d112009-07-15 17:17:17 +0000914 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000915 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000916 self.zip_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000917
Ezio Melottiafd0d112009-07-15 17:17:17 +0000918 def zip_open_test(self, f, compression):
919 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000920
921 # Read the ZIP archive
922 zipfp = zipfile.ZipFile(f, "r", compression)
923 zipdata1 = []
924 zipopen1 = zipfp.open(TESTFN)
925 while 1:
926 read_data = zipopen1.read(256)
927 if not read_data:
928 break
929 zipdata1.append(read_data)
930
931 zipdata2 = []
Skip Montanaro7a98be22007-08-16 14:35:24 +0000932 zipopen2 = zipfp.open("another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000933 while 1:
934 read_data = zipopen2.read(256)
935 if not read_data:
936 break
937 zipdata2.append(read_data)
938
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000939 testdata1 = b''.join(zipdata1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000940 self.assertEqual(len(testdata1), len(self.data))
941 self.assertEqual(testdata1, self.data)
942
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000943 testdata2 = b''.join(zipdata2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000944 self.assertEqual(len(testdata1), len(self.data))
945 self.assertEqual(testdata1, self.data)
946 zipfp.close()
947
Ezio Melottiafd0d112009-07-15 17:17:17 +0000948 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000949 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000950 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000951
Ezio Melottiafd0d112009-07-15 17:17:17 +0000952 def zip_random_open_test(self, f, compression):
953 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000954
955 # Read the ZIP archive
956 zipfp = zipfile.ZipFile(f, "r", compression)
957 zipdata1 = []
958 zipopen1 = zipfp.open(TESTFN)
959 while 1:
960 read_data = zipopen1.read(randint(1, 1024))
961 if not read_data:
962 break
963 zipdata1.append(read_data)
964
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000965 testdata = b''.join(zipdata1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000966 self.assertEqual(len(testdata), len(self.data))
967 self.assertEqual(testdata, self.data)
968 zipfp.close()
969
Ezio Melottiafd0d112009-07-15 17:17:17 +0000970 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000971 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000972 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000973
Ezio Melotti76430242009-07-11 18:28:48 +0000974
Guido van Rossumd8faa362007-04-27 19:54:29 +0000975class TestsWithMultipleOpens(unittest.TestCase):
976 def setUp(self):
977 # Create the ZIP archive
978 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
979 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
980 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
981 zipfp.close()
982
Ezio Melottiafd0d112009-07-15 17:17:17 +0000983 def test_same_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000984 # Verify that (when the ZipFile is in control of creating file objects)
985 # multiple open() calls can be made without interfering with each other.
986 zipf = zipfile.ZipFile(TESTFN2, mode="r")
987 zopen1 = zipf.open('ones')
988 zopen2 = zipf.open('ones')
989 data1 = zopen1.read(500)
990 data2 = zopen2.read(500)
991 data1 += zopen1.read(500)
992 data2 += zopen2.read(500)
993 self.assertEqual(data1, data2)
994 zipf.close()
995
Ezio Melottiafd0d112009-07-15 17:17:17 +0000996 def test_different_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000997 # Verify that (when the ZipFile is in control of creating file objects)
998 # multiple open() calls can be made without interfering with each other.
999 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1000 zopen1 = zipf.open('ones')
1001 zopen2 = zipf.open('twos')
1002 data1 = zopen1.read(500)
1003 data2 = zopen2.read(500)
1004 data1 += zopen1.read(500)
1005 data2 += zopen2.read(500)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001006 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1007 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001008 zipf.close()
1009
Ezio Melottiafd0d112009-07-15 17:17:17 +00001010 def test_interleaved(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001011 # Verify that (when the ZipFile is in control of creating file objects)
1012 # multiple open() calls can be made without interfering with each other.
1013 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1014 zopen1 = zipf.open('ones')
1015 data1 = zopen1.read(500)
1016 zopen2 = zipf.open('twos')
1017 data2 = zopen2.read(500)
1018 data1 += zopen1.read(500)
1019 data2 += zopen2.read(500)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001020 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1021 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001022 zipf.close()
1023
1024 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001025 unlink(TESTFN2)
1026
Guido van Rossumd8faa362007-04-27 19:54:29 +00001027
Martin v. Löwis59e47792009-01-24 14:10:07 +00001028class TestWithDirectory(unittest.TestCase):
1029 def setUp(self):
1030 os.mkdir(TESTFN2)
1031
Ezio Melottiafd0d112009-07-15 17:17:17 +00001032 def test_extract_dir(self):
Martin v. Löwis59e47792009-01-24 14:10:07 +00001033 zipf = zipfile.ZipFile(findfile("zipdir.zip"))
1034 zipf.extractall(TESTFN2)
1035 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1036 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1037 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1038
Ezio Melottiafd0d112009-07-15 17:17:17 +00001039 def test_bug_6050(self):
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001040 # Extraction should succeed if directories already exist
1041 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottiafd0d112009-07-15 17:17:17 +00001042 self.test_extract_dir()
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001043
Ezio Melottiafd0d112009-07-15 17:17:17 +00001044 def test_store_dir(self):
Martin v. Löwis59e47792009-01-24 14:10:07 +00001045 os.mkdir(os.path.join(TESTFN2, "x"))
1046 zipf = zipfile.ZipFile(TESTFN, "w")
1047 zipf.write(os.path.join(TESTFN2, "x"), "x")
1048 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1049
1050 def tearDown(self):
1051 shutil.rmtree(TESTFN2)
1052 if os.path.exists(TESTFN):
Ezio Melotti76430242009-07-11 18:28:48 +00001053 unlink(TESTFN)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001054
Guido van Rossumd8faa362007-04-27 19:54:29 +00001055
1056class UniversalNewlineTests(unittest.TestCase):
1057 def setUp(self):
Guido van Rossum9c627722007-08-27 18:31:48 +00001058 self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001059 for i in range(FIXEDTEST_SIZE)]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001060 self.seps = ('\r', '\r\n', '\n')
1061 self.arcdata, self.arcfiles = {}, {}
1062 for n, s in enumerate(self.seps):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001063 b = s.encode("ascii")
1064 self.arcdata[s] = b.join(self.line_gen) + b
Guido van Rossumd8faa362007-04-27 19:54:29 +00001065 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001066 f = open(self.arcfiles[s], "wb")
1067 try:
1068 f.write(self.arcdata[s])
1069 finally:
1070 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001071
Ezio Melottiafd0d112009-07-15 17:17:17 +00001072 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001073 # Create the ZIP archive
1074 zipfp = zipfile.ZipFile(f, "w", compression)
1075 for fn in self.arcfiles.values():
1076 zipfp.write(fn, fn)
1077 zipfp.close()
1078
Ezio Melottiafd0d112009-07-15 17:17:17 +00001079 def read_test(self, f, compression):
1080 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001081
1082 # Read the ZIP archive
1083 zipfp = zipfile.ZipFile(f, "r")
1084 for sep, fn in self.arcfiles.items():
1085 zipdata = zipfp.open(fn, "rU").read()
1086 self.assertEqual(self.arcdata[sep], zipdata)
1087
1088 zipfp.close()
1089
Ezio Melottiafd0d112009-07-15 17:17:17 +00001090 def readline_test(self, f, compression):
1091 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001092
1093 # Read the ZIP archive
1094 zipfp = zipfile.ZipFile(f, "r")
1095 for sep, fn in self.arcfiles.items():
1096 zipopen = zipfp.open(fn, "rU")
1097 for line in self.line_gen:
1098 linedata = zipopen.readline()
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001099 self.assertEqual(linedata, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001100
1101 zipfp.close()
1102
Ezio Melottiafd0d112009-07-15 17:17:17 +00001103 def readlines_test(self, f, compression):
1104 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001105
1106 # Read the ZIP archive
1107 zipfp = zipfile.ZipFile(f, "r")
1108 for sep, fn in self.arcfiles.items():
1109 ziplines = zipfp.open(fn, "rU").readlines()
1110 for line, zipline in zip(self.line_gen, ziplines):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001111 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001112
1113 zipfp.close()
1114
Ezio Melottiafd0d112009-07-15 17:17:17 +00001115 def iterlines_test(self, f, compression):
1116 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001117
1118 # Read the ZIP archive
1119 zipfp = zipfile.ZipFile(f, "r")
1120 for sep, fn in self.arcfiles.items():
1121 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001122 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001123
1124 zipfp.close()
1125
Ezio Melottiafd0d112009-07-15 17:17:17 +00001126 def test_read_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001127 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001128 self.read_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001129
Ezio Melottiafd0d112009-07-15 17:17:17 +00001130 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001131 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001132 self.readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001133
Ezio Melottiafd0d112009-07-15 17:17:17 +00001134 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001135 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001136 self.readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001137
Ezio Melottiafd0d112009-07-15 17:17:17 +00001138 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001139 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001140 self.iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001141
Ezio Melotti76430242009-07-11 18:28:48 +00001142 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001143 def test_read_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001144 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001145 self.read_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001146
Ezio Melotti76430242009-07-11 18:28:48 +00001147 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001148 def test_readline_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001149 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001150 self.readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001151
Ezio Melotti76430242009-07-11 18:28:48 +00001152 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001153 def test_readlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001154 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001155 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001156
Ezio Melotti76430242009-07-11 18:28:48 +00001157 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001158 def test_iterlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001159 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001160 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001161
1162 def tearDown(self):
1163 for sep, fn in self.arcfiles.items():
1164 os.remove(fn)
Ezio Melotti76430242009-07-11 18:28:48 +00001165 unlink(TESTFN)
1166 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001167
1168
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001169def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +00001170 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1171 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti76430242009-07-11 18:28:48 +00001172 TestWithDirectory, UniversalNewlineTests,
1173 TestsWithRandomBinaryFiles)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001174
1175if __name__ == "__main__":
1176 test_main()