blob: 76be41c187d627ae3c38fdcb8ee477004516c11a [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 Melotti78ea2022009-09-12 18:41:20 +0000310 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000311 def test_per_file_compression(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000312 # Check that files within a Zip archive can have different compression options
313 zipfp = zipfile.ZipFile(TESTFN2, "w")
314 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
315 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
316 sinfo = zipfp.getinfo('storeme')
317 dinfo = zipfp.getinfo('deflateme')
318 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
319 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
320 zipfp.close()
321
Ezio Melottiafd0d112009-07-15 17:17:17 +0000322 def test_write_to_readonly(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000323 # Check that trying to call write() on a readonly ZipFile object
324 # raises a RuntimeError
325 zipf = zipfile.ZipFile(TESTFN2, mode="w")
326 zipf.writestr("somefile.txt", "bogus")
327 zipf.close()
328 zipf = zipfile.ZipFile(TESTFN2, mode="r")
329 self.assertRaises(RuntimeError, zipf.write, TESTFN)
330 zipf.close()
331
Ezio Melottiafd0d112009-07-15 17:17:17 +0000332 def test_extract(self):
Christian Heimes790c8232008-01-07 21:14:23 +0000333 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
334 for fpath, fdata in SMALL_TEST_DATA:
335 zipfp.writestr(fpath, fdata)
336 zipfp.close()
337
338 zipfp = zipfile.ZipFile(TESTFN2, "r")
339 for fpath, fdata in SMALL_TEST_DATA:
340 writtenfile = zipfp.extract(fpath)
341
342 # make sure it was written to the right place
343 if os.path.isabs(fpath):
344 correctfile = os.path.join(os.getcwd(), fpath[1:])
345 else:
346 correctfile = os.path.join(os.getcwd(), fpath)
Christian Heimesaf98da12008-01-27 15:18:18 +0000347 correctfile = os.path.normpath(correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000348
349 self.assertEqual(writtenfile, correctfile)
350
351 # make sure correct data is in correct file
352 self.assertEqual(fdata.encode(), open(writtenfile, "rb").read())
353
354 os.remove(writtenfile)
355
356 zipfp.close()
357
358 # remove the test file subdirectories
359 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
360
Ezio Melottiafd0d112009-07-15 17:17:17 +0000361 def test_extract_all(self):
Christian Heimes790c8232008-01-07 21:14:23 +0000362 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
363 for fpath, fdata in SMALL_TEST_DATA:
364 zipfp.writestr(fpath, fdata)
365 zipfp.close()
366
367 zipfp = zipfile.ZipFile(TESTFN2, "r")
368 zipfp.extractall()
369 for fpath, fdata in SMALL_TEST_DATA:
370 if os.path.isabs(fpath):
371 outfile = os.path.join(os.getcwd(), fpath[1:])
372 else:
373 outfile = os.path.join(os.getcwd(), fpath)
374
375 self.assertEqual(fdata.encode(), open(outfile, "rb").read())
376
377 os.remove(outfile)
378
379 zipfp.close()
380
381 # remove the test file subdirectories
382 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
383
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000384 def zip_test_writestr_permissions(self, f, compression):
385 # Make sure that writestr creates files with mode 0600,
386 # when it is passed a name rather than a ZipInfo instance.
387
Ezio Melottiafd0d112009-07-15 17:17:17 +0000388 self.make_test_archive(f, compression)
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000389 zipfp = zipfile.ZipFile(f, "r")
390 zinfo = zipfp.getinfo('strfile')
391 self.assertEqual(zinfo.external_attr, 0o600 << 16)
392
Ezio Melottiafd0d112009-07-15 17:17:17 +0000393 def test_writestr_permissions(self):
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000394 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
395 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
396
Gregory P. Smithb0d9ca92009-07-07 05:06:04 +0000397 def test_writestr_extended_local_header_issue1202(self):
398 orig_zip = zipfile.ZipFile(TESTFN2, 'w')
399 for data in 'abcdefghijklmnop':
400 zinfo = zipfile.ZipInfo(data)
401 zinfo.flag_bits |= 0x08 # Include an extended local header.
402 orig_zip.writestr(zinfo, data)
403 orig_zip.close()
404
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000405 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000406 unlink(TESTFN)
407 unlink(TESTFN2)
408
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000409
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000410class TestZip64InSmallFiles(unittest.TestCase):
411 # These tests test the ZIP64 functionality without using large files,
412 # see test_zipfile64 for proper tests.
413
414 def setUp(self):
415 self._limit = zipfile.ZIP64_LIMIT
416 zipfile.ZIP64_LIMIT = 5
417
Guido van Rossum9c627722007-08-27 18:31:48 +0000418 line_gen = (bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000419 for i in range(0, FIXEDTEST_SIZE))
420 self.data = b'\n'.join(line_gen)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000421
422 # Make a source file with some lines
423 fp = open(TESTFN, "wb")
424 fp.write(self.data)
425 fp.close()
426
Ezio Melottiafd0d112009-07-15 17:17:17 +0000427 def large_file_exception_test(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000428 zipfp = zipfile.ZipFile(f, "w", compression)
429 self.assertRaises(zipfile.LargeZipFile,
Skip Montanaro7a98be22007-08-16 14:35:24 +0000430 zipfp.write, TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000431 zipfp.close()
432
Ezio Melottiafd0d112009-07-15 17:17:17 +0000433 def large_file_exception_test2(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000434 zipfp = zipfile.ZipFile(f, "w", compression)
435 self.assertRaises(zipfile.LargeZipFile,
Skip Montanaro7a98be22007-08-16 14:35:24 +0000436 zipfp.writestr, "another.name", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000437 zipfp.close()
438
Ezio Melottiafd0d112009-07-15 17:17:17 +0000439 def test_large_file_exception(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000440 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000441 self.large_file_exception_test(f, zipfile.ZIP_STORED)
442 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000443
Ezio Melottiafd0d112009-07-15 17:17:17 +0000444 def zip_test(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000445 # Create the ZIP archive
446 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000447 zipfp.write(TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000448 zipfp.write(TESTFN, TESTFN)
449 zipfp.writestr("strfile", self.data)
450 zipfp.close()
451
452 # Read the ZIP archive
453 zipfp = zipfile.ZipFile(f, "r", compression)
454 self.assertEqual(zipfp.read(TESTFN), self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000455 self.assertEqual(zipfp.read("another.name"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000456 self.assertEqual(zipfp.read("strfile"), self.data)
457
458 # Print the ZIP directory
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000459 fp = io.StringIO()
460 zipfp.printdir(fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000461
462 directory = fp.getvalue()
463 lines = directory.splitlines()
464 self.assertEquals(len(lines), 4) # Number of files + header
465
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000466 self.assertTrue('File Name' in lines[0])
467 self.assertTrue('Modified' in lines[0])
468 self.assertTrue('Size' in lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000469
470 fn, date, time, size = lines[1].split()
471 self.assertEquals(fn, 'another.name')
472 # XXX: timestamp is not tested
473 self.assertEquals(size, str(len(self.data)))
474
475 # Check the namelist
476 names = zipfp.namelist()
477 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000478 self.assertTrue(TESTFN in names)
479 self.assertTrue("another.name" in names)
480 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000481
482 # Check infolist
483 infos = zipfp.infolist()
484 names = [ i.filename for i in infos ]
485 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000486 self.assertTrue(TESTFN in names)
487 self.assertTrue("another.name" in names)
488 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000489 for i in infos:
490 self.assertEquals(i.file_size, len(self.data))
491
492 # check getinfo
Skip Montanaro7a98be22007-08-16 14:35:24 +0000493 for nm in (TESTFN, "another.name", "strfile"):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000494 info = zipfp.getinfo(nm)
495 self.assertEquals(info.filename, nm)
496 self.assertEquals(info.file_size, len(self.data))
497
498 # Check that testzip doesn't raise an exception
499 zipfp.testzip()
500
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000501 zipfp.close()
502
Ezio Melottiafd0d112009-07-15 17:17:17 +0000503 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000504 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000505 self.zip_test(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000506
Ezio Melotti76430242009-07-11 18:28:48 +0000507 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000508 def test_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000509 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000510 self.zip_test(f, zipfile.ZIP_DEFLATED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000511
Ezio Melottiafd0d112009-07-15 17:17:17 +0000512 def test_absolute_arcnames(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000513 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True)
514 zipfp.write(TESTFN, "/absolute")
515 zipfp.close()
516
517 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
518 self.assertEqual(zipfp.namelist(), ["absolute"])
519 zipfp.close()
520
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000521 def tearDown(self):
522 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti76430242009-07-11 18:28:48 +0000523 unlink(TESTFN)
524 unlink(TESTFN2)
525
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000526
527class PyZipFileTests(unittest.TestCase):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000528 def test_write_pyfile(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000529 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
530 fn = __file__
531 if fn.endswith('.pyc') or fn.endswith('.pyo'):
532 fn = fn[:-1]
533
534 zipfp.writepy(fn)
535
536 bn = os.path.basename(fn)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000537 self.assertTrue(bn not in zipfp.namelist())
538 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000539 zipfp.close()
540
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000541 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
542 fn = __file__
543 if fn.endswith('.pyc') or fn.endswith('.pyo'):
544 fn = fn[:-1]
545
546 zipfp.writepy(fn, "testpackage")
547
548 bn = "%s/%s"%("testpackage", os.path.basename(fn))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000549 self.assertTrue(bn not in zipfp.namelist())
550 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000551 zipfp.close()
552
Ezio Melottiafd0d112009-07-15 17:17:17 +0000553 def test_write_python_package(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000554 import email
555 packagedir = os.path.dirname(email.__file__)
556
557 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
558 zipfp.writepy(packagedir)
559
560 # Check for a couple of modules at different levels of the hieararchy
561 names = zipfp.namelist()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000562 self.assertTrue('email/__init__.pyo' in names or 'email/__init__.pyc' in names)
563 self.assertTrue('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000564
Ezio Melottiafd0d112009-07-15 17:17:17 +0000565 def test_write_python_directory(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000566 os.mkdir(TESTFN2)
567 try:
568 fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000569 fp.write("print(42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000570 fp.close()
571
572 fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000573 fp.write("print(42 * 42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000574 fp.close()
575
576 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
577 fp.write("bla bla bla\n")
578 fp.close()
579
580 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
581 zipfp.writepy(TESTFN2)
582
583 names = zipfp.namelist()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000584 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
585 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
586 self.assertTrue('mod2.txt' not in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000587
588 finally:
589 shutil.rmtree(TESTFN2)
590
Ezio Melottiafd0d112009-07-15 17:17:17 +0000591 def test_write_non_pyfile(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000592 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
Guido van Rossum814661e2007-07-18 22:07:29 +0000593 open(TESTFN, 'w').write('most definitely not a python file')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000594 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
595 os.remove(TESTFN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000596
597
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000598class OtherTests(unittest.TestCase):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000599 def test_unicode_filenames(self):
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000600 zf = zipfile.ZipFile(TESTFN, "w")
601 zf.writestr("foo.txt", "Test for unicode filename")
Martin v. Löwis1a9f9002008-05-05 17:50:05 +0000602 zf.writestr("\xf6.txt", "Test for unicode filename")
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000603 zf.close()
Martin v. Löwis1a9f9002008-05-05 17:50:05 +0000604 zf = zipfile.ZipFile(TESTFN, "r")
605 self.assertEqual(zf.filelist[0].filename, "foo.txt")
606 self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
607 zf.close()
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000608
Ezio Melottiafd0d112009-07-15 17:17:17 +0000609 def test_create_non_existent_file_for_append(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000610 if os.path.exists(TESTFN):
611 os.unlink(TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000612
Thomas Wouterscf297e42007-02-23 15:07:44 +0000613 filename = 'testfile.txt'
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000614 content = b'hello, world. this is some content.'
Guido van Rossumd8faa362007-04-27 19:54:29 +0000615
Thomas Wouterscf297e42007-02-23 15:07:44 +0000616 try:
617 zf = zipfile.ZipFile(TESTFN, 'a')
618 zf.writestr(filename, content)
619 zf.close()
620 except IOError:
621 self.fail('Could not append data to a non-existent zip file.')
622
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000623 self.assertTrue(os.path.exists(TESTFN))
Thomas Wouterscf297e42007-02-23 15:07:44 +0000624
625 zf = zipfile.ZipFile(TESTFN, 'r')
626 self.assertEqual(zf.read(filename), content)
627 zf.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000628
Ezio Melottiafd0d112009-07-15 17:17:17 +0000629 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000630 # This test checks that the ZipFile constructor closes the file object
631 # it opens if there's an error in the file. If it doesn't, the traceback
632 # holds a reference to the ZipFile object and, indirectly, the file object.
633 # On Windows, this causes the os.unlink() call to fail because the
634 # underlying file is still open. This is SF bug #412214.
635 #
636 fp = open(TESTFN, "w")
637 fp.write("this is not a legal zip file\n")
638 fp.close()
639 try:
640 zf = zipfile.ZipFile(TESTFN)
641 except zipfile.BadZipfile:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000642 pass
643
Ezio Melottiafd0d112009-07-15 17:17:17 +0000644 def test_is_zip_erroneous_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000645 # This test checks that the is_zipfile function correctly identifies
646 # a file that is not a zip file
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000647
648 # - passing a filename
649 with open(TESTFN, "w") as fp:
650 fp.write("this is not a legal zip file\n")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000651 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000652 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000653 # - passing a file object
654 with open(TESTFN, "rb") as fp:
655 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000656 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000657 # - passing a file-like object
658 fp = io.BytesIO()
659 fp.write(b"this is not a legal zip file\n")
660 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000661 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000662 fp.seek(0,0)
663 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000664 self.assertTrue(not chk)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000665
Ezio Melottiafd0d112009-07-15 17:17:17 +0000666 def test_is_zip_valid_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000667 # This test checks that the is_zipfile function correctly identifies
668 # a file that is a zip file
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000669
670 # - passing a filename
Guido van Rossumd8faa362007-04-27 19:54:29 +0000671 zipf = zipfile.ZipFile(TESTFN, mode="w")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000672 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000673 zipf.close()
674 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000675 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000676 # - passing a file object
677 with open(TESTFN, "rb") as fp:
678 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000679 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000680 fp.seek(0,0)
681 zip_contents = fp.read()
682 # - passing a file-like object
683 fp = io.BytesIO()
684 fp.write(zip_contents)
685 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000686 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000687 fp.seek(0,0)
688 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000689 self.assertTrue(chk)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000690
Ezio Melottiafd0d112009-07-15 17:17:17 +0000691 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000692 # make sure we don't raise an AttributeError when a partially-constructed
693 # ZipFile instance is finalized; this tests for regression on SF tracker
694 # bug #403871.
695
696 # The bug we're testing for caused an AttributeError to be raised
697 # when a ZipFile instance was created for a file that did not
698 # exist; the .fp member was not initialized but was needed by the
699 # __del__() method. Since the AttributeError is in the __del__(),
700 # it is ignored, but the user should be sufficiently annoyed by
701 # the message on the output that regression will be noticed
702 # quickly.
703 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
704
Amaury Forgeot d'Arcbc347802009-07-28 22:18:57 +0000705 def test_empty_file_raises_BadZipFile(self):
706 f = open(TESTFN, 'w')
707 f.close()
708 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
709
710 f = open(TESTFN, 'w')
711 f.write("short file")
712 f.close()
713 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
714
Ezio Melottiafd0d112009-07-15 17:17:17 +0000715 def test_closed_zip_raises_RuntimeError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000716 # Verify that testzip() doesn't swallow inappropriate exceptions.
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000717 data = io.BytesIO()
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000718 zipf = zipfile.ZipFile(data, mode="w")
719 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
720 zipf.close()
721
722 # This is correct; calling .read on a closed ZipFile should throw
723 # a RuntimeError, and so should calling .testzip. An earlier
724 # version of .testzip would swallow this exception (and any other)
725 # and report that the first file in the archive was corrupt.
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000726 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
727 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000728 self.assertRaises(RuntimeError, zipf.testzip)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000729 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Guido van Rossum814661e2007-07-18 22:07:29 +0000730 open(TESTFN, 'w').write('zipfile test data')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000731 self.assertRaises(RuntimeError, zipf.write, TESTFN)
732
Ezio Melottiafd0d112009-07-15 17:17:17 +0000733 def test_bad_constructor_mode(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000734 # Check that bad modes passed to ZipFile constructor are caught
735 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
736
Ezio Melottiafd0d112009-07-15 17:17:17 +0000737 def test_bad_open_mode(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000738 # Check that bad modes passed to ZipFile.open are caught
739 zipf = zipfile.ZipFile(TESTFN, mode="w")
740 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
741 zipf.close()
742 zipf = zipfile.ZipFile(TESTFN, mode="r")
743 # read the data to make sure the file is there
744 zipf.read("foo.txt")
745 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
746 zipf.close()
747
Ezio Melottiafd0d112009-07-15 17:17:17 +0000748 def test_read0(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000749 # Check that calling read(0) on a ZipExtFile object returns an empty
750 # string and doesn't advance file pointer
751 zipf = zipfile.ZipFile(TESTFN, mode="w")
752 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
753 # read the data to make sure the file is there
754 f = zipf.open("foo.txt")
755 for i in range(FIXEDTEST_SIZE):
Guido van Rossum814661e2007-07-18 22:07:29 +0000756 self.assertEqual(f.read(0), b'')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000757
Guido van Rossum814661e2007-07-18 22:07:29 +0000758 self.assertEqual(f.read(), b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000759 zipf.close()
760
Ezio Melottiafd0d112009-07-15 17:17:17 +0000761 def test_open_non_existent_item(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000762 # Check that attempting to call open() for an item that doesn't
763 # exist in the archive raises a RuntimeError
764 zipf = zipfile.ZipFile(TESTFN, mode="w")
765 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
766
Ezio Melottiafd0d112009-07-15 17:17:17 +0000767 def test_bad_compression_mode(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000768 # Check that bad compression methods passed to ZipFile.open are caught
769 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
770
Ezio Melottiafd0d112009-07-15 17:17:17 +0000771 def test_null_byte_in_filename(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000772 # Check that a filename containing a null byte is properly terminated
773 zipf = zipfile.ZipFile(TESTFN, mode="w")
Guido van Rossum814661e2007-07-18 22:07:29 +0000774 zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000775 self.assertEqual(zipf.namelist(), ['foo.txt'])
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000776
Ezio Melottiafd0d112009-07-15 17:17:17 +0000777 def test_struct_sizes(self):
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000778 # check that ZIP internal structure sizes are calculated correctly
779 self.assertEqual(zipfile.sizeEndCentDir, 22)
780 self.assertEqual(zipfile.sizeCentralDir, 46)
781 self.assertEqual(zipfile.sizeEndCentDir64, 56)
782 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
783
Ezio Melottiafd0d112009-07-15 17:17:17 +0000784 def test_comments(self):
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000785 # This test checks that comments on the archive are handled properly
786
787 # check default comment is empty
788 zipf = zipfile.ZipFile(TESTFN, mode="w")
789 self.assertEqual(zipf.comment, b'')
790 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
791 zipf.close()
792 zipfr = zipfile.ZipFile(TESTFN, mode="r")
793 self.assertEqual(zipfr.comment, b'')
794 zipfr.close()
795
796 # check a simple short comment
797 comment = b'Bravely taking to his feet, he beat a very brave retreat.'
798 zipf = zipfile.ZipFile(TESTFN, mode="w")
799 zipf.comment = comment
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, comment)
804 zipfr.close()
805
806 # check a comment of max length
807 comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
808 comment2 = comment2.encode("ascii")
809 zipf = zipfile.ZipFile(TESTFN, mode="w")
810 zipf.comment = comment2
811 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
812 zipf.close()
813 zipfr = zipfile.ZipFile(TESTFN, mode="r")
814 self.assertEqual(zipfr.comment, comment2)
815 zipfr.close()
816
817 # check a comment that is too long is truncated
818 zipf = zipfile.ZipFile(TESTFN, mode="w")
819 zipf.comment = comment2 + b'oops'
820 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
821 zipf.close()
822 zipfr = zipfile.ZipFile(TESTFN, mode="r")
823 self.assertEqual(zipfr.comment, comment2)
824 zipfr.close()
825
Guido van Rossumd8faa362007-04-27 19:54:29 +0000826 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000827 unlink(TESTFN)
828 unlink(TESTFN2)
829
Thomas Wouterscf297e42007-02-23 15:07:44 +0000830
831class DecryptionTests(unittest.TestCase):
832 # This test checks that ZIP decryption works. Since the library does not
833 # support encryption at the moment, we use a pre-generated encrypted
834 # ZIP file
835
836 data = (
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000837 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
838 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
839 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
840 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
841 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
842 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
843 b'\x00\x00L\x00\x00\x00\x00\x00' )
Christian Heimesfdab48e2008-01-20 09:06:41 +0000844 data2 = (
845 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
846 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
847 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
848 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
849 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
850 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
851 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
852 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Thomas Wouterscf297e42007-02-23 15:07:44 +0000853
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000854 plain = b'zipfile.py encryption test'
Christian Heimesfdab48e2008-01-20 09:06:41 +0000855 plain2 = b'\x00'*512
Thomas Wouterscf297e42007-02-23 15:07:44 +0000856
857 def setUp(self):
858 fp = open(TESTFN, "wb")
859 fp.write(self.data)
860 fp.close()
861 self.zip = zipfile.ZipFile(TESTFN, "r")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000862 fp = open(TESTFN2, "wb")
863 fp.write(self.data2)
864 fp.close()
865 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000866
867 def tearDown(self):
868 self.zip.close()
869 os.unlink(TESTFN)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000870 self.zip2.close()
871 os.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000872
Ezio Melottiafd0d112009-07-15 17:17:17 +0000873 def test_no_password(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000874 # Reading the encrypted file without password
875 # must generate a RunTime exception
876 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000877 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000878
Ezio Melottiafd0d112009-07-15 17:17:17 +0000879 def test_bad_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000880 self.zip.setpassword(b"perl")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000881 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000882 self.zip2.setpassword(b"perl")
883 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000884
Ezio Melotti78ea2022009-09-12 18:41:20 +0000885 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000886 def test_good_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000887 self.zip.setpassword(b"python")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000888 self.assertEquals(self.zip.read("test.txt"), self.plain)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000889 self.zip2.setpassword(b"12345")
890 self.assertEquals(self.zip2.read("zero"), self.plain2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000891
Guido van Rossumd8faa362007-04-27 19:54:29 +0000892
893class TestsWithRandomBinaryFiles(unittest.TestCase):
894 def setUp(self):
895 datacount = randint(16, 64)*1024 + randint(1, 1024)
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000896 self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
897 for i in range(datacount))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000898
899 # Make a source file with some lines
900 fp = open(TESTFN, "wb")
901 fp.write(self.data)
902 fp.close()
903
904 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000905 unlink(TESTFN)
906 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000907
Ezio Melottiafd0d112009-07-15 17:17:17 +0000908 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000909 # Create the ZIP archive
910 zipfp = zipfile.ZipFile(f, "w", compression)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000911 zipfp.write(TESTFN, "another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000912 zipfp.write(TESTFN, TESTFN)
913 zipfp.close()
914
Ezio Melottiafd0d112009-07-15 17:17:17 +0000915 def zip_test(self, f, compression):
916 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000917
918 # Read the ZIP archive
919 zipfp = zipfile.ZipFile(f, "r", compression)
920 testdata = zipfp.read(TESTFN)
921 self.assertEqual(len(testdata), len(self.data))
922 self.assertEqual(testdata, self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000923 self.assertEqual(zipfp.read("another.name"), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000924 zipfp.close()
925
Ezio Melottiafd0d112009-07-15 17:17:17 +0000926 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000927 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000928 self.zip_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000929
Ezio Melottiafd0d112009-07-15 17:17:17 +0000930 def zip_open_test(self, f, compression):
931 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000932
933 # Read the ZIP archive
934 zipfp = zipfile.ZipFile(f, "r", compression)
935 zipdata1 = []
936 zipopen1 = zipfp.open(TESTFN)
937 while 1:
938 read_data = zipopen1.read(256)
939 if not read_data:
940 break
941 zipdata1.append(read_data)
942
943 zipdata2 = []
Skip Montanaro7a98be22007-08-16 14:35:24 +0000944 zipopen2 = zipfp.open("another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000945 while 1:
946 read_data = zipopen2.read(256)
947 if not read_data:
948 break
949 zipdata2.append(read_data)
950
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000951 testdata1 = b''.join(zipdata1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000952 self.assertEqual(len(testdata1), len(self.data))
953 self.assertEqual(testdata1, self.data)
954
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000955 testdata2 = b''.join(zipdata2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000956 self.assertEqual(len(testdata1), len(self.data))
957 self.assertEqual(testdata1, self.data)
958 zipfp.close()
959
Ezio Melottiafd0d112009-07-15 17:17:17 +0000960 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000961 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000962 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000963
Ezio Melottiafd0d112009-07-15 17:17:17 +0000964 def zip_random_open_test(self, f, compression):
965 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000966
967 # Read the ZIP archive
968 zipfp = zipfile.ZipFile(f, "r", compression)
969 zipdata1 = []
970 zipopen1 = zipfp.open(TESTFN)
971 while 1:
972 read_data = zipopen1.read(randint(1, 1024))
973 if not read_data:
974 break
975 zipdata1.append(read_data)
976
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000977 testdata = b''.join(zipdata1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000978 self.assertEqual(len(testdata), len(self.data))
979 self.assertEqual(testdata, self.data)
980 zipfp.close()
981
Ezio Melottiafd0d112009-07-15 17:17:17 +0000982 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000983 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000984 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000985
Ezio Melotti76430242009-07-11 18:28:48 +0000986
Ezio Melotti78ea2022009-09-12 18:41:20 +0000987@skipUnless(zlib, "requires zlib")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000988class TestsWithMultipleOpens(unittest.TestCase):
989 def setUp(self):
990 # Create the ZIP archive
991 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
992 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
993 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
994 zipfp.close()
995
Ezio Melottiafd0d112009-07-15 17:17:17 +0000996 def test_same_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('ones')
1002 data1 = zopen1.read(500)
1003 data2 = zopen2.read(500)
1004 data1 += zopen1.read(500)
1005 data2 += zopen2.read(500)
1006 self.assertEqual(data1, data2)
1007 zipf.close()
1008
Ezio Melottiafd0d112009-07-15 17:17:17 +00001009 def test_different_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001010 # Verify that (when the ZipFile is in control of creating file objects)
1011 # multiple open() calls can be made without interfering with each other.
1012 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1013 zopen1 = zipf.open('ones')
1014 zopen2 = zipf.open('twos')
1015 data1 = zopen1.read(500)
1016 data2 = zopen2.read(500)
1017 data1 += zopen1.read(500)
1018 data2 += zopen2.read(500)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001019 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1020 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001021 zipf.close()
1022
Ezio Melottiafd0d112009-07-15 17:17:17 +00001023 def test_interleaved(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001024 # Verify that (when the ZipFile is in control of creating file objects)
1025 # multiple open() calls can be made without interfering with each other.
1026 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1027 zopen1 = zipf.open('ones')
1028 data1 = zopen1.read(500)
1029 zopen2 = zipf.open('twos')
1030 data2 = zopen2.read(500)
1031 data1 += zopen1.read(500)
1032 data2 += zopen2.read(500)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001033 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1034 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001035 zipf.close()
1036
1037 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001038 unlink(TESTFN2)
1039
Guido van Rossumd8faa362007-04-27 19:54:29 +00001040
Martin v. Löwis59e47792009-01-24 14:10:07 +00001041class TestWithDirectory(unittest.TestCase):
1042 def setUp(self):
1043 os.mkdir(TESTFN2)
1044
Ezio Melottiafd0d112009-07-15 17:17:17 +00001045 def test_extract_dir(self):
Martin v. Löwis59e47792009-01-24 14:10:07 +00001046 zipf = zipfile.ZipFile(findfile("zipdir.zip"))
1047 zipf.extractall(TESTFN2)
1048 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1049 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1050 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1051
Ezio Melottiafd0d112009-07-15 17:17:17 +00001052 def test_bug_6050(self):
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001053 # Extraction should succeed if directories already exist
1054 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottiafd0d112009-07-15 17:17:17 +00001055 self.test_extract_dir()
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001056
Ezio Melottiafd0d112009-07-15 17:17:17 +00001057 def test_store_dir(self):
Martin v. Löwis59e47792009-01-24 14:10:07 +00001058 os.mkdir(os.path.join(TESTFN2, "x"))
1059 zipf = zipfile.ZipFile(TESTFN, "w")
1060 zipf.write(os.path.join(TESTFN2, "x"), "x")
1061 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1062
1063 def tearDown(self):
1064 shutil.rmtree(TESTFN2)
1065 if os.path.exists(TESTFN):
Ezio Melotti76430242009-07-11 18:28:48 +00001066 unlink(TESTFN)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001067
Guido van Rossumd8faa362007-04-27 19:54:29 +00001068
1069class UniversalNewlineTests(unittest.TestCase):
1070 def setUp(self):
Guido van Rossum9c627722007-08-27 18:31:48 +00001071 self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001072 for i in range(FIXEDTEST_SIZE)]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001073 self.seps = ('\r', '\r\n', '\n')
1074 self.arcdata, self.arcfiles = {}, {}
1075 for n, s in enumerate(self.seps):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001076 b = s.encode("ascii")
1077 self.arcdata[s] = b.join(self.line_gen) + b
Guido van Rossumd8faa362007-04-27 19:54:29 +00001078 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001079 f = open(self.arcfiles[s], "wb")
1080 try:
1081 f.write(self.arcdata[s])
1082 finally:
1083 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001084
Ezio Melottiafd0d112009-07-15 17:17:17 +00001085 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001086 # Create the ZIP archive
1087 zipfp = zipfile.ZipFile(f, "w", compression)
1088 for fn in self.arcfiles.values():
1089 zipfp.write(fn, fn)
1090 zipfp.close()
1091
Ezio Melottiafd0d112009-07-15 17:17:17 +00001092 def read_test(self, f, compression):
1093 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001094
1095 # Read the ZIP archive
1096 zipfp = zipfile.ZipFile(f, "r")
1097 for sep, fn in self.arcfiles.items():
1098 zipdata = zipfp.open(fn, "rU").read()
1099 self.assertEqual(self.arcdata[sep], zipdata)
1100
1101 zipfp.close()
1102
Ezio Melottiafd0d112009-07-15 17:17:17 +00001103 def readline_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 zipopen = zipfp.open(fn, "rU")
1110 for line in self.line_gen:
1111 linedata = zipopen.readline()
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001112 self.assertEqual(linedata, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001113
1114 zipfp.close()
1115
Ezio Melottiafd0d112009-07-15 17:17:17 +00001116 def readlines_test(self, f, compression):
1117 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001118
1119 # Read the ZIP archive
1120 zipfp = zipfile.ZipFile(f, "r")
1121 for sep, fn in self.arcfiles.items():
1122 ziplines = zipfp.open(fn, "rU").readlines()
1123 for line, zipline in zip(self.line_gen, ziplines):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001124 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001125
1126 zipfp.close()
1127
Ezio Melottiafd0d112009-07-15 17:17:17 +00001128 def iterlines_test(self, f, compression):
1129 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001130
1131 # Read the ZIP archive
1132 zipfp = zipfile.ZipFile(f, "r")
1133 for sep, fn in self.arcfiles.items():
1134 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001135 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001136
1137 zipfp.close()
1138
Ezio Melottiafd0d112009-07-15 17:17:17 +00001139 def test_read_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001140 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001141 self.read_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001142
Ezio Melottiafd0d112009-07-15 17:17:17 +00001143 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001144 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001145 self.readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001146
Ezio Melottiafd0d112009-07-15 17:17:17 +00001147 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001148 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001149 self.readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001150
Ezio Melottiafd0d112009-07-15 17:17:17 +00001151 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001152 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001153 self.iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001154
Ezio Melotti76430242009-07-11 18:28:48 +00001155 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001156 def test_read_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001157 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001158 self.read_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001159
Ezio Melotti76430242009-07-11 18:28:48 +00001160 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001161 def test_readline_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001162 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001163 self.readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001164
Ezio Melotti76430242009-07-11 18:28:48 +00001165 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001166 def test_readlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001167 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001168 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001169
Ezio Melotti76430242009-07-11 18:28:48 +00001170 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001171 def test_iterlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001172 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001173 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001174
1175 def tearDown(self):
1176 for sep, fn in self.arcfiles.items():
1177 os.remove(fn)
Ezio Melotti76430242009-07-11 18:28:48 +00001178 unlink(TESTFN)
1179 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001180
1181
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001182def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +00001183 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1184 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti76430242009-07-11 18:28:48 +00001185 TestWithDirectory, UniversalNewlineTests,
1186 TestsWithRandomBinaryFiles)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001187
1188if __name__ == "__main__":
1189 test_main()