blob: b17f857778c0a0a96fedae67c12a7e46ad427e57 [file] [log] [blame]
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001# We can test part of the module without zlib.
Guido van Rossum368f04a2000-04-10 13:23:04 +00002try:
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00003 import zlib
4except ImportError:
5 zlib = None
Ezio Melotti74c96ec2009-07-08 22:24:06 +00006
7import io
8import os
9import shutil
10import struct
11import zipfile
12import unittest
13
Tim Petersa45cacf2004-08-20 03:47:14 +000014
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000015from tempfile import TemporaryFile
Guido van Rossumd8faa362007-04-27 19:54:29 +000016from random import randint, random
Ezio Melotti74c96ec2009-07-08 22:24:06 +000017from unittest import skipUnless
Tim Petersa19a1682001-03-29 04:36:09 +000018
Ezio Melotti76430242009-07-11 18:28:48 +000019from test.support import TESTFN, run_unittest, findfile, unlink
Guido van Rossum368f04a2000-04-10 13:23:04 +000020
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000021TESTFN2 = TESTFN + "2"
Martin v. Löwis59e47792009-01-24 14:10:07 +000022TESTFNDIR = TESTFN + "d"
Guido van Rossumb5a755e2007-07-18 18:15:48 +000023FIXEDTEST_SIZE = 1000
Guido van Rossum368f04a2000-04-10 13:23:04 +000024
Christian Heimes790c8232008-01-07 21:14:23 +000025SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
26 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
27 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
28 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
29
Ezio Melotti76430242009-07-11 18:28:48 +000030
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000031class TestsWithSourceFile(unittest.TestCase):
32 def setUp(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +000033 self.line_gen = (bytes("Zipfile test line %d. random float: %f" %
Guido van Rossum9c627722007-08-27 18:31:48 +000034 (i, random()), "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +000035 for i in range(FIXEDTEST_SIZE))
36 self.data = b'\n'.join(self.line_gen) + b'\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000037
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000038 # Make a source file with some lines
39 fp = open(TESTFN, "wb")
40 fp.write(self.data)
41 fp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000042
Ezio Melottiafd0d112009-07-15 17:17:17 +000043 def make_test_archive(self, f, compression):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000044 # Create the ZIP archive
45 zipfp = zipfile.ZipFile(f, "w", compression)
Skip Montanaro7a98be22007-08-16 14:35:24 +000046 zipfp.write(TESTFN, "another.name")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000047 zipfp.write(TESTFN, TESTFN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000048 zipfp.writestr("strfile", self.data)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000049 zipfp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000050
Ezio Melottiafd0d112009-07-15 17:17:17 +000051 def zip_test(self, f, compression):
52 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +000053
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000054 # Read the ZIP archive
55 zipfp = zipfile.ZipFile(f, "r", compression)
56 self.assertEqual(zipfp.read(TESTFN), self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +000057 self.assertEqual(zipfp.read("another.name"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058 self.assertEqual(zipfp.read("strfile"), self.data)
59
60 # Print the ZIP directory
Guido van Rossumd6ca5462007-05-22 01:29:33 +000061 fp = io.StringIO()
62 zipfp.printdir(file=fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063 directory = fp.getvalue()
64 lines = directory.splitlines()
65 self.assertEquals(len(lines), 4) # Number of files + header
66
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000067 self.assertTrue('File Name' in lines[0])
68 self.assertTrue('Modified' in lines[0])
69 self.assertTrue('Size' in lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070
71 fn, date, time, size = lines[1].split()
72 self.assertEquals(fn, 'another.name')
73 # XXX: timestamp is not tested
74 self.assertEquals(size, str(len(self.data)))
75
76 # Check the namelist
77 names = zipfp.namelist()
78 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000079 self.assertTrue(TESTFN in names)
80 self.assertTrue("another.name" in names)
81 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000082
83 # Check infolist
84 infos = zipfp.infolist()
85 names = [ i.filename for i in infos ]
86 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000087 self.assertTrue(TESTFN in names)
88 self.assertTrue("another.name" in names)
89 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000090 for i in infos:
91 self.assertEquals(i.file_size, len(self.data))
92
93 # check getinfo
Skip Montanaro7a98be22007-08-16 14:35:24 +000094 for nm in (TESTFN, "another.name", "strfile"):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095 info = zipfp.getinfo(nm)
96 self.assertEquals(info.filename, nm)
97 self.assertEquals(info.file_size, len(self.data))
98
99 # Check that testzip doesn't raise an exception
100 zipfp.testzip()
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000101 zipfp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +0000102
Ezio Melottiafd0d112009-07-15 17:17:17 +0000103 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000104 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000105 self.zip_test(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000106
Ezio Melottiafd0d112009-07-15 17:17:17 +0000107 def zip_open_test(self, f, compression):
108 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000109
110 # Read the ZIP archive
111 zipfp = zipfile.ZipFile(f, "r", compression)
112 zipdata1 = []
113 zipopen1 = zipfp.open(TESTFN)
114 while 1:
115 read_data = zipopen1.read(256)
116 if not read_data:
117 break
118 zipdata1.append(read_data)
119
120 zipdata2 = []
Skip Montanaro7a98be22007-08-16 14:35:24 +0000121 zipopen2 = zipfp.open("another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000122 while 1:
123 read_data = zipopen2.read(256)
124 if not read_data:
125 break
126 zipdata2.append(read_data)
127
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000128 self.assertEqual(b''.join(zipdata1), self.data)
129 self.assertEqual(b''.join(zipdata2), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000130 zipfp.close()
131
Ezio Melottiafd0d112009-07-15 17:17:17 +0000132 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000133 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000134 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000135
Ezio Melottiafd0d112009-07-15 17:17:17 +0000136 def test_open_via_zip_info(self):
Georg Brandlb533e262008-05-25 18:19:30 +0000137 # Create the ZIP archive
138 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
139 zipfp.writestr("name", "foo")
140 zipfp.writestr("name", "bar")
141 zipfp.close()
142
143 zipfp = zipfile.ZipFile(TESTFN2, "r")
144 infos = zipfp.infolist()
145 data = b""
146 for info in infos:
147 data += zipfp.open(info).read()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000148 self.assertTrue(data == b"foobar" or data == b"barfoo")
Georg Brandlb533e262008-05-25 18:19:30 +0000149 data = b""
150 for info in infos:
151 data += zipfp.read(info)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000152 self.assertTrue(data == b"foobar" or data == b"barfoo")
Georg Brandlb533e262008-05-25 18:19:30 +0000153 zipfp.close()
154
Ezio Melottiafd0d112009-07-15 17:17:17 +0000155 def zip_random_open_test(self, f, compression):
156 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000157
158 # Read the ZIP archive
159 zipfp = zipfile.ZipFile(f, "r", compression)
160 zipdata1 = []
161 zipopen1 = zipfp.open(TESTFN)
162 while 1:
163 read_data = zipopen1.read(randint(1, 1024))
164 if not read_data:
165 break
166 zipdata1.append(read_data)
167
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000168 self.assertEqual(b''.join(zipdata1), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000169 zipfp.close()
170
Ezio Melottiafd0d112009-07-15 17:17:17 +0000171 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000172 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000173 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000174
Ezio Melottiafd0d112009-07-15 17:17:17 +0000175 def zip_readline_test(self, f, compression):
176 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000177
178 # Read the ZIP archive
179 zipfp = zipfile.ZipFile(f, "r")
180 zipopen = zipfp.open(TESTFN)
181 for line in self.line_gen:
182 linedata = zipopen.readline()
183 self.assertEqual(linedata, line + '\n')
184
185 zipfp.close()
186
Ezio Melottiafd0d112009-07-15 17:17:17 +0000187 def zip_readlines_test(self, f, compression):
188 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000189
190 # Read the ZIP archive
191 zipfp = zipfile.ZipFile(f, "r")
192 ziplines = zipfp.open(TESTFN).readlines()
193 for line, zipline in zip(self.line_gen, ziplines):
194 self.assertEqual(zipline, line + '\n')
195
196 zipfp.close()
197
Ezio Melottiafd0d112009-07-15 17:17:17 +0000198 def zip_iterlines_test(self, f, compression):
199 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000200
201 # Read the ZIP archive
202 zipfp = zipfile.ZipFile(f, "r")
203 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
204 self.assertEqual(zipline, line + '\n')
205
206 zipfp.close()
207
Ezio Melottiafd0d112009-07-15 17:17:17 +0000208 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000209 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000210 self.zip_readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000211
Ezio Melottiafd0d112009-07-15 17:17:17 +0000212 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000213 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000214 self.zip_readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000215
Ezio Melottiafd0d112009-07-15 17:17:17 +0000216 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000217 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000218 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000219
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000220 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000221 def test_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000222 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000223 self.zip_test(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000224
Guido van Rossumd8faa362007-04-27 19:54:29 +0000225
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000226 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000227 def test_open_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000228 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000229 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000230
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000231 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000232 def test_random_open_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000233 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000234 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000235
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000236 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000237 def test_readline_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000238 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000239 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000240
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000241 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000242 def test_readlines_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000243 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000244 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000245
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000246 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000247 def test_iterlines_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000248 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000249 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000250
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000251 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000252 def test_low_compression(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000253 # Checks for cases where compressed data is larger than original
254 # Create the ZIP archive
255 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
256 zipfp.writestr("strfile", '12')
257 zipfp.close()
258
259 # Get an open object for strfile
260 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED)
261 openobj = zipfp.open("strfile")
262 self.assertEqual(openobj.read(1), b'1')
263 self.assertEqual(openobj.read(1), b'2')
264
Ezio Melottiafd0d112009-07-15 17:17:17 +0000265 def test_absolute_arcnames(self):
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000266 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
267 zipfp.write(TESTFN, "/absolute")
268 zipfp.close()
269
270 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
271 self.assertEqual(zipfp.namelist(), ["absolute"])
272 zipfp.close()
Tim Peters32cbc962006-02-20 21:42:18 +0000273
Ezio Melottiafd0d112009-07-15 17:17:17 +0000274 def test_append_to_zip_file(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000275 # Test appending to an existing zipfile
276 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
277 zipfp.write(TESTFN, TESTFN)
278 zipfp.close()
279 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
280 zipfp.writestr("strfile", self.data)
281 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
282 zipfp.close()
283
Ezio Melottiafd0d112009-07-15 17:17:17 +0000284 def test_append_to_non_zip_file(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000285 # Test appending to an existing file that is not a zipfile
286 # NOTE: this test fails if len(d) < 22 because of the first
287 # line "fpin.seek(-22, 2)" in _EndRecData
Guido van Rossum9c627722007-08-27 18:31:48 +0000288 d = b'I am not a ZipFile!'*10
Guido van Rossum814661e2007-07-18 22:07:29 +0000289 f = open(TESTFN2, 'wb')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000290 f.write(d)
291 f.close()
292 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
293 zipfp.write(TESTFN, TESTFN)
294 zipfp.close()
295
Guido van Rossum814661e2007-07-18 22:07:29 +0000296 f = open(TESTFN2, 'rb')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000297 f.seek(len(d))
298 zipfp = zipfile.ZipFile(f, "r")
299 self.assertEqual(zipfp.namelist(), [TESTFN])
300 zipfp.close()
301 f.close()
302
Ezio Melottiafd0d112009-07-15 17:17:17 +0000303 def test_write_default_name(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000304 # Check that calling ZipFile.write without arcname specified produces the expected result
305 zipfp = zipfile.ZipFile(TESTFN2, "w")
306 zipfp.write(TESTFN)
Guido van Rossum814661e2007-07-18 22:07:29 +0000307 self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read())
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000308 zipfp.close()
309
Ezio Melottiafd0d112009-07-15 17:17:17 +0000310 def test_per_file_compression(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000311 # Check that files within a Zip archive can have different compression options
312 zipfp = zipfile.ZipFile(TESTFN2, "w")
313 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
314 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
315 sinfo = zipfp.getinfo('storeme')
316 dinfo = zipfp.getinfo('deflateme')
317 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
318 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
319 zipfp.close()
320
Ezio Melottiafd0d112009-07-15 17:17:17 +0000321 def test_write_to_readonly(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000322 # Check that trying to call write() on a readonly ZipFile object
323 # raises a RuntimeError
324 zipf = zipfile.ZipFile(TESTFN2, mode="w")
325 zipf.writestr("somefile.txt", "bogus")
326 zipf.close()
327 zipf = zipfile.ZipFile(TESTFN2, mode="r")
328 self.assertRaises(RuntimeError, zipf.write, TESTFN)
329 zipf.close()
330
Ezio Melottiafd0d112009-07-15 17:17:17 +0000331 def test_extract(self):
Christian Heimes790c8232008-01-07 21:14:23 +0000332 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
333 for fpath, fdata in SMALL_TEST_DATA:
334 zipfp.writestr(fpath, fdata)
335 zipfp.close()
336
337 zipfp = zipfile.ZipFile(TESTFN2, "r")
338 for fpath, fdata in SMALL_TEST_DATA:
339 writtenfile = zipfp.extract(fpath)
340
341 # make sure it was written to the right place
342 if os.path.isabs(fpath):
343 correctfile = os.path.join(os.getcwd(), fpath[1:])
344 else:
345 correctfile = os.path.join(os.getcwd(), fpath)
Christian Heimesaf98da12008-01-27 15:18:18 +0000346 correctfile = os.path.normpath(correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000347
348 self.assertEqual(writtenfile, correctfile)
349
350 # make sure correct data is in correct file
351 self.assertEqual(fdata.encode(), open(writtenfile, "rb").read())
352
353 os.remove(writtenfile)
354
355 zipfp.close()
356
357 # remove the test file subdirectories
358 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
359
Ezio Melottiafd0d112009-07-15 17:17:17 +0000360 def test_extract_all(self):
Christian Heimes790c8232008-01-07 21:14:23 +0000361 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
362 for fpath, fdata in SMALL_TEST_DATA:
363 zipfp.writestr(fpath, fdata)
364 zipfp.close()
365
366 zipfp = zipfile.ZipFile(TESTFN2, "r")
367 zipfp.extractall()
368 for fpath, fdata in SMALL_TEST_DATA:
369 if os.path.isabs(fpath):
370 outfile = os.path.join(os.getcwd(), fpath[1:])
371 else:
372 outfile = os.path.join(os.getcwd(), fpath)
373
374 self.assertEqual(fdata.encode(), open(outfile, "rb").read())
375
376 os.remove(outfile)
377
378 zipfp.close()
379
380 # remove the test file subdirectories
381 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
382
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000383 def zip_test_writestr_permissions(self, f, compression):
384 # Make sure that writestr creates files with mode 0600,
385 # when it is passed a name rather than a ZipInfo instance.
386
Ezio Melottiafd0d112009-07-15 17:17:17 +0000387 self.make_test_archive(f, compression)
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000388 zipfp = zipfile.ZipFile(f, "r")
389 zinfo = zipfp.getinfo('strfile')
390 self.assertEqual(zinfo.external_attr, 0o600 << 16)
391
Ezio Melottiafd0d112009-07-15 17:17:17 +0000392 def test_writestr_permissions(self):
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000393 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
394 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
395
Gregory P. Smithb0d9ca92009-07-07 05:06:04 +0000396 def test_writestr_extended_local_header_issue1202(self):
397 orig_zip = zipfile.ZipFile(TESTFN2, 'w')
398 for data in 'abcdefghijklmnop':
399 zinfo = zipfile.ZipInfo(data)
400 zinfo.flag_bits |= 0x08 # Include an extended local header.
401 orig_zip.writestr(zinfo, data)
402 orig_zip.close()
403
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000404 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000405 unlink(TESTFN)
406 unlink(TESTFN2)
407
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000408
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000409class TestZip64InSmallFiles(unittest.TestCase):
410 # These tests test the ZIP64 functionality without using large files,
411 # see test_zipfile64 for proper tests.
412
413 def setUp(self):
414 self._limit = zipfile.ZIP64_LIMIT
415 zipfile.ZIP64_LIMIT = 5
416
Guido van Rossum9c627722007-08-27 18:31:48 +0000417 line_gen = (bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000418 for i in range(0, FIXEDTEST_SIZE))
419 self.data = b'\n'.join(line_gen)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000420
421 # Make a source file with some lines
422 fp = open(TESTFN, "wb")
423 fp.write(self.data)
424 fp.close()
425
Ezio Melottiafd0d112009-07-15 17:17:17 +0000426 def large_file_exception_test(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000427 zipfp = zipfile.ZipFile(f, "w", compression)
428 self.assertRaises(zipfile.LargeZipFile,
Skip Montanaro7a98be22007-08-16 14:35:24 +0000429 zipfp.write, TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000430 zipfp.close()
431
Ezio Melottiafd0d112009-07-15 17:17:17 +0000432 def large_file_exception_test2(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000433 zipfp = zipfile.ZipFile(f, "w", compression)
434 self.assertRaises(zipfile.LargeZipFile,
Skip Montanaro7a98be22007-08-16 14:35:24 +0000435 zipfp.writestr, "another.name", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000436 zipfp.close()
437
Ezio Melottiafd0d112009-07-15 17:17:17 +0000438 def test_large_file_exception(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000439 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000440 self.large_file_exception_test(f, zipfile.ZIP_STORED)
441 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000442
Ezio Melottiafd0d112009-07-15 17:17:17 +0000443 def zip_test(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000444 # Create the ZIP archive
445 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000446 zipfp.write(TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000447 zipfp.write(TESTFN, TESTFN)
448 zipfp.writestr("strfile", self.data)
449 zipfp.close()
450
451 # Read the ZIP archive
452 zipfp = zipfile.ZipFile(f, "r", compression)
453 self.assertEqual(zipfp.read(TESTFN), self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000454 self.assertEqual(zipfp.read("another.name"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000455 self.assertEqual(zipfp.read("strfile"), self.data)
456
457 # Print the ZIP directory
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000458 fp = io.StringIO()
459 zipfp.printdir(fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000460
461 directory = fp.getvalue()
462 lines = directory.splitlines()
463 self.assertEquals(len(lines), 4) # Number of files + header
464
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000465 self.assertTrue('File Name' in lines[0])
466 self.assertTrue('Modified' in lines[0])
467 self.assertTrue('Size' in lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000468
469 fn, date, time, size = lines[1].split()
470 self.assertEquals(fn, 'another.name')
471 # XXX: timestamp is not tested
472 self.assertEquals(size, str(len(self.data)))
473
474 # Check the namelist
475 names = zipfp.namelist()
476 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000477 self.assertTrue(TESTFN in names)
478 self.assertTrue("another.name" in names)
479 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000480
481 # Check infolist
482 infos = zipfp.infolist()
483 names = [ i.filename for i in infos ]
484 self.assertEquals(len(names), 3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000485 self.assertTrue(TESTFN in names)
486 self.assertTrue("another.name" in names)
487 self.assertTrue("strfile" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000488 for i in infos:
489 self.assertEquals(i.file_size, len(self.data))
490
491 # check getinfo
Skip Montanaro7a98be22007-08-16 14:35:24 +0000492 for nm in (TESTFN, "another.name", "strfile"):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000493 info = zipfp.getinfo(nm)
494 self.assertEquals(info.filename, nm)
495 self.assertEquals(info.file_size, len(self.data))
496
497 # Check that testzip doesn't raise an exception
498 zipfp.testzip()
499
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000500 zipfp.close()
501
Ezio Melottiafd0d112009-07-15 17:17:17 +0000502 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000503 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000504 self.zip_test(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000505
Ezio Melotti76430242009-07-11 18:28:48 +0000506 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000507 def test_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000508 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000509 self.zip_test(f, zipfile.ZIP_DEFLATED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000510
Ezio Melottiafd0d112009-07-15 17:17:17 +0000511 def test_absolute_arcnames(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000512 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True)
513 zipfp.write(TESTFN, "/absolute")
514 zipfp.close()
515
516 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
517 self.assertEqual(zipfp.namelist(), ["absolute"])
518 zipfp.close()
519
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000520 def tearDown(self):
521 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti76430242009-07-11 18:28:48 +0000522 unlink(TESTFN)
523 unlink(TESTFN2)
524
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000525
526class PyZipFileTests(unittest.TestCase):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000527 def test_write_pyfile(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000528 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
529 fn = __file__
530 if fn.endswith('.pyc') or fn.endswith('.pyo'):
531 fn = fn[:-1]
532
533 zipfp.writepy(fn)
534
535 bn = os.path.basename(fn)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000536 self.assertTrue(bn not in zipfp.namelist())
537 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000538 zipfp.close()
539
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000540 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
541 fn = __file__
542 if fn.endswith('.pyc') or fn.endswith('.pyo'):
543 fn = fn[:-1]
544
545 zipfp.writepy(fn, "testpackage")
546
547 bn = "%s/%s"%("testpackage", os.path.basename(fn))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000548 self.assertTrue(bn not in zipfp.namelist())
549 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000550 zipfp.close()
551
Ezio Melottiafd0d112009-07-15 17:17:17 +0000552 def test_write_python_package(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000553 import email
554 packagedir = os.path.dirname(email.__file__)
555
556 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
557 zipfp.writepy(packagedir)
558
559 # Check for a couple of modules at different levels of the hieararchy
560 names = zipfp.namelist()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000561 self.assertTrue('email/__init__.pyo' in names or 'email/__init__.pyc' in names)
562 self.assertTrue('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000563
Ezio Melottiafd0d112009-07-15 17:17:17 +0000564 def test_write_python_directory(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000565 os.mkdir(TESTFN2)
566 try:
567 fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000568 fp.write("print(42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000569 fp.close()
570
571 fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000572 fp.write("print(42 * 42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000573 fp.close()
574
575 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
576 fp.write("bla bla bla\n")
577 fp.close()
578
579 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
580 zipfp.writepy(TESTFN2)
581
582 names = zipfp.namelist()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000583 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
584 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
585 self.assertTrue('mod2.txt' not in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000586
587 finally:
588 shutil.rmtree(TESTFN2)
589
Ezio Melottiafd0d112009-07-15 17:17:17 +0000590 def test_write_non_pyfile(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000591 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
Guido van Rossum814661e2007-07-18 22:07:29 +0000592 open(TESTFN, 'w').write('most definitely not a python file')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000593 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
594 os.remove(TESTFN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000595
596
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000597class OtherTests(unittest.TestCase):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000598 def test_unicode_filenames(self):
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000599 zf = zipfile.ZipFile(TESTFN, "w")
600 zf.writestr("foo.txt", "Test for unicode filename")
Martin v. Löwis1a9f9002008-05-05 17:50:05 +0000601 zf.writestr("\xf6.txt", "Test for unicode filename")
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000602 zf.close()
Martin v. Löwis1a9f9002008-05-05 17:50:05 +0000603 zf = zipfile.ZipFile(TESTFN, "r")
604 self.assertEqual(zf.filelist[0].filename, "foo.txt")
605 self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
606 zf.close()
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000607
Ezio Melottiafd0d112009-07-15 17:17:17 +0000608 def test_create_non_existent_file_for_append(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000609 if os.path.exists(TESTFN):
610 os.unlink(TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000611
Thomas Wouterscf297e42007-02-23 15:07:44 +0000612 filename = 'testfile.txt'
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000613 content = b'hello, world. this is some content.'
Guido van Rossumd8faa362007-04-27 19:54:29 +0000614
Thomas Wouterscf297e42007-02-23 15:07:44 +0000615 try:
616 zf = zipfile.ZipFile(TESTFN, 'a')
617 zf.writestr(filename, content)
618 zf.close()
619 except IOError:
620 self.fail('Could not append data to a non-existent zip file.')
621
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000622 self.assertTrue(os.path.exists(TESTFN))
Thomas Wouterscf297e42007-02-23 15:07:44 +0000623
624 zf = zipfile.ZipFile(TESTFN, 'r')
625 self.assertEqual(zf.read(filename), content)
626 zf.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000627
Ezio Melottiafd0d112009-07-15 17:17:17 +0000628 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000629 # This test checks that the ZipFile constructor closes the file object
630 # it opens if there's an error in the file. If it doesn't, the traceback
631 # holds a reference to the ZipFile object and, indirectly, the file object.
632 # On Windows, this causes the os.unlink() call to fail because the
633 # underlying file is still open. This is SF bug #412214.
634 #
635 fp = open(TESTFN, "w")
636 fp.write("this is not a legal zip file\n")
637 fp.close()
638 try:
639 zf = zipfile.ZipFile(TESTFN)
640 except zipfile.BadZipfile:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000641 pass
642
Ezio Melottiafd0d112009-07-15 17:17:17 +0000643 def test_is_zip_erroneous_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000644 # This test checks that the is_zipfile function correctly identifies
645 # a file that is not a zip file
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000646
647 # - passing a filename
648 with open(TESTFN, "w") as fp:
649 fp.write("this is not a legal zip file\n")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000650 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000651 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000652 # - passing a file object
653 with open(TESTFN, "rb") as fp:
654 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000655 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000656 # - passing a file-like object
657 fp = io.BytesIO()
658 fp.write(b"this is not a legal zip file\n")
659 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000660 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000661 fp.seek(0,0)
662 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000663 self.assertTrue(not chk)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000664
Ezio Melottiafd0d112009-07-15 17:17:17 +0000665 def test_is_zip_valid_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000666 # This test checks that the is_zipfile function correctly identifies
667 # a file that is a zip file
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000668
669 # - passing a filename
Guido van Rossumd8faa362007-04-27 19:54:29 +0000670 zipf = zipfile.ZipFile(TESTFN, mode="w")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000671 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000672 zipf.close()
673 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000674 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000675 # - passing a file object
676 with open(TESTFN, "rb") as fp:
677 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000678 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000679 fp.seek(0,0)
680 zip_contents = fp.read()
681 # - passing a file-like object
682 fp = io.BytesIO()
683 fp.write(zip_contents)
684 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000685 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000686 fp.seek(0,0)
687 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000688 self.assertTrue(chk)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000689
Ezio Melottiafd0d112009-07-15 17:17:17 +0000690 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000691 # make sure we don't raise an AttributeError when a partially-constructed
692 # ZipFile instance is finalized; this tests for regression on SF tracker
693 # bug #403871.
694
695 # The bug we're testing for caused an AttributeError to be raised
696 # when a ZipFile instance was created for a file that did not
697 # exist; the .fp member was not initialized but was needed by the
698 # __del__() method. Since the AttributeError is in the __del__(),
699 # it is ignored, but the user should be sufficiently annoyed by
700 # the message on the output that regression will be noticed
701 # quickly.
702 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
703
Amaury Forgeot d'Arcbc347802009-07-28 22:18:57 +0000704 def test_empty_file_raises_BadZipFile(self):
705 f = open(TESTFN, 'w')
706 f.close()
707 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
708
709 f = open(TESTFN, 'w')
710 f.write("short file")
711 f.close()
712 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
713
Ezio Melottiafd0d112009-07-15 17:17:17 +0000714 def test_closed_zip_raises_RuntimeError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000715 # Verify that testzip() doesn't swallow inappropriate exceptions.
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000716 data = io.BytesIO()
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000717 zipf = zipfile.ZipFile(data, mode="w")
718 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
719 zipf.close()
720
721 # This is correct; calling .read on a closed ZipFile should throw
722 # a RuntimeError, and so should calling .testzip. An earlier
723 # version of .testzip would swallow this exception (and any other)
724 # and report that the first file in the archive was corrupt.
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000725 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
726 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000727 self.assertRaises(RuntimeError, zipf.testzip)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000728 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Guido van Rossum814661e2007-07-18 22:07:29 +0000729 open(TESTFN, 'w').write('zipfile test data')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000730 self.assertRaises(RuntimeError, zipf.write, TESTFN)
731
Ezio Melottiafd0d112009-07-15 17:17:17 +0000732 def test_bad_constructor_mode(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000733 # Check that bad modes passed to ZipFile constructor are caught
734 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
735
Ezio Melottiafd0d112009-07-15 17:17:17 +0000736 def test_bad_open_mode(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000737 # Check that bad modes passed to ZipFile.open are caught
738 zipf = zipfile.ZipFile(TESTFN, mode="w")
739 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
740 zipf.close()
741 zipf = zipfile.ZipFile(TESTFN, mode="r")
742 # read the data to make sure the file is there
743 zipf.read("foo.txt")
744 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
745 zipf.close()
746
Ezio Melottiafd0d112009-07-15 17:17:17 +0000747 def test_read0(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000748 # Check that calling read(0) on a ZipExtFile object returns an empty
749 # string and doesn't advance file pointer
750 zipf = zipfile.ZipFile(TESTFN, mode="w")
751 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
752 # read the data to make sure the file is there
753 f = zipf.open("foo.txt")
754 for i in range(FIXEDTEST_SIZE):
Guido van Rossum814661e2007-07-18 22:07:29 +0000755 self.assertEqual(f.read(0), b'')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000756
Guido van Rossum814661e2007-07-18 22:07:29 +0000757 self.assertEqual(f.read(), b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000758 zipf.close()
759
Ezio Melottiafd0d112009-07-15 17:17:17 +0000760 def test_open_non_existent_item(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000761 # Check that attempting to call open() for an item that doesn't
762 # exist in the archive raises a RuntimeError
763 zipf = zipfile.ZipFile(TESTFN, mode="w")
764 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
765
Ezio Melottiafd0d112009-07-15 17:17:17 +0000766 def test_bad_compression_mode(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000767 # Check that bad compression methods passed to ZipFile.open are caught
768 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
769
Ezio Melottiafd0d112009-07-15 17:17:17 +0000770 def test_null_byte_in_filename(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000771 # Check that a filename containing a null byte is properly terminated
772 zipf = zipfile.ZipFile(TESTFN, mode="w")
Guido van Rossum814661e2007-07-18 22:07:29 +0000773 zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000774 self.assertEqual(zipf.namelist(), ['foo.txt'])
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000775
Ezio Melottiafd0d112009-07-15 17:17:17 +0000776 def test_struct_sizes(self):
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000777 # check that ZIP internal structure sizes are calculated correctly
778 self.assertEqual(zipfile.sizeEndCentDir, 22)
779 self.assertEqual(zipfile.sizeCentralDir, 46)
780 self.assertEqual(zipfile.sizeEndCentDir64, 56)
781 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
782
Ezio Melottiafd0d112009-07-15 17:17:17 +0000783 def test_comments(self):
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000784 # This test checks that comments on the archive are handled properly
785
786 # check default comment is empty
787 zipf = zipfile.ZipFile(TESTFN, mode="w")
788 self.assertEqual(zipf.comment, b'')
789 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
790 zipf.close()
791 zipfr = zipfile.ZipFile(TESTFN, mode="r")
792 self.assertEqual(zipfr.comment, b'')
793 zipfr.close()
794
795 # check a simple short comment
796 comment = b'Bravely taking to his feet, he beat a very brave retreat.'
797 zipf = zipfile.ZipFile(TESTFN, mode="w")
798 zipf.comment = comment
799 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
800 zipf.close()
801 zipfr = zipfile.ZipFile(TESTFN, mode="r")
802 self.assertEqual(zipfr.comment, comment)
803 zipfr.close()
804
805 # check a comment of max length
806 comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
807 comment2 = comment2.encode("ascii")
808 zipf = zipfile.ZipFile(TESTFN, mode="w")
809 zipf.comment = comment2
810 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
811 zipf.close()
812 zipfr = zipfile.ZipFile(TESTFN, mode="r")
813 self.assertEqual(zipfr.comment, comment2)
814 zipfr.close()
815
816 # check a comment that is too long is truncated
817 zipf = zipfile.ZipFile(TESTFN, mode="w")
818 zipf.comment = comment2 + b'oops'
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, comment2)
823 zipfr.close()
824
Guido van Rossumd8faa362007-04-27 19:54:29 +0000825 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000826 unlink(TESTFN)
827 unlink(TESTFN2)
828
Thomas Wouterscf297e42007-02-23 15:07:44 +0000829
830class DecryptionTests(unittest.TestCase):
831 # This test checks that ZIP decryption works. Since the library does not
832 # support encryption at the moment, we use a pre-generated encrypted
833 # ZIP file
834
835 data = (
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000836 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
837 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
838 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
839 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
840 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
841 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
842 b'\x00\x00L\x00\x00\x00\x00\x00' )
Christian Heimesfdab48e2008-01-20 09:06:41 +0000843 data2 = (
844 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
845 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
846 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
847 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
848 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
849 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
850 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
851 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Thomas Wouterscf297e42007-02-23 15:07:44 +0000852
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000853 plain = b'zipfile.py encryption test'
Christian Heimesfdab48e2008-01-20 09:06:41 +0000854 plain2 = b'\x00'*512
Thomas Wouterscf297e42007-02-23 15:07:44 +0000855
856 def setUp(self):
857 fp = open(TESTFN, "wb")
858 fp.write(self.data)
859 fp.close()
860 self.zip = zipfile.ZipFile(TESTFN, "r")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000861 fp = open(TESTFN2, "wb")
862 fp.write(self.data2)
863 fp.close()
864 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000865
866 def tearDown(self):
867 self.zip.close()
868 os.unlink(TESTFN)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000869 self.zip2.close()
870 os.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000871
Ezio Melottiafd0d112009-07-15 17:17:17 +0000872 def test_no_password(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000873 # Reading the encrypted file without password
874 # must generate a RunTime exception
875 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000876 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000877
Ezio Melottiafd0d112009-07-15 17:17:17 +0000878 def test_bad_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000879 self.zip.setpassword(b"perl")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000880 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000881 self.zip2.setpassword(b"perl")
882 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000883
Ezio Melottiafd0d112009-07-15 17:17:17 +0000884 def test_good_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000885 self.zip.setpassword(b"python")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000886 self.assertEquals(self.zip.read("test.txt"), self.plain)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000887 self.zip2.setpassword(b"12345")
888 self.assertEquals(self.zip2.read("zero"), self.plain2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000889
Guido van Rossumd8faa362007-04-27 19:54:29 +0000890
891class TestsWithRandomBinaryFiles(unittest.TestCase):
892 def setUp(self):
893 datacount = randint(16, 64)*1024 + randint(1, 1024)
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000894 self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
895 for i in range(datacount))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000896
897 # Make a source file with some lines
898 fp = open(TESTFN, "wb")
899 fp.write(self.data)
900 fp.close()
901
902 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000903 unlink(TESTFN)
904 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000905
Ezio Melottiafd0d112009-07-15 17:17:17 +0000906 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000907 # Create the ZIP archive
908 zipfp = zipfile.ZipFile(f, "w", compression)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000909 zipfp.write(TESTFN, "another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000910 zipfp.write(TESTFN, TESTFN)
911 zipfp.close()
912
Ezio Melottiafd0d112009-07-15 17:17:17 +0000913 def zip_test(self, f, compression):
914 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000915
916 # Read the ZIP archive
917 zipfp = zipfile.ZipFile(f, "r", compression)
918 testdata = zipfp.read(TESTFN)
919 self.assertEqual(len(testdata), len(self.data))
920 self.assertEqual(testdata, self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000921 self.assertEqual(zipfp.read("another.name"), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000922 zipfp.close()
923
Ezio Melottiafd0d112009-07-15 17:17:17 +0000924 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000925 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000926 self.zip_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000927
Ezio Melottiafd0d112009-07-15 17:17:17 +0000928 def zip_open_test(self, f, compression):
929 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000930
931 # Read the ZIP archive
932 zipfp = zipfile.ZipFile(f, "r", compression)
933 zipdata1 = []
934 zipopen1 = zipfp.open(TESTFN)
935 while 1:
936 read_data = zipopen1.read(256)
937 if not read_data:
938 break
939 zipdata1.append(read_data)
940
941 zipdata2 = []
Skip Montanaro7a98be22007-08-16 14:35:24 +0000942 zipopen2 = zipfp.open("another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000943 while 1:
944 read_data = zipopen2.read(256)
945 if not read_data:
946 break
947 zipdata2.append(read_data)
948
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000949 testdata1 = b''.join(zipdata1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000950 self.assertEqual(len(testdata1), len(self.data))
951 self.assertEqual(testdata1, self.data)
952
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000953 testdata2 = b''.join(zipdata2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000954 self.assertEqual(len(testdata1), len(self.data))
955 self.assertEqual(testdata1, self.data)
956 zipfp.close()
957
Ezio Melottiafd0d112009-07-15 17:17:17 +0000958 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000959 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000960 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000961
Ezio Melottiafd0d112009-07-15 17:17:17 +0000962 def zip_random_open_test(self, f, compression):
963 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000964
965 # Read the ZIP archive
966 zipfp = zipfile.ZipFile(f, "r", compression)
967 zipdata1 = []
968 zipopen1 = zipfp.open(TESTFN)
969 while 1:
970 read_data = zipopen1.read(randint(1, 1024))
971 if not read_data:
972 break
973 zipdata1.append(read_data)
974
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000975 testdata = b''.join(zipdata1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000976 self.assertEqual(len(testdata), len(self.data))
977 self.assertEqual(testdata, self.data)
978 zipfp.close()
979
Ezio Melottiafd0d112009-07-15 17:17:17 +0000980 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000981 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000982 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000983
Ezio Melotti76430242009-07-11 18:28:48 +0000984
Guido van Rossumd8faa362007-04-27 19:54:29 +0000985class TestsWithMultipleOpens(unittest.TestCase):
986 def setUp(self):
987 # Create the ZIP archive
988 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
989 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
990 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
991 zipfp.close()
992
Ezio Melottiafd0d112009-07-15 17:17:17 +0000993 def test_same_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000994 # Verify that (when the ZipFile is in control of creating file objects)
995 # multiple open() calls can be made without interfering with each other.
996 zipf = zipfile.ZipFile(TESTFN2, mode="r")
997 zopen1 = zipf.open('ones')
998 zopen2 = zipf.open('ones')
999 data1 = zopen1.read(500)
1000 data2 = zopen2.read(500)
1001 data1 += zopen1.read(500)
1002 data2 += zopen2.read(500)
1003 self.assertEqual(data1, data2)
1004 zipf.close()
1005
Ezio Melottiafd0d112009-07-15 17:17:17 +00001006 def test_different_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001007 # Verify that (when the ZipFile is in control of creating file objects)
1008 # multiple open() calls can be made without interfering with each other.
1009 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1010 zopen1 = zipf.open('ones')
1011 zopen2 = zipf.open('twos')
1012 data1 = zopen1.read(500)
1013 data2 = zopen2.read(500)
1014 data1 += zopen1.read(500)
1015 data2 += zopen2.read(500)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001016 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1017 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001018 zipf.close()
1019
Ezio Melottiafd0d112009-07-15 17:17:17 +00001020 def test_interleaved(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001021 # Verify that (when the ZipFile is in control of creating file objects)
1022 # multiple open() calls can be made without interfering with each other.
1023 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1024 zopen1 = zipf.open('ones')
1025 data1 = zopen1.read(500)
1026 zopen2 = zipf.open('twos')
1027 data2 = zopen2.read(500)
1028 data1 += zopen1.read(500)
1029 data2 += zopen2.read(500)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001030 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1031 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001032 zipf.close()
1033
1034 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001035 unlink(TESTFN2)
1036
Guido van Rossumd8faa362007-04-27 19:54:29 +00001037
Martin v. Löwis59e47792009-01-24 14:10:07 +00001038class TestWithDirectory(unittest.TestCase):
1039 def setUp(self):
1040 os.mkdir(TESTFN2)
1041
Ezio Melottiafd0d112009-07-15 17:17:17 +00001042 def test_extract_dir(self):
Martin v. Löwis59e47792009-01-24 14:10:07 +00001043 zipf = zipfile.ZipFile(findfile("zipdir.zip"))
1044 zipf.extractall(TESTFN2)
1045 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1046 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1047 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1048
Ezio Melottiafd0d112009-07-15 17:17:17 +00001049 def test_bug_6050(self):
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001050 # Extraction should succeed if directories already exist
1051 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottiafd0d112009-07-15 17:17:17 +00001052 self.test_extract_dir()
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001053
Ezio Melottiafd0d112009-07-15 17:17:17 +00001054 def test_store_dir(self):
Martin v. Löwis59e47792009-01-24 14:10:07 +00001055 os.mkdir(os.path.join(TESTFN2, "x"))
1056 zipf = zipfile.ZipFile(TESTFN, "w")
1057 zipf.write(os.path.join(TESTFN2, "x"), "x")
1058 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1059
1060 def tearDown(self):
1061 shutil.rmtree(TESTFN2)
1062 if os.path.exists(TESTFN):
Ezio Melotti76430242009-07-11 18:28:48 +00001063 unlink(TESTFN)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001064
Guido van Rossumd8faa362007-04-27 19:54:29 +00001065
1066class UniversalNewlineTests(unittest.TestCase):
1067 def setUp(self):
Guido van Rossum9c627722007-08-27 18:31:48 +00001068 self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001069 for i in range(FIXEDTEST_SIZE)]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001070 self.seps = ('\r', '\r\n', '\n')
1071 self.arcdata, self.arcfiles = {}, {}
1072 for n, s in enumerate(self.seps):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001073 b = s.encode("ascii")
1074 self.arcdata[s] = b.join(self.line_gen) + b
Guido van Rossumd8faa362007-04-27 19:54:29 +00001075 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001076 f = open(self.arcfiles[s], "wb")
1077 try:
1078 f.write(self.arcdata[s])
1079 finally:
1080 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001081
Ezio Melottiafd0d112009-07-15 17:17:17 +00001082 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001083 # Create the ZIP archive
1084 zipfp = zipfile.ZipFile(f, "w", compression)
1085 for fn in self.arcfiles.values():
1086 zipfp.write(fn, fn)
1087 zipfp.close()
1088
Ezio Melottiafd0d112009-07-15 17:17:17 +00001089 def read_test(self, f, compression):
1090 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001091
1092 # Read the ZIP archive
1093 zipfp = zipfile.ZipFile(f, "r")
1094 for sep, fn in self.arcfiles.items():
1095 zipdata = zipfp.open(fn, "rU").read()
1096 self.assertEqual(self.arcdata[sep], zipdata)
1097
1098 zipfp.close()
1099
Ezio Melottiafd0d112009-07-15 17:17:17 +00001100 def readline_test(self, f, compression):
1101 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001102
1103 # Read the ZIP archive
1104 zipfp = zipfile.ZipFile(f, "r")
1105 for sep, fn in self.arcfiles.items():
1106 zipopen = zipfp.open(fn, "rU")
1107 for line in self.line_gen:
1108 linedata = zipopen.readline()
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001109 self.assertEqual(linedata, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001110
1111 zipfp.close()
1112
Ezio Melottiafd0d112009-07-15 17:17:17 +00001113 def readlines_test(self, f, compression):
1114 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001115
1116 # Read the ZIP archive
1117 zipfp = zipfile.ZipFile(f, "r")
1118 for sep, fn in self.arcfiles.items():
1119 ziplines = zipfp.open(fn, "rU").readlines()
1120 for line, zipline in zip(self.line_gen, ziplines):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001121 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001122
1123 zipfp.close()
1124
Ezio Melottiafd0d112009-07-15 17:17:17 +00001125 def iterlines_test(self, f, compression):
1126 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001127
1128 # Read the ZIP archive
1129 zipfp = zipfile.ZipFile(f, "r")
1130 for sep, fn in self.arcfiles.items():
1131 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001132 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001133
1134 zipfp.close()
1135
Ezio Melottiafd0d112009-07-15 17:17:17 +00001136 def test_read_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001137 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001138 self.read_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001139
Ezio Melottiafd0d112009-07-15 17:17:17 +00001140 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001141 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001142 self.readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001143
Ezio Melottiafd0d112009-07-15 17:17:17 +00001144 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001145 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001146 self.readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001147
Ezio Melottiafd0d112009-07-15 17:17:17 +00001148 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001149 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001150 self.iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001151
Ezio Melotti76430242009-07-11 18:28:48 +00001152 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001153 def test_read_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001154 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001155 self.read_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001156
Ezio Melotti76430242009-07-11 18:28:48 +00001157 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001158 def test_readline_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001159 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001160 self.readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001161
Ezio Melotti76430242009-07-11 18:28:48 +00001162 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001163 def test_readlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001164 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001165 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001166
Ezio Melotti76430242009-07-11 18:28:48 +00001167 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001168 def test_iterlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001169 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001170 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001171
1172 def tearDown(self):
1173 for sep, fn in self.arcfiles.items():
1174 os.remove(fn)
Ezio Melotti76430242009-07-11 18:28:48 +00001175 unlink(TESTFN)
1176 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001177
1178
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001179def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +00001180 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1181 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti76430242009-07-11 18:28:48 +00001182 TestWithDirectory, UniversalNewlineTests,
1183 TestsWithRandomBinaryFiles)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001184
1185if __name__ == "__main__":
1186 test_main()