blob: 380e63b5ab71944f4040e88cbe2370b0f1cd4bdc [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
Barry Warsaw28a691b2010-04-17 00:19:56 +00009import imp
Ezio Melotti35386712009-12-31 13:22:41 +000010import time
Ezio Melotti74c96ec2009-07-08 22:24:06 +000011import shutil
12import struct
13import zipfile
14import unittest
15
Tim Petersa45cacf2004-08-20 03:47:14 +000016
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000017from tempfile import TemporaryFile
Guido van Rossumd8faa362007-04-27 19:54:29 +000018from random import randint, random
Ezio Melotti74c96ec2009-07-08 22:24:06 +000019from unittest import skipUnless
Tim Petersa19a1682001-03-29 04:36:09 +000020
Ezio Melotti76430242009-07-11 18:28:48 +000021from test.support import TESTFN, run_unittest, findfile, unlink
Guido van Rossum368f04a2000-04-10 13:23:04 +000022
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000023TESTFN2 = TESTFN + "2"
Martin v. Löwis59e47792009-01-24 14:10:07 +000024TESTFNDIR = TESTFN + "d"
Guido van Rossumb5a755e2007-07-18 18:15:48 +000025FIXEDTEST_SIZE = 1000
Guido van Rossum368f04a2000-04-10 13:23:04 +000026
Christian Heimes790c8232008-01-07 21:14:23 +000027SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
28 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
29 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
30 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
31
Ezio Melotti76430242009-07-11 18:28:48 +000032
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000033class TestsWithSourceFile(unittest.TestCase):
34 def setUp(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +000035 self.line_gen = (bytes("Zipfile test line %d. random float: %f" %
Guido van Rossum9c627722007-08-27 18:31:48 +000036 (i, random()), "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +000037 for i in range(FIXEDTEST_SIZE))
38 self.data = b'\n'.join(self.line_gen) + b'\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000039
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000040 # Make a source file with some lines
Ezio Melotti35386712009-12-31 13:22:41 +000041 with open(TESTFN, "wb") as fp:
42 fp.write(self.data)
Tim Peters7d3bad62001-04-04 18:56:49 +000043
Ezio Melottiafd0d112009-07-15 17:17:17 +000044 def make_test_archive(self, f, compression):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000045 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000046 with zipfile.ZipFile(f, "w", compression) as zipfp:
47 zipfp.write(TESTFN, "another.name")
48 zipfp.write(TESTFN, TESTFN)
49 zipfp.writestr("strfile", self.data)
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
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000055 with zipfile.ZipFile(f, "r", compression) as zipfp:
56 self.assertEqual(zipfp.read(TESTFN), self.data)
57 self.assertEqual(zipfp.read("another.name"), self.data)
58 self.assertEqual(zipfp.read("strfile"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000060 # Print the ZIP directory
61 fp = io.StringIO()
62 zipfp.printdir(file=fp)
63 directory = fp.getvalue()
64 lines = directory.splitlines()
Ezio Melotti35386712009-12-31 13:22:41 +000065 self.assertEqual(len(lines), 4) # Number of files + header
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066
Benjamin Peterson577473f2010-01-19 00:09:57 +000067 self.assertIn('File Name', lines[0])
68 self.assertIn('Modified', lines[0])
69 self.assertIn('Size', lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070
Ezio Melotti35386712009-12-31 13:22:41 +000071 fn, date, time_, size = lines[1].split()
72 self.assertEqual(fn, 'another.name')
73 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
74 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
75 self.assertEqual(size, str(len(self.data)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000077 # Check the namelist
78 names = zipfp.namelist()
Ezio Melotti35386712009-12-31 13:22:41 +000079 self.assertEqual(len(names), 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +000080 self.assertIn(TESTFN, names)
81 self.assertIn("another.name", names)
82 self.assertIn("strfile", names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000084 # Check infolist
85 infos = zipfp.infolist()
Ezio Melotti35386712009-12-31 13:22:41 +000086 names = [i.filename for i in infos]
87 self.assertEqual(len(names), 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +000088 self.assertIn(TESTFN, names)
89 self.assertIn("another.name", names)
90 self.assertIn("strfile", names)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000091 for i in infos:
Ezio Melotti35386712009-12-31 13:22:41 +000092 self.assertEqual(i.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000094 # check getinfo
95 for nm in (TESTFN, "another.name", "strfile"):
96 info = zipfp.getinfo(nm)
Ezio Melotti35386712009-12-31 13:22:41 +000097 self.assertEqual(info.filename, nm)
98 self.assertEqual(info.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000099
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000100 # Check that testzip doesn't raise an exception
101 zipfp.testzip()
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
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000111 with zipfile.ZipFile(f, "r", compression) as zipfp:
112 zipdata1 = []
113 zipopen1 = zipfp.open(TESTFN)
114 while True:
115 read_data = zipopen1.read(256)
116 if not read_data:
117 break
118 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000119
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000120 zipdata2 = []
121 zipopen2 = zipfp.open("another.name")
122 while True:
123 read_data = zipopen2.read(256)
124 if not read_data:
125 break
126 zipdata2.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000127
Ezio Melottifaa6b7f2009-12-30 12:34:59 +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
Ezio Melottiafd0d112009-07-15 17:17:17 +0000131 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000132 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000133 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000134
Ezio Melottiafd0d112009-07-15 17:17:17 +0000135 def test_open_via_zip_info(self):
Georg Brandlb533e262008-05-25 18:19:30 +0000136 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000137 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
138 zipfp.writestr("name", "foo")
139 zipfp.writestr("name", "bar")
Georg Brandlb533e262008-05-25 18:19:30 +0000140
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000141 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
142 infos = zipfp.infolist()
143 data = b""
144 for info in infos:
145 data += zipfp.open(info).read()
146 self.assertTrue(data == b"foobar" or data == b"barfoo")
147 data = b""
148 for info in infos:
149 data += zipfp.read(info)
150 self.assertTrue(data == b"foobar" or data == b"barfoo")
Georg Brandlb533e262008-05-25 18:19:30 +0000151
Ezio Melottiafd0d112009-07-15 17:17:17 +0000152 def zip_random_open_test(self, f, compression):
153 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000154
155 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000156 with zipfile.ZipFile(f, "r", compression) as zipfp:
157 zipdata1 = []
158 zipopen1 = zipfp.open(TESTFN)
159 while True:
160 read_data = zipopen1.read(randint(1, 1024))
161 if not read_data:
162 break
163 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000164
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000165 self.assertEqual(b''.join(zipdata1), self.data)
166
Guido van Rossumd8faa362007-04-27 19:54:29 +0000167
Ezio Melottiafd0d112009-07-15 17:17:17 +0000168 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000169 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000170 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000171
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000172 def test_univeral_readaheads(self):
173 f = io.BytesIO()
174
175 data = b'a\r\n' * 16 * 1024
176 zipfp = zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED)
177 zipfp.writestr(TESTFN, data)
178 zipfp.close()
179
180 data2 = b''
181 zipfp = zipfile.ZipFile(f, 'r')
182 zipopen = zipfp.open(TESTFN, 'rU')
183 for line in zipopen:
184 data2 += line
185 zipfp.close()
186
187 self.assertEqual(data, data2.replace(b'\n', b'\r\n'))
188
189 def zip_readline_read_test(self, f, compression):
190 self.make_test_archive(f, compression)
191
192 # Read the ZIP archive
193 zipfp = zipfile.ZipFile(f, "r")
194 zipopen = zipfp.open(TESTFN)
195
196 data = b''
197 while True:
198 read = zipopen.readline()
199 if not read:
200 break
201 data += read
202
203 read = zipopen.read(100)
204 if not read:
205 break
206 data += read
207
208 self.assertEqual(data, self.data)
209 zipfp.close()
210
Ezio Melottiafd0d112009-07-15 17:17:17 +0000211 def zip_readline_test(self, f, compression):
212 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000213
214 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000215 with zipfile.ZipFile(f, "r") as zipfp:
216 zipopen = zipfp.open(TESTFN)
217 for line in self.line_gen:
218 linedata = zipopen.readline()
219 self.assertEqual(linedata, line + '\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000220
Ezio Melottiafd0d112009-07-15 17:17:17 +0000221 def zip_readlines_test(self, f, compression):
222 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000223
224 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000225 with zipfile.ZipFile(f, "r") as zipfp:
226 ziplines = zipfp.open(TESTFN).readlines()
227 for line, zipline in zip(self.line_gen, ziplines):
228 self.assertEqual(zipline, line + '\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000229
Ezio Melottiafd0d112009-07-15 17:17:17 +0000230 def zip_iterlines_test(self, f, compression):
231 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000232
233 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000234 with zipfile.ZipFile(f, "r") as zipfp:
235 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
236 self.assertEqual(zipline, line + '\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000237
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000238 def test_readline_read_stored(self):
239 # Issue #7610: calls to readline() interleaved with calls to read().
240 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
241 self.zip_readline_read_test(f, zipfile.ZIP_STORED)
242
Ezio Melottiafd0d112009-07-15 17:17:17 +0000243 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000244 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000245 self.zip_readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000246
Ezio Melottiafd0d112009-07-15 17:17:17 +0000247 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000248 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000249 self.zip_readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000250
Ezio Melottiafd0d112009-07-15 17:17:17 +0000251 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000252 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000253 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000254
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000255 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000256 def test_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000257 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000258 self.zip_test(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000259
Guido van Rossumd8faa362007-04-27 19:54:29 +0000260
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000261 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000262 def test_open_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000263 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000264 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000265
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000266 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000267 def test_random_open_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000268 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000269 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000270
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000271 @skipUnless(zlib, "requires zlib")
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000272 def test_readline_read_deflated(self):
273 # Issue #7610: calls to readline() interleaved with calls to read().
274 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
275 self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED)
276
277 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000278 def test_readline_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000279 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000280 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000281
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000282 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000283 def test_readlines_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000284 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000285 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000286
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000287 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000288 def test_iterlines_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000289 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000290 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000291
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000292 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000293 def test_low_compression(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000294 """Check for cases where compressed data is larger than original."""
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000295 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000296 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
297 zipfp.writestr("strfile", '12')
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000298
299 # Get an open object for strfile
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000300 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
301 openobj = zipfp.open("strfile")
302 self.assertEqual(openobj.read(1), b'1')
303 self.assertEqual(openobj.read(1), b'2')
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000304
Ezio Melottiafd0d112009-07-15 17:17:17 +0000305 def test_absolute_arcnames(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000306 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
307 zipfp.write(TESTFN, "/absolute")
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000308
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000309 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
310 self.assertEqual(zipfp.namelist(), ["absolute"])
Tim Peters32cbc962006-02-20 21:42:18 +0000311
Ezio Melottiafd0d112009-07-15 17:17:17 +0000312 def test_append_to_zip_file(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000313 """Test appending to an existing zipfile."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000314 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
315 zipfp.write(TESTFN, TESTFN)
316
317 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
318 zipfp.writestr("strfile", self.data)
319 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000320
Ezio Melottiafd0d112009-07-15 17:17:17 +0000321 def test_append_to_non_zip_file(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000322 """Test appending to an existing file that is not a zipfile."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000323 # NOTE: this test fails if len(d) < 22 because of the first
324 # line "fpin.seek(-22, 2)" in _EndRecData
Ezio Melotti35386712009-12-31 13:22:41 +0000325 data = b'I am not a ZipFile!'*10
326 with open(TESTFN2, 'wb') as f:
327 f.write(data)
328
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000329 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
330 zipfp.write(TESTFN, TESTFN)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000331
Ezio Melotti35386712009-12-31 13:22:41 +0000332 with open(TESTFN2, 'rb') as f:
333 f.seek(len(data))
334 with zipfile.ZipFile(f, "r") as zipfp:
335 self.assertEqual(zipfp.namelist(), [TESTFN])
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000336
Ezio Melottiafd0d112009-07-15 17:17:17 +0000337 def test_write_default_name(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000338 """Check that calling ZipFile.write without arcname specified
339 produces the expected result."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000340 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
341 zipfp.write(TESTFN)
342 self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read())
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000343
Ezio Melotti78ea2022009-09-12 18:41:20 +0000344 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000345 def test_per_file_compression(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000346 """Check that files within a Zip archive can have different
347 compression options."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000348 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
349 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
350 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
351 sinfo = zipfp.getinfo('storeme')
352 dinfo = zipfp.getinfo('deflateme')
353 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
354 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000355
Ezio Melottiafd0d112009-07-15 17:17:17 +0000356 def test_write_to_readonly(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000357 """Check that trying to call write() on a readonly ZipFile object
358 raises a RuntimeError."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000359 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
360 zipfp.writestr("somefile.txt", "bogus")
361
362 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
363 self.assertRaises(RuntimeError, zipfp.write, TESTFN)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000364
Ezio Melottiafd0d112009-07-15 17:17:17 +0000365 def test_extract(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000366 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
367 for fpath, fdata in SMALL_TEST_DATA:
368 zipfp.writestr(fpath, fdata)
Christian Heimes790c8232008-01-07 21:14:23 +0000369
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000370 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
371 for fpath, fdata in SMALL_TEST_DATA:
372 writtenfile = zipfp.extract(fpath)
Christian Heimes790c8232008-01-07 21:14:23 +0000373
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000374 # make sure it was written to the right place
375 if os.path.isabs(fpath):
376 correctfile = os.path.join(os.getcwd(), fpath[1:])
377 else:
378 correctfile = os.path.join(os.getcwd(), fpath)
379 correctfile = os.path.normpath(correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000380
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000381 self.assertEqual(writtenfile, correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000382
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000383 # make sure correct data is in correct file
384 self.assertEqual(fdata.encode(), open(writtenfile, "rb").read())
Christian Heimes790c8232008-01-07 21:14:23 +0000385
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000386 os.remove(writtenfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000387
388 # remove the test file subdirectories
389 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
390
Ezio Melottiafd0d112009-07-15 17:17:17 +0000391 def test_extract_all(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000392 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
393 for fpath, fdata in SMALL_TEST_DATA:
394 zipfp.writestr(fpath, fdata)
Christian Heimes790c8232008-01-07 21:14:23 +0000395
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000396 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
397 zipfp.extractall()
398 for fpath, fdata in SMALL_TEST_DATA:
399 if os.path.isabs(fpath):
400 outfile = os.path.join(os.getcwd(), fpath[1:])
401 else:
402 outfile = os.path.join(os.getcwd(), fpath)
Christian Heimes790c8232008-01-07 21:14:23 +0000403
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000404 self.assertEqual(fdata.encode(), open(outfile, "rb").read())
Christian Heimes790c8232008-01-07 21:14:23 +0000405
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000406 os.remove(outfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000407
408 # remove the test file subdirectories
409 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
410
Ronald Oussorenee5c8852010-02-07 20:24:02 +0000411 def test_writestr_compression(self):
412 zipfp = zipfile.ZipFile(TESTFN2, "w")
413 zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED)
414 if zlib:
415 zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED)
416
417 info = zipfp.getinfo('a.txt')
418 self.assertEqual(info.compress_type, zipfile.ZIP_STORED)
419
420 if zlib:
421 info = zipfp.getinfo('b.txt')
422 self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED)
423
424
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000425 def zip_test_writestr_permissions(self, f, compression):
426 # Make sure that writestr creates files with mode 0600,
427 # when it is passed a name rather than a ZipInfo instance.
428
Ezio Melottiafd0d112009-07-15 17:17:17 +0000429 self.make_test_archive(f, compression)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000430 with zipfile.ZipFile(f, "r") as zipfp:
431 zinfo = zipfp.getinfo('strfile')
432 self.assertEqual(zinfo.external_attr, 0o600 << 16)
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000433
Ezio Melottiafd0d112009-07-15 17:17:17 +0000434 def test_writestr_permissions(self):
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000435 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
436 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
437
Gregory P. Smithb0d9ca92009-07-07 05:06:04 +0000438 def test_writestr_extended_local_header_issue1202(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000439 with zipfile.ZipFile(TESTFN2, 'w') as orig_zip:
440 for data in 'abcdefghijklmnop':
441 zinfo = zipfile.ZipInfo(data)
442 zinfo.flag_bits |= 0x08 # Include an extended local header.
443 orig_zip.writestr(zinfo, data)
444
445 def test_close(self):
446 """Check that the zipfile is closed after the 'with' block."""
447 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
448 for fpath, fdata in SMALL_TEST_DATA:
449 zipfp.writestr(fpath, fdata)
450 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
451 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
452
453 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
454 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
455 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
456
457 def test_close_on_exception(self):
458 """Check that the zipfile is closed if an exception is raised in the
459 'with' block."""
460 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
461 for fpath, fdata in SMALL_TEST_DATA:
462 zipfp.writestr(fpath, fdata)
463
464 try:
465 with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
466 raise zipfile.BadZipfile()
467 except zipfile.BadZipfile:
468 self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
Gregory P. Smithb0d9ca92009-07-07 05:06:04 +0000469
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000470 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000471 unlink(TESTFN)
472 unlink(TESTFN2)
473
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000474
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000475class TestZip64InSmallFiles(unittest.TestCase):
476 # These tests test the ZIP64 functionality without using large files,
477 # see test_zipfile64 for proper tests.
478
479 def setUp(self):
480 self._limit = zipfile.ZIP64_LIMIT
481 zipfile.ZIP64_LIMIT = 5
482
Guido van Rossum9c627722007-08-27 18:31:48 +0000483 line_gen = (bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000484 for i in range(0, FIXEDTEST_SIZE))
485 self.data = b'\n'.join(line_gen)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000486
487 # Make a source file with some lines
Ezio Melotti35386712009-12-31 13:22:41 +0000488 with open(TESTFN, "wb") as fp:
489 fp.write(self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000490
Ezio Melottiafd0d112009-07-15 17:17:17 +0000491 def large_file_exception_test(self, f, compression):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000492 with zipfile.ZipFile(f, "w", compression) as zipfp:
493 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti35386712009-12-31 13:22:41 +0000494 zipfp.write, TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000495
Ezio Melottiafd0d112009-07-15 17:17:17 +0000496 def large_file_exception_test2(self, f, compression):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000497 with zipfile.ZipFile(f, "w", compression) as zipfp:
498 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti35386712009-12-31 13:22:41 +0000499 zipfp.writestr, "another.name", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000500
Ezio Melottiafd0d112009-07-15 17:17:17 +0000501 def test_large_file_exception(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000502 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000503 self.large_file_exception_test(f, zipfile.ZIP_STORED)
504 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000505
Ezio Melottiafd0d112009-07-15 17:17:17 +0000506 def zip_test(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000507 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000508 with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
509 zipfp.write(TESTFN, "another.name")
510 zipfp.write(TESTFN, TESTFN)
511 zipfp.writestr("strfile", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000512
513 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000514 with zipfile.ZipFile(f, "r", compression) as zipfp:
515 self.assertEqual(zipfp.read(TESTFN), self.data)
516 self.assertEqual(zipfp.read("another.name"), self.data)
517 self.assertEqual(zipfp.read("strfile"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000518
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000519 # Print the ZIP directory
520 fp = io.StringIO()
521 zipfp.printdir(fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000522
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000523 directory = fp.getvalue()
524 lines = directory.splitlines()
Ezio Melotti35386712009-12-31 13:22:41 +0000525 self.assertEqual(len(lines), 4) # Number of files + header
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000526
Benjamin Peterson577473f2010-01-19 00:09:57 +0000527 self.assertIn('File Name', lines[0])
528 self.assertIn('Modified', lines[0])
529 self.assertIn('Size', lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000530
Ezio Melotti35386712009-12-31 13:22:41 +0000531 fn, date, time_, size = lines[1].split()
532 self.assertEqual(fn, 'another.name')
533 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
534 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
535 self.assertEqual(size, str(len(self.data)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000536
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000537 # Check the namelist
538 names = zipfp.namelist()
Ezio Melotti35386712009-12-31 13:22:41 +0000539 self.assertEqual(len(names), 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000540 self.assertIn(TESTFN, names)
541 self.assertIn("another.name", names)
542 self.assertIn("strfile", names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000543
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000544 # Check infolist
545 infos = zipfp.infolist()
Ezio Melotti35386712009-12-31 13:22:41 +0000546 names = [i.filename for i in infos]
547 self.assertEqual(len(names), 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000548 self.assertIn(TESTFN, names)
549 self.assertIn("another.name", names)
550 self.assertIn("strfile", names)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000551 for i in infos:
Ezio Melotti35386712009-12-31 13:22:41 +0000552 self.assertEqual(i.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000553
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000554 # check getinfo
555 for nm in (TESTFN, "another.name", "strfile"):
556 info = zipfp.getinfo(nm)
Ezio Melotti35386712009-12-31 13:22:41 +0000557 self.assertEqual(info.filename, nm)
558 self.assertEqual(info.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000559
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000560 # Check that testzip doesn't raise an exception
561 zipfp.testzip()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000562
Ezio Melottiafd0d112009-07-15 17:17:17 +0000563 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000564 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000565 self.zip_test(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000566
Ezio Melotti76430242009-07-11 18:28:48 +0000567 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000568 def test_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000569 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000570 self.zip_test(f, zipfile.ZIP_DEFLATED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000571
Ezio Melottiafd0d112009-07-15 17:17:17 +0000572 def test_absolute_arcnames(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000573 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
574 allowZip64=True) as zipfp:
575 zipfp.write(TESTFN, "/absolute")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000576
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000577 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
578 self.assertEqual(zipfp.namelist(), ["absolute"])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000579
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000580 def tearDown(self):
581 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti76430242009-07-11 18:28:48 +0000582 unlink(TESTFN)
583 unlink(TESTFN2)
584
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000585
586class PyZipFileTests(unittest.TestCase):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000587 def test_write_pyfile(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000588 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
589 fn = __file__
590 if fn.endswith('.pyc') or fn.endswith('.pyo'):
Barry Warsaw28a691b2010-04-17 00:19:56 +0000591 path_split = fn.split(os.sep)
592 if os.altsep is not None:
593 path_split.extend(fn.split(os.altsep))
594 if '__pycache__' in path_split:
595 fn = imp.source_from_cache(fn)
596 else:
597 fn = fn[:-1]
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000598
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000599 zipfp.writepy(fn)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000600
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000601 bn = os.path.basename(fn)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000602 self.assertNotIn(bn, zipfp.namelist())
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000603 self.assertTrue(bn + 'o' in zipfp.namelist() or
604 bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000605
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000606 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
607 fn = __file__
Ezio Melotti35386712009-12-31 13:22:41 +0000608 if fn.endswith(('.pyc', '.pyo')):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000609 fn = fn[:-1]
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000610
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000611 zipfp.writepy(fn, "testpackage")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000612
Ezio Melotti35386712009-12-31 13:22:41 +0000613 bn = "%s/%s" % ("testpackage", os.path.basename(fn))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000614 self.assertNotIn(bn, zipfp.namelist())
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000615 self.assertTrue(bn + 'o' in zipfp.namelist() or
616 bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000617
Ezio Melottiafd0d112009-07-15 17:17:17 +0000618 def test_write_python_package(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000619 import email
620 packagedir = os.path.dirname(email.__file__)
621
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000622 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
623 zipfp.writepy(packagedir)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000624
Ezio Melotti35386712009-12-31 13:22:41 +0000625 # Check for a couple of modules at different levels of the
626 # hierarchy
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000627 names = zipfp.namelist()
628 self.assertTrue('email/__init__.pyo' in names or
629 'email/__init__.pyc' in names)
630 self.assertTrue('email/mime/text.pyo' in names or
631 'email/mime/text.pyc' in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000632
Ezio Melottiafd0d112009-07-15 17:17:17 +0000633 def test_write_python_directory(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000634 os.mkdir(TESTFN2)
635 try:
Ezio Melotti35386712009-12-31 13:22:41 +0000636 with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
637 fp.write("print(42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000638
Ezio Melotti35386712009-12-31 13:22:41 +0000639 with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
640 fp.write("print(42 * 42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000641
Ezio Melotti35386712009-12-31 13:22:41 +0000642 with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp:
643 fp.write("bla bla bla\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000644
645 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
646 zipfp.writepy(TESTFN2)
647
648 names = zipfp.namelist()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000649 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
650 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000651 self.assertNotIn('mod2.txt', names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000652
653 finally:
654 shutil.rmtree(TESTFN2)
655
Ezio Melottiafd0d112009-07-15 17:17:17 +0000656 def test_write_non_pyfile(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000657 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
658 open(TESTFN, 'w').write('most definitely not a python file')
659 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
660 os.remove(TESTFN)
661
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000662
663
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000664class OtherTests(unittest.TestCase):
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +0000665 zips_with_bad_crc = {
666 zipfile.ZIP_STORED: (
667 b'PK\003\004\024\0\0\0\0\0 \213\212;:r'
668 b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af'
669 b'ilehello,AworldP'
670 b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:'
671 b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0'
672 b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi'
673 b'lePK\005\006\0\0\0\0\001\0\001\0003\000'
674 b'\0\0/\0\0\0\0\0'),
675 zipfile.ZIP_DEFLATED: (
676 b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA'
677 b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
678 b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0'
679 b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n'
680 b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05'
681 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00'
682 b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00'
683 b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'),
684 }
685
Ezio Melottiafd0d112009-07-15 17:17:17 +0000686 def test_unicode_filenames(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000687 with zipfile.ZipFile(TESTFN, "w") as zf:
688 zf.writestr("foo.txt", "Test for unicode filename")
689 zf.writestr("\xf6.txt", "Test for unicode filename")
Ezio Melottie9615932010-01-24 19:26:24 +0000690 self.assertIsInstance(zf.infolist()[0].filename, str)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000691
692 with zipfile.ZipFile(TESTFN, "r") as zf:
693 self.assertEqual(zf.filelist[0].filename, "foo.txt")
694 self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000695
Ezio Melottiafd0d112009-07-15 17:17:17 +0000696 def test_create_non_existent_file_for_append(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000697 if os.path.exists(TESTFN):
698 os.unlink(TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000699
Thomas Wouterscf297e42007-02-23 15:07:44 +0000700 filename = 'testfile.txt'
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000701 content = b'hello, world. this is some content.'
Guido van Rossumd8faa362007-04-27 19:54:29 +0000702
Thomas Wouterscf297e42007-02-23 15:07:44 +0000703 try:
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000704 with zipfile.ZipFile(TESTFN, 'a') as zf:
705 zf.writestr(filename, content)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000706 except IOError:
707 self.fail('Could not append data to a non-existent zip file.')
708
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000709 self.assertTrue(os.path.exists(TESTFN))
Thomas Wouterscf297e42007-02-23 15:07:44 +0000710
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000711 with zipfile.ZipFile(TESTFN, 'r') as zf:
712 self.assertEqual(zf.read(filename), content)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000713
Ezio Melottiafd0d112009-07-15 17:17:17 +0000714 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000715 # This test checks that the ZipFile constructor closes the file object
Ezio Melotti35386712009-12-31 13:22:41 +0000716 # it opens if there's an error in the file. If it doesn't, the
717 # traceback holds a reference to the ZipFile object and, indirectly,
718 # the file object.
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000719 # On Windows, this causes the os.unlink() call to fail because the
720 # underlying file is still open. This is SF bug #412214.
721 #
Ezio Melotti35386712009-12-31 13:22:41 +0000722 with open(TESTFN, "w") as fp:
723 fp.write("this is not a legal zip file\n")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000724 try:
725 zf = zipfile.ZipFile(TESTFN)
726 except zipfile.BadZipfile:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000727 pass
728
Ezio Melottiafd0d112009-07-15 17:17:17 +0000729 def test_is_zip_erroneous_file(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000730 """Check that is_zipfile() correctly identifies non-zip files."""
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000731 # - passing a filename
732 with open(TESTFN, "w") as fp:
733 fp.write("this is not a legal zip file\n")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000734 chk = zipfile.is_zipfile(TESTFN)
Ezio Melotti35386712009-12-31 13:22:41 +0000735 self.assertFalse(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000736 # - passing a file object
737 with open(TESTFN, "rb") as fp:
738 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000739 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000740 # - passing a file-like object
741 fp = io.BytesIO()
742 fp.write(b"this is not a legal zip file\n")
743 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000744 self.assertTrue(not chk)
Ezio Melotti35386712009-12-31 13:22:41 +0000745 fp.seek(0, 0)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000746 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000747 self.assertTrue(not chk)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000748
Ezio Melottiafd0d112009-07-15 17:17:17 +0000749 def test_is_zip_valid_file(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000750 """Check that is_zipfile() correctly identifies zip files."""
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000751 # - passing a filename
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000752 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
753 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
754
Guido van Rossumd8faa362007-04-27 19:54:29 +0000755 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000756 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000757 # - passing a file object
758 with open(TESTFN, "rb") as fp:
759 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000760 self.assertTrue(chk)
Ezio Melotti35386712009-12-31 13:22:41 +0000761 fp.seek(0, 0)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000762 zip_contents = fp.read()
763 # - passing a file-like object
764 fp = io.BytesIO()
765 fp.write(zip_contents)
766 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000767 self.assertTrue(chk)
Ezio Melotti35386712009-12-31 13:22:41 +0000768 fp.seek(0, 0)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000769 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000770 self.assertTrue(chk)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000771
Ezio Melottiafd0d112009-07-15 17:17:17 +0000772 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000773 # make sure we don't raise an AttributeError when a partially-constructed
774 # ZipFile instance is finalized; this tests for regression on SF tracker
775 # bug #403871.
776
777 # The bug we're testing for caused an AttributeError to be raised
778 # when a ZipFile instance was created for a file that did not
779 # exist; the .fp member was not initialized but was needed by the
780 # __del__() method. Since the AttributeError is in the __del__(),
781 # it is ignored, but the user should be sufficiently annoyed by
782 # the message on the output that regression will be noticed
783 # quickly.
784 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
785
Amaury Forgeot d'Arcbc347802009-07-28 22:18:57 +0000786 def test_empty_file_raises_BadZipFile(self):
787 f = open(TESTFN, 'w')
788 f.close()
789 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
790
Ezio Melotti35386712009-12-31 13:22:41 +0000791 with open(TESTFN, 'w') as fp:
792 fp.write("short file")
Amaury Forgeot d'Arcbc347802009-07-28 22:18:57 +0000793 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
794
Ezio Melottiafd0d112009-07-15 17:17:17 +0000795 def test_closed_zip_raises_RuntimeError(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000796 """Verify that testzip() doesn't swallow inappropriate exceptions."""
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000797 data = io.BytesIO()
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000798 with zipfile.ZipFile(data, mode="w") as zipf:
799 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000800
801 # This is correct; calling .read on a closed ZipFile should throw
802 # a RuntimeError, and so should calling .testzip. An earlier
803 # version of .testzip would swallow this exception (and any other)
804 # and report that the first file in the archive was corrupt.
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000805 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
806 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000807 self.assertRaises(RuntimeError, zipf.testzip)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000808 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Guido van Rossum814661e2007-07-18 22:07:29 +0000809 open(TESTFN, 'w').write('zipfile test data')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000810 self.assertRaises(RuntimeError, zipf.write, TESTFN)
811
Ezio Melottiafd0d112009-07-15 17:17:17 +0000812 def test_bad_constructor_mode(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000813 """Check that bad modes passed to ZipFile constructor are caught."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000814 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
815
Ezio Melottiafd0d112009-07-15 17:17:17 +0000816 def test_bad_open_mode(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000817 """Check that bad modes passed to ZipFile.open are caught."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000818 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
819 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
820
821 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000822 # read the data to make sure the file is there
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000823 zipf.read("foo.txt")
824 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000825
Ezio Melottiafd0d112009-07-15 17:17:17 +0000826 def test_read0(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000827 """Check that calling read(0) on a ZipExtFile object returns an empty
828 string and doesn't advance file pointer."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000829 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
830 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
831 # read the data to make sure the file is there
832 f = zipf.open("foo.txt")
833 for i in range(FIXEDTEST_SIZE):
834 self.assertEqual(f.read(0), b'')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000835
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000836 self.assertEqual(f.read(), b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000837
Ezio Melottiafd0d112009-07-15 17:17:17 +0000838 def test_open_non_existent_item(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000839 """Check that attempting to call open() for an item that doesn't
840 exist in the archive raises a RuntimeError."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000841 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
842 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000843
Ezio Melottiafd0d112009-07-15 17:17:17 +0000844 def test_bad_compression_mode(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000845 """Check that bad compression methods passed to ZipFile.open are
846 caught."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000847 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
848
Ezio Melottiafd0d112009-07-15 17:17:17 +0000849 def test_null_byte_in_filename(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000850 """Check that a filename containing a null byte is properly
851 terminated."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000852 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
853 zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
854 self.assertEqual(zipf.namelist(), ['foo.txt'])
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000855
Ezio Melottiafd0d112009-07-15 17:17:17 +0000856 def test_struct_sizes(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000857 """Check that ZIP internal structure sizes are calculated correctly."""
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000858 self.assertEqual(zipfile.sizeEndCentDir, 22)
859 self.assertEqual(zipfile.sizeCentralDir, 46)
860 self.assertEqual(zipfile.sizeEndCentDir64, 56)
861 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
862
Ezio Melottiafd0d112009-07-15 17:17:17 +0000863 def test_comments(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000864 """Check that comments on the archive are handled properly."""
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000865
866 # check default comment is empty
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000867 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
868 self.assertEqual(zipf.comment, b'')
869 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
870
871 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
872 self.assertEqual(zipfr.comment, b'')
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000873
874 # check a simple short comment
875 comment = b'Bravely taking to his feet, he beat a very brave retreat.'
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000876 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
877 zipf.comment = comment
878 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
879 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
880 self.assertEqual(zipf.comment, comment)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000881
882 # check a comment of max length
883 comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
884 comment2 = comment2.encode("ascii")
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000885 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
886 zipf.comment = comment2
887 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
888
889 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
890 self.assertEqual(zipfr.comment, comment2)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000891
892 # check a comment that is too long is truncated
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000893 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
894 zipf.comment = comment2 + b'oops'
895 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
896 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
897 self.assertEqual(zipfr.comment, comment2)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000898
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +0000899 def check_testzip_with_bad_crc(self, compression):
900 """Tests that files with bad CRCs return their name from testzip."""
901 zipdata = self.zips_with_bad_crc[compression]
902
903 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
904 # testzip returns the name of the first corrupt file, or None
905 self.assertEqual('afile', zipf.testzip())
906
907 def test_testzip_with_bad_crc_stored(self):
908 self.check_testzip_with_bad_crc(zipfile.ZIP_STORED)
909
910 @skipUnless(zlib, "requires zlib")
911 def test_testzip_with_bad_crc_deflated(self):
912 self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED)
913
914 def check_read_with_bad_crc(self, compression):
915 """Tests that files with bad CRCs raise a BadZipfile exception when read."""
916 zipdata = self.zips_with_bad_crc[compression]
917
918 # Using ZipFile.read()
919 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
920 self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile')
921
922 # Using ZipExtFile.read()
923 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
924 with zipf.open('afile', 'r') as corrupt_file:
925 self.assertRaises(zipfile.BadZipfile, corrupt_file.read)
926
927 # Same with small reads (in order to exercise the buffering logic)
928 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
929 with zipf.open('afile', 'r') as corrupt_file:
930 corrupt_file.MIN_READ_SIZE = 2
931 with self.assertRaises(zipfile.BadZipfile):
932 while corrupt_file.read(2):
933 pass
934
935 def test_read_with_bad_crc_stored(self):
936 self.check_read_with_bad_crc(zipfile.ZIP_STORED)
937
938 @skipUnless(zlib, "requires zlib")
939 def test_read_with_bad_crc_deflated(self):
940 self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED)
941
Antoine Pitrou6464d5f2010-09-12 14:51:20 +0000942 def check_read_return_size(self, compression):
943 # Issue #9837: ZipExtFile.read() shouldn't return more bytes
944 # than requested.
945 for test_size in (1, 4095, 4096, 4097, 16384):
946 file_size = test_size + 1
947 junk = b''.join(struct.pack('B', randint(0, 255))
948 for x in range(file_size))
949 with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf:
950 zipf.writestr('foo', junk)
951 with zipf.open('foo', 'r') as fp:
952 buf = fp.read(test_size)
953 self.assertEqual(len(buf), test_size)
954
955 def test_read_return_size_stored(self):
956 self.check_read_return_size(zipfile.ZIP_STORED)
957
958 @skipUnless(zlib, "requires zlib")
959 def test_read_return_size_deflated(self):
960 self.check_read_return_size(zipfile.ZIP_DEFLATED)
961
Guido van Rossumd8faa362007-04-27 19:54:29 +0000962 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000963 unlink(TESTFN)
964 unlink(TESTFN2)
965
Thomas Wouterscf297e42007-02-23 15:07:44 +0000966
967class DecryptionTests(unittest.TestCase):
Ezio Melotti35386712009-12-31 13:22:41 +0000968 """Check that ZIP decryption works. Since the library does not
969 support encryption at the moment, we use a pre-generated encrypted
970 ZIP file."""
Thomas Wouterscf297e42007-02-23 15:07:44 +0000971
972 data = (
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000973 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
974 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
975 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
976 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
977 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
978 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
979 b'\x00\x00L\x00\x00\x00\x00\x00' )
Christian Heimesfdab48e2008-01-20 09:06:41 +0000980 data2 = (
981 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
982 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
983 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
984 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
985 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
986 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
987 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
988 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Thomas Wouterscf297e42007-02-23 15:07:44 +0000989
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000990 plain = b'zipfile.py encryption test'
Christian Heimesfdab48e2008-01-20 09:06:41 +0000991 plain2 = b'\x00'*512
Thomas Wouterscf297e42007-02-23 15:07:44 +0000992
993 def setUp(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000994 with open(TESTFN, "wb") as fp:
995 fp.write(self.data)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000996 self.zip = zipfile.ZipFile(TESTFN, "r")
Ezio Melotti35386712009-12-31 13:22:41 +0000997 with open(TESTFN2, "wb") as fp:
998 fp.write(self.data2)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000999 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001000
1001 def tearDown(self):
1002 self.zip.close()
1003 os.unlink(TESTFN)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001004 self.zip2.close()
1005 os.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001006
Ezio Melottiafd0d112009-07-15 17:17:17 +00001007 def test_no_password(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +00001008 # Reading the encrypted file without password
1009 # must generate a RunTime exception
1010 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +00001011 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001012
Ezio Melottiafd0d112009-07-15 17:17:17 +00001013 def test_bad_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001014 self.zip.setpassword(b"perl")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001015 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +00001016 self.zip2.setpassword(b"perl")
1017 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001018
Ezio Melotti78ea2022009-09-12 18:41:20 +00001019 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001020 def test_good_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001021 self.zip.setpassword(b"python")
Ezio Melotti35386712009-12-31 13:22:41 +00001022 self.assertEqual(self.zip.read("test.txt"), self.plain)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001023 self.zip2.setpassword(b"12345")
Ezio Melotti35386712009-12-31 13:22:41 +00001024 self.assertEqual(self.zip2.read("zero"), self.plain2)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001025
Guido van Rossumd8faa362007-04-27 19:54:29 +00001026
1027class TestsWithRandomBinaryFiles(unittest.TestCase):
1028 def setUp(self):
1029 datacount = randint(16, 64)*1024 + randint(1, 1024)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001030 self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
1031 for i in range(datacount))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001032
1033 # Make a source file with some lines
Ezio Melotti35386712009-12-31 13:22:41 +00001034 with open(TESTFN, "wb") as fp:
1035 fp.write(self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001036
1037 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001038 unlink(TESTFN)
1039 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001040
Ezio Melottiafd0d112009-07-15 17:17:17 +00001041 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001042 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001043 with zipfile.ZipFile(f, "w", compression) as zipfp:
1044 zipfp.write(TESTFN, "another.name")
1045 zipfp.write(TESTFN, TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001046
Ezio Melottiafd0d112009-07-15 17:17:17 +00001047 def zip_test(self, f, compression):
1048 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001049
1050 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001051 with zipfile.ZipFile(f, "r", compression) as zipfp:
1052 testdata = zipfp.read(TESTFN)
1053 self.assertEqual(len(testdata), len(self.data))
1054 self.assertEqual(testdata, self.data)
1055 self.assertEqual(zipfp.read("another.name"), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001056
Ezio Melottiafd0d112009-07-15 17:17:17 +00001057 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001058 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001059 self.zip_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001060
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001061 @skipUnless(zlib, "requires zlib")
1062 def test_deflated(self):
1063 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1064 self.zip_test(f, zipfile.ZIP_DEFLATED)
1065
Ezio Melottiafd0d112009-07-15 17:17:17 +00001066 def zip_open_test(self, f, compression):
1067 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001068
1069 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001070 with zipfile.ZipFile(f, "r", compression) as zipfp:
1071 zipdata1 = []
1072 zipopen1 = zipfp.open(TESTFN)
1073 while True:
1074 read_data = zipopen1.read(256)
1075 if not read_data:
1076 break
1077 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001078
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001079 zipdata2 = []
1080 zipopen2 = zipfp.open("another.name")
1081 while True:
1082 read_data = zipopen2.read(256)
1083 if not read_data:
1084 break
1085 zipdata2.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001086
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001087 testdata1 = b''.join(zipdata1)
1088 self.assertEqual(len(testdata1), len(self.data))
1089 self.assertEqual(testdata1, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001090
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001091 testdata2 = b''.join(zipdata2)
Ezio Melotti35386712009-12-31 13:22:41 +00001092 self.assertEqual(len(testdata2), len(self.data))
1093 self.assertEqual(testdata2, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001094
Ezio Melottiafd0d112009-07-15 17:17:17 +00001095 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001096 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001097 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001098
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001099 @skipUnless(zlib, "requires zlib")
1100 def test_open_deflated(self):
1101 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1102 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
1103
Ezio Melottiafd0d112009-07-15 17:17:17 +00001104 def zip_random_open_test(self, f, compression):
1105 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001106
1107 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001108 with zipfile.ZipFile(f, "r", compression) as zipfp:
1109 zipdata1 = []
1110 zipopen1 = zipfp.open(TESTFN)
1111 while True:
1112 read_data = zipopen1.read(randint(1, 1024))
1113 if not read_data:
1114 break
1115 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001116
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001117 testdata = b''.join(zipdata1)
1118 self.assertEqual(len(testdata), len(self.data))
1119 self.assertEqual(testdata, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001120
Ezio Melottiafd0d112009-07-15 17:17:17 +00001121 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001122 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001123 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001124
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001125 @skipUnless(zlib, "requires zlib")
1126 def test_random_open_deflated(self):
1127 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1128 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
1129
Ezio Melotti76430242009-07-11 18:28:48 +00001130
Ezio Melotti78ea2022009-09-12 18:41:20 +00001131@skipUnless(zlib, "requires zlib")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001132class TestsWithMultipleOpens(unittest.TestCase):
1133 def setUp(self):
1134 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001135 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
1136 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
1137 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001138
Ezio Melottiafd0d112009-07-15 17:17:17 +00001139 def test_same_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001140 # Verify that (when the ZipFile is in control of creating file objects)
1141 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001142 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1143 zopen1 = zipf.open('ones')
1144 zopen2 = zipf.open('ones')
1145 data1 = zopen1.read(500)
1146 data2 = zopen2.read(500)
1147 data1 += zopen1.read(500)
1148 data2 += zopen2.read(500)
1149 self.assertEqual(data1, data2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001150
Ezio Melottiafd0d112009-07-15 17:17:17 +00001151 def test_different_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001152 # Verify that (when the ZipFile is in control of creating file objects)
1153 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001154 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1155 zopen1 = zipf.open('ones')
1156 zopen2 = zipf.open('twos')
1157 data1 = zopen1.read(500)
1158 data2 = zopen2.read(500)
1159 data1 += zopen1.read(500)
1160 data2 += zopen2.read(500)
1161 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1162 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001163
Ezio Melottiafd0d112009-07-15 17:17:17 +00001164 def test_interleaved(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001165 # Verify that (when the ZipFile is in control of creating file objects)
1166 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001167 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1168 zopen1 = zipf.open('ones')
1169 data1 = zopen1.read(500)
1170 zopen2 = zipf.open('twos')
1171 data2 = zopen2.read(500)
1172 data1 += zopen1.read(500)
1173 data2 += zopen2.read(500)
1174 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1175 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001176
1177 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001178 unlink(TESTFN2)
1179
Guido van Rossumd8faa362007-04-27 19:54:29 +00001180
Martin v. Löwis59e47792009-01-24 14:10:07 +00001181class TestWithDirectory(unittest.TestCase):
1182 def setUp(self):
1183 os.mkdir(TESTFN2)
1184
Ezio Melottiafd0d112009-07-15 17:17:17 +00001185 def test_extract_dir(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001186 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
1187 zipf.extractall(TESTFN2)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001188 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1189 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1190 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1191
Ezio Melottiafd0d112009-07-15 17:17:17 +00001192 def test_bug_6050(self):
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001193 # Extraction should succeed if directories already exist
1194 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottiafd0d112009-07-15 17:17:17 +00001195 self.test_extract_dir()
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001196
Ezio Melottiafd0d112009-07-15 17:17:17 +00001197 def test_store_dir(self):
Martin v. Löwis59e47792009-01-24 14:10:07 +00001198 os.mkdir(os.path.join(TESTFN2, "x"))
1199 zipf = zipfile.ZipFile(TESTFN, "w")
1200 zipf.write(os.path.join(TESTFN2, "x"), "x")
1201 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1202
1203 def tearDown(self):
1204 shutil.rmtree(TESTFN2)
1205 if os.path.exists(TESTFN):
Ezio Melotti76430242009-07-11 18:28:48 +00001206 unlink(TESTFN)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001207
Guido van Rossumd8faa362007-04-27 19:54:29 +00001208
1209class UniversalNewlineTests(unittest.TestCase):
1210 def setUp(self):
Guido van Rossum9c627722007-08-27 18:31:48 +00001211 self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001212 for i in range(FIXEDTEST_SIZE)]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001213 self.seps = ('\r', '\r\n', '\n')
1214 self.arcdata, self.arcfiles = {}, {}
1215 for n, s in enumerate(self.seps):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001216 b = s.encode("ascii")
1217 self.arcdata[s] = b.join(self.line_gen) + b
Guido van Rossumd8faa362007-04-27 19:54:29 +00001218 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001219 f = open(self.arcfiles[s], "wb")
1220 try:
1221 f.write(self.arcdata[s])
1222 finally:
1223 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001224
Ezio Melottiafd0d112009-07-15 17:17:17 +00001225 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001226 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001227 with zipfile.ZipFile(f, "w", compression) as zipfp:
1228 for fn in self.arcfiles.values():
1229 zipfp.write(fn, fn)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001230
Ezio Melottiafd0d112009-07-15 17:17:17 +00001231 def read_test(self, f, compression):
1232 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001233
1234 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001235 with zipfile.ZipFile(f, "r") as zipfp:
1236 for sep, fn in self.arcfiles.items():
1237 zipdata = zipfp.open(fn, "rU").read()
1238 self.assertEqual(self.arcdata[sep], zipdata)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001239
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001240 def readline_read_test(self, f, compression):
1241 self.make_test_archive(f, compression)
1242
1243 # Read the ZIP archive
1244 zipfp = zipfile.ZipFile(f, "r")
1245 for sep, fn in self.arcfiles.items():
1246 zipopen = zipfp.open(fn, "rU")
1247 data = b''
1248 while True:
1249 read = zipopen.readline()
1250 if not read:
1251 break
1252 data += read
1253
1254 read = zipopen.read(5)
1255 if not read:
1256 break
1257 data += read
1258
1259 self.assertEqual(data, self.arcdata['\n'])
1260
1261 zipfp.close()
1262
Ezio Melottiafd0d112009-07-15 17:17:17 +00001263 def readline_test(self, f, compression):
1264 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001265
1266 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001267 with zipfile.ZipFile(f, "r") as zipfp:
1268 for sep, fn in self.arcfiles.items():
1269 zipopen = zipfp.open(fn, "rU")
1270 for line in self.line_gen:
1271 linedata = zipopen.readline()
1272 self.assertEqual(linedata, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001273
Ezio Melottiafd0d112009-07-15 17:17:17 +00001274 def readlines_test(self, f, compression):
1275 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001276
1277 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001278 with zipfile.ZipFile(f, "r") as zipfp:
1279 for sep, fn in self.arcfiles.items():
1280 ziplines = zipfp.open(fn, "rU").readlines()
1281 for line, zipline in zip(self.line_gen, ziplines):
1282 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001283
Ezio Melottiafd0d112009-07-15 17:17:17 +00001284 def iterlines_test(self, f, compression):
1285 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001286
1287 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001288 with zipfile.ZipFile(f, "r") as zipfp:
1289 for sep, fn in self.arcfiles.items():
1290 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
1291 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001292
Ezio Melottiafd0d112009-07-15 17:17:17 +00001293 def test_read_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001294 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001295 self.read_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001296
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001297 def test_readline_read_stored(self):
1298 # Issue #7610: calls to readline() interleaved with calls to read().
1299 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1300 self.readline_read_test(f, zipfile.ZIP_STORED)
1301
Ezio Melottiafd0d112009-07-15 17:17:17 +00001302 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001303 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001304 self.readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001305
Ezio Melottiafd0d112009-07-15 17:17:17 +00001306 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001307 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001308 self.readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001309
Ezio Melottiafd0d112009-07-15 17:17:17 +00001310 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001311 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001312 self.iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001313
Ezio Melotti76430242009-07-11 18:28:48 +00001314 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001315 def test_read_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001316 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001317 self.read_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001318
Ezio Melotti76430242009-07-11 18:28:48 +00001319 @skipUnless(zlib, "requires zlib")
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001320 def test_readline_read_deflated(self):
1321 # Issue #7610: calls to readline() interleaved with calls to read().
1322 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1323 self.readline_read_test(f, zipfile.ZIP_DEFLATED)
1324
1325 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001326 def test_readline_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001327 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001328 self.readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001329
Ezio Melotti76430242009-07-11 18:28:48 +00001330 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001331 def test_readlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001332 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001333 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001334
Ezio Melotti76430242009-07-11 18:28:48 +00001335 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001336 def test_iterlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001337 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001338 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001339
1340 def tearDown(self):
1341 for sep, fn in self.arcfiles.items():
1342 os.remove(fn)
Ezio Melotti76430242009-07-11 18:28:48 +00001343 unlink(TESTFN)
1344 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001345
1346
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001347def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +00001348 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1349 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti76430242009-07-11 18:28:48 +00001350 TestWithDirectory, UniversalNewlineTests,
1351 TestsWithRandomBinaryFiles)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001352
1353if __name__ == "__main__":
1354 test_main()