blob: a36b010bd882c9ee9ff94fc2226457610a13db4b [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
Victor Stinnerff1d2f42011-05-18 13:43:23 +02006import io
7import os
8import shutil
9import struct
10import sys
11import unittest
12import zipfile
Tim Petersa45cacf2004-08-20 03:47:14 +000013
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000014from tempfile import TemporaryFile
Guido van Rossumd8faa362007-04-27 19:54:29 +000015from random import randint, random
Tim Petersa19a1682001-03-29 04:36:09 +000016
Benjamin Petersonee8712c2008-05-20 21:35:26 +000017import test.support as support
Martin v. Löwis59e47792009-01-24 14:10:07 +000018from test.support import TESTFN, run_unittest, findfile
Guido van Rossum368f04a2000-04-10 13:23:04 +000019
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000020TESTFN2 = TESTFN + "2"
Martin v. Löwis59e47792009-01-24 14:10:07 +000021TESTFNDIR = TESTFN + "d"
Guido van Rossumb5a755e2007-07-18 18:15:48 +000022FIXEDTEST_SIZE = 1000
Victor Stinnerff1d2f42011-05-18 13:43:23 +020023DATAFILES_DIR = 'zipfile_datafiles'
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
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000030class TestsWithSourceFile(unittest.TestCase):
31 def setUp(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +000032 self.line_gen = (bytes("Zipfile test line %d. random float: %f" %
Guido van Rossum9c627722007-08-27 18:31:48 +000033 (i, random()), "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +000034 for i in range(FIXEDTEST_SIZE))
35 self.data = b'\n'.join(self.line_gen) + b'\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000036
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000037 # Make a source file with some lines
38 fp = open(TESTFN, "wb")
39 fp.write(self.data)
40 fp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000041
Guido van Rossumd8faa362007-04-27 19:54:29 +000042 def makeTestArchive(self, f, compression):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000043 # Create the ZIP archive
44 zipfp = zipfile.ZipFile(f, "w", compression)
Skip Montanaro7a98be22007-08-16 14:35:24 +000045 zipfp.write(TESTFN, "another.name")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000046 zipfp.write(TESTFN, TESTFN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000047 zipfp.writestr("strfile", self.data)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000048 zipfp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000049
Guido van Rossumd8faa362007-04-27 19:54:29 +000050 def zipTest(self, f, compression):
51 self.makeTestArchive(f, compression)
52
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000053 # Read the ZIP archive
54 zipfp = zipfile.ZipFile(f, "r", compression)
55 self.assertEqual(zipfp.read(TESTFN), self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +000056 self.assertEqual(zipfp.read("another.name"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000057 self.assertEqual(zipfp.read("strfile"), self.data)
58
59 # Print the ZIP directory
Guido van Rossumd6ca5462007-05-22 01:29:33 +000060 fp = io.StringIO()
61 zipfp.printdir(file=fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062
63 directory = fp.getvalue()
64 lines = directory.splitlines()
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000065 self.assertEqual(len(lines), 4) # Number of files + header
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066
Georg Brandlab91fde2009-08-13 08:51:18 +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()
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000072 self.assertEqual(fn, 'another.name')
Thomas Wouters0e3f5912006-08-11 14:57:12 +000073 # XXX: timestamp is not tested
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000074 self.assertEqual(size, str(len(self.data)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075
76 # Check the namelist
77 names = zipfp.namelist()
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000078 self.assertEqual(len(names), 3)
Georg Brandlab91fde2009-08-13 08:51:18 +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 ]
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000086 self.assertEqual(len(names), 3)
Georg Brandlab91fde2009-08-13 08:51:18 +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:
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000091 self.assertEqual(i.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000092
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)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000096 self.assertEqual(info.filename, nm)
97 self.assertEqual(info.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000098
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
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000103 def testStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000104 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000105 self.zipTest(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000106
Guido van Rossumd8faa362007-04-27 19:54:29 +0000107 def zipOpenTest(self, f, compression):
108 self.makeTestArchive(f, compression)
109
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
132 def testOpenStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000133 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000134 self.zipOpenTest(f, zipfile.ZIP_STORED)
135
Georg Brandlb533e262008-05-25 18:19:30 +0000136 def testOpenViaZipInfo(self):
137 # 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()
Georg Brandlab91fde2009-08-13 08:51:18 +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)
Georg Brandlab91fde2009-08-13 08:51:18 +0000152 self.assertTrue(data == b"foobar" or data == b"barfoo")
Georg Brandlb533e262008-05-25 18:19:30 +0000153 zipfp.close()
154
Guido van Rossumd8faa362007-04-27 19:54:29 +0000155 def zipRandomOpenTest(self, f, compression):
156 self.makeTestArchive(f, compression)
157
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
171 def testRandomOpenStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000172 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000173 self.zipRandomOpenTest(f, zipfile.ZIP_STORED)
174
175 def zipReadlineTest(self, f, compression):
176 self.makeTestArchive(f, compression)
177
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
187 def zipReadlinesTest(self, f, compression):
188 self.makeTestArchive(f, compression)
189
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
198 def zipIterlinesTest(self, f, compression):
199 self.makeTestArchive(f, compression)
200
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
208 def testReadlineStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000209 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000210 self.zipReadlineTest(f, zipfile.ZIP_STORED)
211
212 def testReadlinesStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000213 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000214 self.zipReadlinesTest(f, zipfile.ZIP_STORED)
215
216 def testIterlinesStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000217 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000218 self.zipIterlinesTest(f, zipfile.ZIP_STORED)
219
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000220 if zlib:
221 def testDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000222 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000223 self.zipTest(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000224
Guido van Rossumd8faa362007-04-27 19:54:29 +0000225 def testOpenDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000226 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000227 self.zipOpenTest(f, zipfile.ZIP_DEFLATED)
228
229 def testRandomOpenDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000230 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000231 self.zipRandomOpenTest(f, zipfile.ZIP_DEFLATED)
232
233 def testReadlineDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000234 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000235 self.zipReadlineTest(f, zipfile.ZIP_DEFLATED)
236
237 def testReadlinesDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000238 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000239 self.zipReadlinesTest(f, zipfile.ZIP_DEFLATED)
240
241 def testIterlinesDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000242 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000243 self.zipIterlinesTest(f, zipfile.ZIP_DEFLATED)
244
245 def testLowCompression(self):
246 # Checks for cases where compressed data is larger than original
247 # Create the ZIP archive
248 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
249 zipfp.writestr("strfile", '12')
250 zipfp.close()
251
252 # Get an open object for strfile
253 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED)
254 openobj = zipfp.open("strfile")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000255 self.assertEqual(openobj.read(1), b'1')
256 self.assertEqual(openobj.read(1), b'2')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000257
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000258 def testAbsoluteArcnames(self):
259 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
260 zipfp.write(TESTFN, "/absolute")
261 zipfp.close()
262
263 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
264 self.assertEqual(zipfp.namelist(), ["absolute"])
265 zipfp.close()
Tim Peters32cbc962006-02-20 21:42:18 +0000266
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000267 def testAppendToZipFile(self):
268 # Test appending to an existing zipfile
269 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
270 zipfp.write(TESTFN, TESTFN)
271 zipfp.close()
272 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
273 zipfp.writestr("strfile", self.data)
274 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
275 zipfp.close()
276
277 def testAppendToNonZipFile(self):
278 # Test appending to an existing file that is not a zipfile
279 # NOTE: this test fails if len(d) < 22 because of the first
280 # line "fpin.seek(-22, 2)" in _EndRecData
Guido van Rossum9c627722007-08-27 18:31:48 +0000281 d = b'I am not a ZipFile!'*10
Guido van Rossum814661e2007-07-18 22:07:29 +0000282 f = open(TESTFN2, 'wb')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000283 f.write(d)
284 f.close()
285 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
286 zipfp.write(TESTFN, TESTFN)
287 zipfp.close()
288
Guido van Rossum814661e2007-07-18 22:07:29 +0000289 f = open(TESTFN2, 'rb')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000290 f.seek(len(d))
291 zipfp = zipfile.ZipFile(f, "r")
292 self.assertEqual(zipfp.namelist(), [TESTFN])
293 zipfp.close()
294 f.close()
295
296 def test_WriteDefaultName(self):
297 # Check that calling ZipFile.write without arcname specified produces the expected result
298 zipfp = zipfile.ZipFile(TESTFN2, "w")
299 zipfp.write(TESTFN)
Guido van Rossum814661e2007-07-18 22:07:29 +0000300 self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read())
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000301 zipfp.close()
302
303 def test_PerFileCompression(self):
304 # Check that files within a Zip archive can have different compression options
305 zipfp = zipfile.ZipFile(TESTFN2, "w")
306 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
307 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
308 sinfo = zipfp.getinfo('storeme')
309 dinfo = zipfp.getinfo('deflateme')
310 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
311 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
312 zipfp.close()
313
314 def test_WriteToReadonly(self):
315 # Check that trying to call write() on a readonly ZipFile object
316 # raises a RuntimeError
317 zipf = zipfile.ZipFile(TESTFN2, mode="w")
318 zipf.writestr("somefile.txt", "bogus")
319 zipf.close()
320 zipf = zipfile.ZipFile(TESTFN2, mode="r")
321 self.assertRaises(RuntimeError, zipf.write, TESTFN)
322 zipf.close()
323
Christian Heimes790c8232008-01-07 21:14:23 +0000324 def testExtract(self):
325 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
326 for fpath, fdata in SMALL_TEST_DATA:
327 zipfp.writestr(fpath, fdata)
328 zipfp.close()
329
330 zipfp = zipfile.ZipFile(TESTFN2, "r")
331 for fpath, fdata in SMALL_TEST_DATA:
332 writtenfile = zipfp.extract(fpath)
333
334 # make sure it was written to the right place
335 if os.path.isabs(fpath):
336 correctfile = os.path.join(os.getcwd(), fpath[1:])
337 else:
338 correctfile = os.path.join(os.getcwd(), fpath)
Christian Heimesaf98da12008-01-27 15:18:18 +0000339 correctfile = os.path.normpath(correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000340
341 self.assertEqual(writtenfile, correctfile)
342
343 # make sure correct data is in correct file
344 self.assertEqual(fdata.encode(), open(writtenfile, "rb").read())
345
346 os.remove(writtenfile)
347
348 zipfp.close()
349
350 # remove the test file subdirectories
351 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
352
353 def testExtractAll(self):
354 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
355 for fpath, fdata in SMALL_TEST_DATA:
356 zipfp.writestr(fpath, fdata)
357 zipfp.close()
358
359 zipfp = zipfile.ZipFile(TESTFN2, "r")
360 zipfp.extractall()
361 for fpath, fdata in SMALL_TEST_DATA:
362 if os.path.isabs(fpath):
363 outfile = os.path.join(os.getcwd(), fpath[1:])
364 else:
365 outfile = os.path.join(os.getcwd(), fpath)
366
367 self.assertEqual(fdata.encode(), open(outfile, "rb").read())
368
369 os.remove(outfile)
370
371 zipfp.close()
372
373 # remove the test file subdirectories
374 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
375
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000376 def zip_test_writestr_permissions(self, f, compression):
377 # Make sure that writestr creates files with mode 0600,
378 # when it is passed a name rather than a ZipInfo instance.
379
380 self.makeTestArchive(f, compression)
381 zipfp = zipfile.ZipFile(f, "r")
382 zinfo = zipfp.getinfo('strfile')
383 self.assertEqual(zinfo.external_attr, 0o600 << 16)
384
385 def test_writestr_permissions(self):
386 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
387 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
388
Georg Brandldf475152009-08-13 09:05:38 +0000389 def test_writestr_extended_local_header_issue1202(self):
390 orig_zip = zipfile.ZipFile(TESTFN2, 'w')
391 for data in 'abcdefghijklmnop':
392 zinfo = zipfile.ZipInfo(data)
393 zinfo.flag_bits |= 0x08 # Include an extended local header.
394 orig_zip.writestr(zinfo, data)
395 orig_zip.close()
396
Victor Stinnerff1d2f42011-05-18 13:43:23 +0200397 def test_unicode_filenames(self):
398 if __name__ == '__main__':
399 myfile = sys.argv[0]
400 else:
401 myfile = __file__
402
403 mydir = os.path.dirname(myfile) or os.curdir
404 fname = os.path.join(mydir, 'zip_cp437_header.zip')
405
Victor Stinnerff1d2f42011-05-18 13:43:23 +0200406 zipfp = zipfile.ZipFile(fname)
407 try:
408 zipfp.extractall()
409 finally:
410 zipfp.close()
411
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000412 def tearDown(self):
Victor Stinnerff1d2f42011-05-18 13:43:23 +0200413 support.unlink(TESTFN)
414 support.unlink(TESTFN2)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000415
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000416class TestZip64InSmallFiles(unittest.TestCase):
417 # These tests test the ZIP64 functionality without using large files,
418 # see test_zipfile64 for proper tests.
419
420 def setUp(self):
421 self._limit = zipfile.ZIP64_LIMIT
422 zipfile.ZIP64_LIMIT = 5
423
Guido van Rossum9c627722007-08-27 18:31:48 +0000424 line_gen = (bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000425 for i in range(0, FIXEDTEST_SIZE))
426 self.data = b'\n'.join(line_gen)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000427
428 # Make a source file with some lines
429 fp = open(TESTFN, "wb")
430 fp.write(self.data)
431 fp.close()
432
433 def largeFileExceptionTest(self, f, compression):
434 zipfp = zipfile.ZipFile(f, "w", compression)
435 self.assertRaises(zipfile.LargeZipFile,
Skip Montanaro7a98be22007-08-16 14:35:24 +0000436 zipfp.write, TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000437 zipfp.close()
438
439 def largeFileExceptionTest2(self, f, compression):
440 zipfp = zipfile.ZipFile(f, "w", compression)
441 self.assertRaises(zipfile.LargeZipFile,
Skip Montanaro7a98be22007-08-16 14:35:24 +0000442 zipfp.writestr, "another.name", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000443 zipfp.close()
444
445 def testLargeFileException(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000446 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000447 self.largeFileExceptionTest(f, zipfile.ZIP_STORED)
448 self.largeFileExceptionTest2(f, zipfile.ZIP_STORED)
449
450 def zipTest(self, f, compression):
451 # Create the ZIP archive
452 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000453 zipfp.write(TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000454 zipfp.write(TESTFN, TESTFN)
455 zipfp.writestr("strfile", self.data)
456 zipfp.close()
457
458 # Read the ZIP archive
459 zipfp = zipfile.ZipFile(f, "r", compression)
460 self.assertEqual(zipfp.read(TESTFN), self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000461 self.assertEqual(zipfp.read("another.name"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000462 self.assertEqual(zipfp.read("strfile"), self.data)
463
464 # Print the ZIP directory
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000465 fp = io.StringIO()
466 zipfp.printdir(fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000467
468 directory = fp.getvalue()
469 lines = directory.splitlines()
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000470 self.assertEqual(len(lines), 4) # Number of files + header
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000471
Georg Brandlab91fde2009-08-13 08:51:18 +0000472 self.assertTrue('File Name' in lines[0])
473 self.assertTrue('Modified' in lines[0])
474 self.assertTrue('Size' in lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000475
476 fn, date, time, size = lines[1].split()
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000477 self.assertEqual(fn, 'another.name')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000478 # XXX: timestamp is not tested
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000479 self.assertEqual(size, str(len(self.data)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000480
481 # Check the namelist
482 names = zipfp.namelist()
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000483 self.assertEqual(len(names), 3)
Georg Brandlab91fde2009-08-13 08:51:18 +0000484 self.assertTrue(TESTFN in names)
485 self.assertTrue("another.name" in names)
486 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000487
488 # Check infolist
489 infos = zipfp.infolist()
490 names = [ i.filename for i in infos ]
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000491 self.assertEqual(len(names), 3)
Georg Brandlab91fde2009-08-13 08:51:18 +0000492 self.assertTrue(TESTFN in names)
493 self.assertTrue("another.name" in names)
494 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000495 for i in infos:
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000496 self.assertEqual(i.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000497
498 # check getinfo
Skip Montanaro7a98be22007-08-16 14:35:24 +0000499 for nm in (TESTFN, "another.name", "strfile"):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000500 info = zipfp.getinfo(nm)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +0000501 self.assertEqual(info.filename, nm)
502 self.assertEqual(info.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000503
504 # Check that testzip doesn't raise an exception
505 zipfp.testzip()
506
507
508 zipfp.close()
509
510 def testStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000511 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000512 self.zipTest(f, zipfile.ZIP_STORED)
513
514
515 if zlib:
516 def testDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000517 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000518 self.zipTest(f, zipfile.ZIP_DEFLATED)
519
520 def testAbsoluteArcnames(self):
521 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True)
522 zipfp.write(TESTFN, "/absolute")
523 zipfp.close()
524
525 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
526 self.assertEqual(zipfp.namelist(), ["absolute"])
527 zipfp.close()
528
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000529 def tearDown(self):
530 zipfile.ZIP64_LIMIT = self._limit
531 os.remove(TESTFN)
532 os.remove(TESTFN2)
533
534class PyZipFileTests(unittest.TestCase):
535 def testWritePyfile(self):
536 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
537 fn = __file__
538 if fn.endswith('.pyc') or fn.endswith('.pyo'):
539 fn = fn[:-1]
540
541 zipfp.writepy(fn)
542
543 bn = os.path.basename(fn)
Georg Brandlab91fde2009-08-13 08:51:18 +0000544 self.assertTrue(bn not in zipfp.namelist())
545 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000546 zipfp.close()
547
548
549 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
550 fn = __file__
551 if fn.endswith('.pyc') or fn.endswith('.pyo'):
552 fn = fn[:-1]
553
554 zipfp.writepy(fn, "testpackage")
555
556 bn = "%s/%s"%("testpackage", os.path.basename(fn))
Georg Brandlab91fde2009-08-13 08:51:18 +0000557 self.assertTrue(bn not in zipfp.namelist())
558 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000559 zipfp.close()
560
561 def testWritePythonPackage(self):
562 import email
563 packagedir = os.path.dirname(email.__file__)
564
565 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
566 zipfp.writepy(packagedir)
567
568 # Check for a couple of modules at different levels of the hieararchy
569 names = zipfp.namelist()
Georg Brandlab91fde2009-08-13 08:51:18 +0000570 self.assertTrue('email/__init__.pyo' in names or 'email/__init__.pyc' in names)
571 self.assertTrue('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000572
573 def testWritePythonDirectory(self):
574 os.mkdir(TESTFN2)
575 try:
576 fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000577 fp.write("print(42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000578 fp.close()
579
580 fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000581 fp.write("print(42 * 42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000582 fp.close()
583
584 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
585 fp.write("bla bla bla\n")
586 fp.close()
587
588 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
589 zipfp.writepy(TESTFN2)
590
591 names = zipfp.namelist()
Georg Brandlab91fde2009-08-13 08:51:18 +0000592 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
593 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
594 self.assertTrue('mod2.txt' not in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000595
596 finally:
597 shutil.rmtree(TESTFN2)
598
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000599 def testWriteNonPyfile(self):
600 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
Guido van Rossum814661e2007-07-18 22:07:29 +0000601 open(TESTFN, 'w').write('most definitely not a python file')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000602 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
603 os.remove(TESTFN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000604
605
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000606class OtherTests(unittest.TestCase):
Antoine Pitrou5f2a7bc2010-08-12 15:30:13 +0000607 zips_with_bad_crc = {
608 zipfile.ZIP_STORED: (
609 b'PK\003\004\024\0\0\0\0\0 \213\212;:r'
610 b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af'
611 b'ilehello,AworldP'
612 b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:'
613 b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0'
614 b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi'
615 b'lePK\005\006\0\0\0\0\001\0\001\0003\000'
616 b'\0\0/\0\0\0\0\0'),
617 zipfile.ZIP_DEFLATED: (
618 b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA'
619 b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
620 b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0'
621 b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n'
622 b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05'
623 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00'
624 b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00'
625 b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'),
626 }
627
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000628 def testUnicodeFilenames(self):
629 zf = zipfile.ZipFile(TESTFN, "w")
630 zf.writestr("foo.txt", "Test for unicode filename")
Martin v. Löwis1a9f9002008-05-05 17:50:05 +0000631 zf.writestr("\xf6.txt", "Test for unicode filename")
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000632 zf.close()
Martin v. Löwis1a9f9002008-05-05 17:50:05 +0000633 zf = zipfile.ZipFile(TESTFN, "r")
634 self.assertEqual(zf.filelist[0].filename, "foo.txt")
635 self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
636 zf.close()
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000637
Thomas Wouterscf297e42007-02-23 15:07:44 +0000638 def testCreateNonExistentFileForAppend(self):
639 if os.path.exists(TESTFN):
640 os.unlink(TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000641
Thomas Wouterscf297e42007-02-23 15:07:44 +0000642 filename = 'testfile.txt'
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000643 content = b'hello, world. this is some content.'
Guido van Rossumd8faa362007-04-27 19:54:29 +0000644
Thomas Wouterscf297e42007-02-23 15:07:44 +0000645 try:
646 zf = zipfile.ZipFile(TESTFN, 'a')
647 zf.writestr(filename, content)
648 zf.close()
649 except IOError:
650 self.fail('Could not append data to a non-existent zip file.')
651
Georg Brandlab91fde2009-08-13 08:51:18 +0000652 self.assertTrue(os.path.exists(TESTFN))
Thomas Wouterscf297e42007-02-23 15:07:44 +0000653
654 zf = zipfile.ZipFile(TESTFN, 'r')
655 self.assertEqual(zf.read(filename), content)
656 zf.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000657
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000658 def testCloseErroneousFile(self):
659 # This test checks that the ZipFile constructor closes the file object
660 # it opens if there's an error in the file. If it doesn't, the traceback
661 # holds a reference to the ZipFile object and, indirectly, the file object.
662 # On Windows, this causes the os.unlink() call to fail because the
663 # underlying file is still open. This is SF bug #412214.
664 #
665 fp = open(TESTFN, "w")
666 fp.write("this is not a legal zip file\n")
667 fp.close()
668 try:
669 zf = zipfile.ZipFile(TESTFN)
670 except zipfile.BadZipfile:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000671 pass
672
673 def testIsZipErroneousFile(self):
674 # This test checks that the is_zipfile function correctly identifies
675 # a file that is not a zip file
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000676
677 # - passing a filename
678 with open(TESTFN, "w") as fp:
679 fp.write("this is not a legal zip file\n")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000680 chk = zipfile.is_zipfile(TESTFN)
Georg Brandlab91fde2009-08-13 08:51:18 +0000681 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000682 # - passing a file object
683 with open(TESTFN, "rb") as fp:
684 chk = zipfile.is_zipfile(fp)
Georg Brandlab91fde2009-08-13 08:51:18 +0000685 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000686 # - passing a file-like object
687 fp = io.BytesIO()
688 fp.write(b"this is not a legal zip file\n")
689 chk = zipfile.is_zipfile(fp)
Georg Brandlab91fde2009-08-13 08:51:18 +0000690 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000691 fp.seek(0,0)
692 chk = zipfile.is_zipfile(fp)
Georg Brandlab91fde2009-08-13 08:51:18 +0000693 self.assertTrue(not chk)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000694
695 def testIsZipValidFile(self):
696 # This test checks that the is_zipfile function correctly identifies
697 # a file that is a zip file
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000698
699 # - passing a filename
Guido van Rossumd8faa362007-04-27 19:54:29 +0000700 zipf = zipfile.ZipFile(TESTFN, mode="w")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000701 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000702 zipf.close()
703 chk = zipfile.is_zipfile(TESTFN)
Georg Brandlab91fde2009-08-13 08:51:18 +0000704 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000705 # - passing a file object
706 with open(TESTFN, "rb") as fp:
707 chk = zipfile.is_zipfile(fp)
Georg Brandlab91fde2009-08-13 08:51:18 +0000708 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000709 fp.seek(0,0)
710 zip_contents = fp.read()
711 # - passing a file-like object
712 fp = io.BytesIO()
713 fp.write(zip_contents)
714 chk = zipfile.is_zipfile(fp)
Georg Brandlab91fde2009-08-13 08:51:18 +0000715 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000716 fp.seek(0,0)
717 chk = zipfile.is_zipfile(fp)
Georg Brandlab91fde2009-08-13 08:51:18 +0000718 self.assertTrue(chk)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000719
720 def testNonExistentFileRaisesIOError(self):
721 # make sure we don't raise an AttributeError when a partially-constructed
722 # ZipFile instance is finalized; this tests for regression on SF tracker
723 # bug #403871.
724
725 # The bug we're testing for caused an AttributeError to be raised
726 # when a ZipFile instance was created for a file that did not
727 # exist; the .fp member was not initialized but was needed by the
728 # __del__() method. Since the AttributeError is in the __del__(),
729 # it is ignored, but the user should be sufficiently annoyed by
730 # the message on the output that regression will be noticed
731 # quickly.
732 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
733
R. David Murray93a59652010-01-06 20:12:07 +0000734 def test_empty_file_raises_BadZipFile(self):
735 f = open(TESTFN, 'w')
736 f.close()
737 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
738
739 f = open(TESTFN, 'w')
740 f.write("short file")
741 f.close()
742 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
743
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000744 def testClosedZipRaisesRuntimeError(self):
745 # Verify that testzip() doesn't swallow inappropriate exceptions.
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000746 data = io.BytesIO()
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000747 zipf = zipfile.ZipFile(data, mode="w")
748 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
749 zipf.close()
750
751 # This is correct; calling .read on a closed ZipFile should throw
752 # a RuntimeError, and so should calling .testzip. An earlier
753 # version of .testzip would swallow this exception (and any other)
754 # and report that the first file in the archive was corrupt.
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000755 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
756 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000757 self.assertRaises(RuntimeError, zipf.testzip)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000758 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Guido van Rossum814661e2007-07-18 22:07:29 +0000759 open(TESTFN, 'w').write('zipfile test data')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000760 self.assertRaises(RuntimeError, zipf.write, TESTFN)
761
762 def test_BadConstructorMode(self):
763 # Check that bad modes passed to ZipFile constructor are caught
764 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
765
766 def test_BadOpenMode(self):
767 # Check that bad modes passed to ZipFile.open are caught
768 zipf = zipfile.ZipFile(TESTFN, mode="w")
769 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
770 zipf.close()
771 zipf = zipfile.ZipFile(TESTFN, mode="r")
772 # read the data to make sure the file is there
773 zipf.read("foo.txt")
774 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
775 zipf.close()
776
777 def test_Read0(self):
778 # Check that calling read(0) on a ZipExtFile object returns an empty
779 # string and doesn't advance file pointer
780 zipf = zipfile.ZipFile(TESTFN, mode="w")
781 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
782 # read the data to make sure the file is there
783 f = zipf.open("foo.txt")
784 for i in range(FIXEDTEST_SIZE):
Guido van Rossum814661e2007-07-18 22:07:29 +0000785 self.assertEqual(f.read(0), b'')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000786
Guido van Rossum814661e2007-07-18 22:07:29 +0000787 self.assertEqual(f.read(), b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000788 zipf.close()
789
790 def test_OpenNonexistentItem(self):
791 # Check that attempting to call open() for an item that doesn't
792 # exist in the archive raises a RuntimeError
793 zipf = zipfile.ZipFile(TESTFN, mode="w")
794 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
795
796 def test_BadCompressionMode(self):
797 # Check that bad compression methods passed to ZipFile.open are caught
798 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
799
800 def test_NullByteInFilename(self):
801 # Check that a filename containing a null byte is properly terminated
802 zipf = zipfile.ZipFile(TESTFN, mode="w")
Guido van Rossum814661e2007-07-18 22:07:29 +0000803 zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000804 self.assertEqual(zipf.namelist(), ['foo.txt'])
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000805
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000806 def test_StructSizes(self):
807 # check that ZIP internal structure sizes are calculated correctly
808 self.assertEqual(zipfile.sizeEndCentDir, 22)
809 self.assertEqual(zipfile.sizeCentralDir, 46)
810 self.assertEqual(zipfile.sizeEndCentDir64, 56)
811 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
812
813 def testComments(self):
814 # This test checks that comments on the archive are handled properly
815
816 # check default comment is empty
817 zipf = zipfile.ZipFile(TESTFN, mode="w")
818 self.assertEqual(zipf.comment, b'')
819 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
820 zipf.close()
821 zipfr = zipfile.ZipFile(TESTFN, mode="r")
822 self.assertEqual(zipfr.comment, b'')
823 zipfr.close()
824
825 # check a simple short comment
826 comment = b'Bravely taking to his feet, he beat a very brave retreat.'
827 zipf = zipfile.ZipFile(TESTFN, mode="w")
828 zipf.comment = comment
829 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
830 zipf.close()
831 zipfr = zipfile.ZipFile(TESTFN, mode="r")
832 self.assertEqual(zipfr.comment, comment)
833 zipfr.close()
834
835 # check a comment of max length
836 comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
837 comment2 = comment2.encode("ascii")
838 zipf = zipfile.ZipFile(TESTFN, mode="w")
839 zipf.comment = comment2
840 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
841 zipf.close()
842 zipfr = zipfile.ZipFile(TESTFN, mode="r")
843 self.assertEqual(zipfr.comment, comment2)
844 zipfr.close()
845
846 # check a comment that is too long is truncated
847 zipf = zipfile.ZipFile(TESTFN, mode="w")
848 zipf.comment = comment2 + b'oops'
849 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
850 zipf.close()
851 zipfr = zipfile.ZipFile(TESTFN, mode="r")
852 self.assertEqual(zipfr.comment, comment2)
853 zipfr.close()
854
Antoine Pitrou5f2a7bc2010-08-12 15:30:13 +0000855 def check_testzip_with_bad_crc(self, compression):
856 """Tests that files with bad CRCs return their name from testzip."""
857 zipdata = self.zips_with_bad_crc[compression]
858
859 zipf = zipfile.ZipFile(io.BytesIO(zipdata), mode="r")
860 # testzip returns the name of the first corrupt file, or None
861 self.assertEqual('afile', zipf.testzip())
862 zipf.close()
863
864 def test_testzip_with_bad_crc_stored(self):
865 self.check_testzip_with_bad_crc(zipfile.ZIP_STORED)
866
867 if zlib:
868 def test_testzip_with_bad_crc_deflated(self):
869 self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED)
870
871 def check_read_with_bad_crc(self, compression):
872 """Tests that files with bad CRCs raise a BadZipfile exception when read."""
873 zipdata = self.zips_with_bad_crc[compression]
874
875 # Using ZipFile.read()
876 zipf = zipfile.ZipFile(io.BytesIO(zipdata), mode="r")
877 self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile')
878 zipf.close()
879
880 # Using ZipExtFile.read()
881 zipf = zipfile.ZipFile(io.BytesIO(zipdata), mode="r")
882 corrupt_file = zipf.open('afile', 'r')
883 self.assertRaises(zipfile.BadZipfile, corrupt_file.read)
884 corrupt_file.close()
885 zipf.close()
886
887 # Same with small reads (in order to exercise the buffering logic)
888 zipf = zipfile.ZipFile(io.BytesIO(zipdata), mode="r")
889 corrupt_file = zipf.open('afile', 'r')
890 corrupt_file.MIN_READ_SIZE = 2
891 with self.assertRaises(zipfile.BadZipfile):
892 while corrupt_file.read(2):
893 pass
894 corrupt_file.close()
895 zipf.close()
896
897 def test_read_with_bad_crc_stored(self):
898 self.check_read_with_bad_crc(zipfile.ZIP_STORED)
899
900 if zlib:
901 def test_read_with_bad_crc_deflated(self):
902 self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED)
903
Antoine Pitrou1052e392010-09-12 14:55:22 +0000904 def check_read_return_size(self, compression):
905 # Issue #9837: ZipExtFile.read() shouldn't return more bytes
906 # than requested.
907 for test_size in (1, 4095, 4096, 4097, 16384):
908 file_size = test_size + 1
909 junk = b''.join(struct.pack('B', randint(0, 255))
910 for x in range(file_size))
911 zipf = zipfile.ZipFile(io.BytesIO(), "w", compression)
912 try:
913 zipf.writestr('foo', junk)
914 fp = zipf.open('foo', 'r')
915 buf = fp.read(test_size)
916 self.assertEqual(len(buf), test_size)
917 finally:
918 zipf.close()
919
920 def test_read_return_size_stored(self):
921 self.check_read_return_size(zipfile.ZIP_STORED)
922
923 if zlib:
924 def test_read_return_size_deflated(self):
925 self.check_read_return_size(zipfile.ZIP_DEFLATED)
926
Georg Brandlaba97962010-11-26 08:37:46 +0000927 def test_empty_zipfile(self):
928 # Check that creating a file in 'w' or 'a' mode and closing without
929 # adding any files to the archives creates a valid empty ZIP file
930 zipf = zipfile.ZipFile(TESTFN, mode="w")
931 zipf.close()
932 try:
933 zipf = zipfile.ZipFile(TESTFN, mode="r")
Éric Araujoa172e252011-02-02 16:58:43 +0000934 except zipfile.BadZipfile:
Georg Brandlaba97962010-11-26 08:37:46 +0000935 self.fail("Unable to create empty ZIP file in 'w' mode")
936
937 zipf = zipfile.ZipFile(TESTFN, mode="a")
938 zipf.close()
939 try:
940 zipf = zipfile.ZipFile(TESTFN, mode="r")
941 except:
942 self.fail("Unable to create empty ZIP file in 'a' mode")
943
944 def test_open_empty_file(self):
945 # Issue 1710703: Check that opening a file with less than 22 bytes
946 # raises a BadZipfile exception (rather than the previously unhelpful
947 # IOError)
948 f = open(TESTFN, 'w')
949 f.close()
950 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r')
951
Guido van Rossumd8faa362007-04-27 19:54:29 +0000952 def tearDown(self):
953 support.unlink(TESTFN)
954 support.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000955
956class DecryptionTests(unittest.TestCase):
957 # This test checks that ZIP decryption works. Since the library does not
958 # support encryption at the moment, we use a pre-generated encrypted
959 # ZIP file
960
961 data = (
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000962 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
963 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
964 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
965 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
966 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
967 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
968 b'\x00\x00L\x00\x00\x00\x00\x00' )
Christian Heimesfdab48e2008-01-20 09:06:41 +0000969 data2 = (
970 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
971 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
972 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
973 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
974 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
975 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
976 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
977 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Thomas Wouterscf297e42007-02-23 15:07:44 +0000978
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000979 plain = b'zipfile.py encryption test'
Christian Heimesfdab48e2008-01-20 09:06:41 +0000980 plain2 = b'\x00'*512
Thomas Wouterscf297e42007-02-23 15:07:44 +0000981
982 def setUp(self):
983 fp = open(TESTFN, "wb")
984 fp.write(self.data)
985 fp.close()
986 self.zip = zipfile.ZipFile(TESTFN, "r")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000987 fp = open(TESTFN2, "wb")
988 fp.write(self.data2)
989 fp.close()
990 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000991
992 def tearDown(self):
993 self.zip.close()
994 os.unlink(TESTFN)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000995 self.zip2.close()
996 os.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000997
998 def testNoPassword(self):
999 # Reading the encrypted file without password
1000 # must generate a RunTime exception
1001 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +00001002 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001003
1004 def testBadPassword(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001005 self.zip.setpassword(b"perl")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001006 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +00001007 self.zip2.setpassword(b"perl")
1008 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001009
Thomas Wouterscf297e42007-02-23 15:07:44 +00001010 def testGoodPassword(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001011 self.zip.setpassword(b"python")
Ezio Melotti19f2aeb2010-11-21 01:30:29 +00001012 self.assertEqual(self.zip.read("test.txt"), self.plain)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001013 self.zip2.setpassword(b"12345")
Ezio Melotti19f2aeb2010-11-21 01:30:29 +00001014 self.assertEqual(self.zip2.read("zero"), self.plain2)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001015
R. David Murray30f9c8c2010-12-21 21:57:54 +00001016 def test_unicode_password(self):
1017 self.assertRaises(TypeError, self.zip.setpassword, "unicode")
1018 self.assertRaises(TypeError, self.zip.read, "test.txt", "python")
1019 self.assertRaises(TypeError, self.zip.open, "test.txt", pwd="python")
1020 self.assertRaises(TypeError, self.zip.extract, "test.txt", pwd="python")
1021
Guido van Rossumd8faa362007-04-27 19:54:29 +00001022
1023class TestsWithRandomBinaryFiles(unittest.TestCase):
1024 def setUp(self):
1025 datacount = randint(16, 64)*1024 + randint(1, 1024)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001026 self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
1027 for i in range(datacount))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001028
1029 # Make a source file with some lines
1030 fp = open(TESTFN, "wb")
1031 fp.write(self.data)
1032 fp.close()
1033
1034 def tearDown(self):
1035 support.unlink(TESTFN)
1036 support.unlink(TESTFN2)
1037
1038 def makeTestArchive(self, f, compression):
1039 # Create the ZIP archive
1040 zipfp = zipfile.ZipFile(f, "w", compression)
Skip Montanaro7a98be22007-08-16 14:35:24 +00001041 zipfp.write(TESTFN, "another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001042 zipfp.write(TESTFN, TESTFN)
1043 zipfp.close()
1044
1045 def zipTest(self, f, compression):
1046 self.makeTestArchive(f, compression)
1047
1048 # Read the ZIP archive
1049 zipfp = zipfile.ZipFile(f, "r", compression)
1050 testdata = zipfp.read(TESTFN)
1051 self.assertEqual(len(testdata), len(self.data))
1052 self.assertEqual(testdata, self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +00001053 self.assertEqual(zipfp.read("another.name"), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001054 zipfp.close()
1055
1056 def testStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001057 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001058 self.zipTest(f, zipfile.ZIP_STORED)
1059
1060 def zipOpenTest(self, f, compression):
1061 self.makeTestArchive(f, compression)
1062
1063 # Read the ZIP archive
1064 zipfp = zipfile.ZipFile(f, "r", compression)
1065 zipdata1 = []
1066 zipopen1 = zipfp.open(TESTFN)
1067 while 1:
1068 read_data = zipopen1.read(256)
1069 if not read_data:
1070 break
1071 zipdata1.append(read_data)
1072
1073 zipdata2 = []
Skip Montanaro7a98be22007-08-16 14:35:24 +00001074 zipopen2 = zipfp.open("another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001075 while 1:
1076 read_data = zipopen2.read(256)
1077 if not read_data:
1078 break
1079 zipdata2.append(read_data)
1080
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001081 testdata1 = b''.join(zipdata1)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001082 self.assertEqual(len(testdata1), len(self.data))
1083 self.assertEqual(testdata1, self.data)
1084
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001085 testdata2 = b''.join(zipdata2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001086 self.assertEqual(len(testdata1), len(self.data))
1087 self.assertEqual(testdata1, self.data)
1088 zipfp.close()
1089
1090 def testOpenStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001091 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001092 self.zipOpenTest(f, zipfile.ZIP_STORED)
1093
1094 def zipRandomOpenTest(self, f, compression):
1095 self.makeTestArchive(f, compression)
1096
1097 # Read the ZIP archive
1098 zipfp = zipfile.ZipFile(f, "r", compression)
1099 zipdata1 = []
1100 zipopen1 = zipfp.open(TESTFN)
1101 while 1:
1102 read_data = zipopen1.read(randint(1, 1024))
1103 if not read_data:
1104 break
1105 zipdata1.append(read_data)
1106
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001107 testdata = b''.join(zipdata1)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001108 self.assertEqual(len(testdata), len(self.data))
1109 self.assertEqual(testdata, self.data)
1110 zipfp.close()
1111
1112 def testRandomOpenStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001113 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001114 self.zipRandomOpenTest(f, zipfile.ZIP_STORED)
1115
1116class TestsWithMultipleOpens(unittest.TestCase):
1117 def setUp(self):
1118 # Create the ZIP archive
1119 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
1120 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
1121 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
1122 zipfp.close()
1123
1124 def testSameFile(self):
1125 # Verify that (when the ZipFile is in control of creating file objects)
1126 # multiple open() calls can be made without interfering with each other.
1127 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1128 zopen1 = zipf.open('ones')
1129 zopen2 = zipf.open('ones')
1130 data1 = zopen1.read(500)
1131 data2 = zopen2.read(500)
1132 data1 += zopen1.read(500)
1133 data2 += zopen2.read(500)
1134 self.assertEqual(data1, data2)
1135 zipf.close()
1136
1137 def testDifferentFile(self):
1138 # Verify that (when the ZipFile is in control of creating file objects)
1139 # multiple open() calls can be made without interfering with each other.
1140 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1141 zopen1 = zipf.open('ones')
1142 zopen2 = zipf.open('twos')
1143 data1 = zopen1.read(500)
1144 data2 = zopen2.read(500)
1145 data1 += zopen1.read(500)
1146 data2 += zopen2.read(500)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001147 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1148 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001149 zipf.close()
1150
1151 def testInterleaved(self):
1152 # Verify that (when the ZipFile is in control of creating file objects)
1153 # multiple open() calls can be made without interfering with each other.
1154 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1155 zopen1 = zipf.open('ones')
1156 data1 = zopen1.read(500)
1157 zopen2 = zipf.open('twos')
1158 data2 = zopen2.read(500)
1159 data1 += zopen1.read(500)
1160 data2 += zopen2.read(500)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001161 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1162 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001163 zipf.close()
1164
1165 def tearDown(self):
1166 os.remove(TESTFN2)
1167
Martin v. Löwis59e47792009-01-24 14:10:07 +00001168class TestWithDirectory(unittest.TestCase):
1169 def setUp(self):
1170 os.mkdir(TESTFN2)
1171
1172 def testExtractDir(self):
1173 zipf = zipfile.ZipFile(findfile("zipdir.zip"))
1174 zipf.extractall(TESTFN2)
1175 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1176 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1177 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1178
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001179 def test_bug_6050(self):
1180 # Extraction should succeed if directories already exist
1181 os.mkdir(os.path.join(TESTFN2, "a"))
1182 self.testExtractDir()
1183
Martin v. Löwis59e47792009-01-24 14:10:07 +00001184 def testStoreDir(self):
1185 os.mkdir(os.path.join(TESTFN2, "x"))
1186 zipf = zipfile.ZipFile(TESTFN, "w")
1187 zipf.write(os.path.join(TESTFN2, "x"), "x")
1188 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1189
1190 def tearDown(self):
1191 shutil.rmtree(TESTFN2)
1192 if os.path.exists(TESTFN):
1193 os.remove(TESTFN)
1194
Guido van Rossumd8faa362007-04-27 19:54:29 +00001195
1196class UniversalNewlineTests(unittest.TestCase):
1197 def setUp(self):
Guido van Rossum9c627722007-08-27 18:31:48 +00001198 self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001199 for i in range(FIXEDTEST_SIZE)]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001200 self.seps = ('\r', '\r\n', '\n')
1201 self.arcdata, self.arcfiles = {}, {}
1202 for n, s in enumerate(self.seps):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001203 b = s.encode("ascii")
1204 self.arcdata[s] = b.join(self.line_gen) + b
Guido van Rossumd8faa362007-04-27 19:54:29 +00001205 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001206 f = open(self.arcfiles[s], "wb")
1207 try:
1208 f.write(self.arcdata[s])
1209 finally:
1210 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001211
1212 def makeTestArchive(self, f, compression):
1213 # Create the ZIP archive
1214 zipfp = zipfile.ZipFile(f, "w", compression)
1215 for fn in self.arcfiles.values():
1216 zipfp.write(fn, fn)
1217 zipfp.close()
1218
1219 def readTest(self, f, compression):
1220 self.makeTestArchive(f, compression)
1221
1222 # Read the ZIP archive
1223 zipfp = zipfile.ZipFile(f, "r")
1224 for sep, fn in self.arcfiles.items():
1225 zipdata = zipfp.open(fn, "rU").read()
1226 self.assertEqual(self.arcdata[sep], zipdata)
1227
1228 zipfp.close()
1229
1230 def readlineTest(self, f, compression):
1231 self.makeTestArchive(f, compression)
1232
1233 # Read the ZIP archive
1234 zipfp = zipfile.ZipFile(f, "r")
1235 for sep, fn in self.arcfiles.items():
1236 zipopen = zipfp.open(fn, "rU")
1237 for line in self.line_gen:
1238 linedata = zipopen.readline()
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001239 self.assertEqual(linedata, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001240
1241 zipfp.close()
1242
1243 def readlinesTest(self, f, compression):
1244 self.makeTestArchive(f, compression)
1245
1246 # Read the ZIP archive
1247 zipfp = zipfile.ZipFile(f, "r")
1248 for sep, fn in self.arcfiles.items():
1249 ziplines = zipfp.open(fn, "rU").readlines()
1250 for line, zipline in zip(self.line_gen, ziplines):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001251 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001252
1253 zipfp.close()
1254
1255 def iterlinesTest(self, f, compression):
1256 self.makeTestArchive(f, compression)
1257
1258 # Read the ZIP archive
1259 zipfp = zipfile.ZipFile(f, "r")
1260 for sep, fn in self.arcfiles.items():
1261 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001262 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001263
1264 zipfp.close()
1265
1266 def testReadStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001267 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001268 self.readTest(f, zipfile.ZIP_STORED)
1269
1270 def testReadlineStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001271 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001272 self.readlineTest(f, zipfile.ZIP_STORED)
1273
1274 def testReadlinesStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001275 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001276 self.readlinesTest(f, zipfile.ZIP_STORED)
1277
1278 def testIterlinesStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001279 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001280 self.iterlinesTest(f, zipfile.ZIP_STORED)
1281
1282 if zlib:
1283 def testReadDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001284 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001285 self.readTest(f, zipfile.ZIP_DEFLATED)
1286
1287 def testReadlineDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001288 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001289 self.readlineTest(f, zipfile.ZIP_DEFLATED)
1290
1291 def testReadlinesDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001292 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001293 self.readlinesTest(f, zipfile.ZIP_DEFLATED)
1294
1295 def testIterlinesDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001296 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001297 self.iterlinesTest(f, zipfile.ZIP_DEFLATED)
1298
1299 def tearDown(self):
1300 for sep, fn in self.arcfiles.items():
1301 os.remove(fn)
1302 support.unlink(TESTFN)
1303 support.unlink(TESTFN2)
1304
1305
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001306def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +00001307 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1308 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Martin v. Löwis59e47792009-01-24 14:10:07 +00001309 TestWithDirectory,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001310 UniversalNewlineTests, TestsWithRandomBinaryFiles)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001311
1312if __name__ == "__main__":
1313 test_main()