blob: c02d8731f87522ba6d59b880107a9edcecc92636 [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
Georg Brandl268e4d42010-10-14 06:59:45 +0000962 def test_empty_zipfile(self):
963 # Check that creating a file in 'w' or 'a' mode and closing without
964 # adding any files to the archives creates a valid empty ZIP file
965 zipf = zipfile.ZipFile(TESTFN, mode="w")
966 zipf.close()
967 try:
968 zipf = zipfile.ZipFile(TESTFN, mode="r")
969 except zipfile.BadZipFile:
970 self.fail("Unable to create empty ZIP file in 'w' mode")
971
972 zipf = zipfile.ZipFile(TESTFN, mode="a")
973 zipf.close()
974 try:
975 zipf = zipfile.ZipFile(TESTFN, mode="r")
976 except:
977 self.fail("Unable to create empty ZIP file in 'a' mode")
978
979 def test_open_empty_file(self):
980 # Issue 1710703: Check that opening a file with less than 22 bytes
981 # raises a BadZipfile exception (rather than the previously unhelpful
982 # IOError)
983 f = open(TESTFN, 'w')
984 f.close()
985 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r')
986
Guido van Rossumd8faa362007-04-27 19:54:29 +0000987 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000988 unlink(TESTFN)
989 unlink(TESTFN2)
990
Thomas Wouterscf297e42007-02-23 15:07:44 +0000991
992class DecryptionTests(unittest.TestCase):
Ezio Melotti35386712009-12-31 13:22:41 +0000993 """Check that ZIP decryption works. Since the library does not
994 support encryption at the moment, we use a pre-generated encrypted
995 ZIP file."""
Thomas Wouterscf297e42007-02-23 15:07:44 +0000996
997 data = (
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000998 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
999 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
1000 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
1001 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
1002 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
1003 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
1004 b'\x00\x00L\x00\x00\x00\x00\x00' )
Christian Heimesfdab48e2008-01-20 09:06:41 +00001005 data2 = (
1006 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
1007 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
1008 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
1009 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
1010 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
1011 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
1012 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
1013 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Thomas Wouterscf297e42007-02-23 15:07:44 +00001014
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001015 plain = b'zipfile.py encryption test'
Christian Heimesfdab48e2008-01-20 09:06:41 +00001016 plain2 = b'\x00'*512
Thomas Wouterscf297e42007-02-23 15:07:44 +00001017
1018 def setUp(self):
Ezio Melotti35386712009-12-31 13:22:41 +00001019 with open(TESTFN, "wb") as fp:
1020 fp.write(self.data)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001021 self.zip = zipfile.ZipFile(TESTFN, "r")
Ezio Melotti35386712009-12-31 13:22:41 +00001022 with open(TESTFN2, "wb") as fp:
1023 fp.write(self.data2)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001024 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001025
1026 def tearDown(self):
1027 self.zip.close()
1028 os.unlink(TESTFN)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001029 self.zip2.close()
1030 os.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001031
Ezio Melottiafd0d112009-07-15 17:17:17 +00001032 def test_no_password(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +00001033 # Reading the encrypted file without password
1034 # must generate a RunTime exception
1035 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +00001036 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001037
Ezio Melottiafd0d112009-07-15 17:17:17 +00001038 def test_bad_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001039 self.zip.setpassword(b"perl")
Thomas Wouterscf297e42007-02-23 15:07:44 +00001040 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +00001041 self.zip2.setpassword(b"perl")
1042 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001043
Ezio Melotti78ea2022009-09-12 18:41:20 +00001044 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001045 def test_good_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001046 self.zip.setpassword(b"python")
Ezio Melotti35386712009-12-31 13:22:41 +00001047 self.assertEqual(self.zip.read("test.txt"), self.plain)
Christian Heimesfdab48e2008-01-20 09:06:41 +00001048 self.zip2.setpassword(b"12345")
Ezio Melotti35386712009-12-31 13:22:41 +00001049 self.assertEqual(self.zip2.read("zero"), self.plain2)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001050
Guido van Rossumd8faa362007-04-27 19:54:29 +00001051
1052class TestsWithRandomBinaryFiles(unittest.TestCase):
1053 def setUp(self):
1054 datacount = randint(16, 64)*1024 + randint(1, 1024)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001055 self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
1056 for i in range(datacount))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001057
1058 # Make a source file with some lines
Ezio Melotti35386712009-12-31 13:22:41 +00001059 with open(TESTFN, "wb") as fp:
1060 fp.write(self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001061
1062 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001063 unlink(TESTFN)
1064 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001065
Ezio Melottiafd0d112009-07-15 17:17:17 +00001066 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001067 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001068 with zipfile.ZipFile(f, "w", compression) as zipfp:
1069 zipfp.write(TESTFN, "another.name")
1070 zipfp.write(TESTFN, TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001071
Ezio Melottiafd0d112009-07-15 17:17:17 +00001072 def zip_test(self, f, compression):
1073 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001074
1075 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001076 with zipfile.ZipFile(f, "r", compression) as zipfp:
1077 testdata = zipfp.read(TESTFN)
1078 self.assertEqual(len(testdata), len(self.data))
1079 self.assertEqual(testdata, self.data)
1080 self.assertEqual(zipfp.read("another.name"), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001081
Ezio Melottiafd0d112009-07-15 17:17:17 +00001082 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001083 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001084 self.zip_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001085
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001086 @skipUnless(zlib, "requires zlib")
1087 def test_deflated(self):
1088 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1089 self.zip_test(f, zipfile.ZIP_DEFLATED)
1090
Ezio Melottiafd0d112009-07-15 17:17:17 +00001091 def zip_open_test(self, f, compression):
1092 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001093
1094 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001095 with zipfile.ZipFile(f, "r", compression) as zipfp:
1096 zipdata1 = []
1097 zipopen1 = zipfp.open(TESTFN)
1098 while True:
1099 read_data = zipopen1.read(256)
1100 if not read_data:
1101 break
1102 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001103
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001104 zipdata2 = []
1105 zipopen2 = zipfp.open("another.name")
1106 while True:
1107 read_data = zipopen2.read(256)
1108 if not read_data:
1109 break
1110 zipdata2.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001111
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001112 testdata1 = b''.join(zipdata1)
1113 self.assertEqual(len(testdata1), len(self.data))
1114 self.assertEqual(testdata1, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001115
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001116 testdata2 = b''.join(zipdata2)
Ezio Melotti35386712009-12-31 13:22:41 +00001117 self.assertEqual(len(testdata2), len(self.data))
1118 self.assertEqual(testdata2, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001119
Ezio Melottiafd0d112009-07-15 17:17:17 +00001120 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001121 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001122 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001123
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001124 @skipUnless(zlib, "requires zlib")
1125 def test_open_deflated(self):
1126 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1127 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
1128
Ezio Melottiafd0d112009-07-15 17:17:17 +00001129 def zip_random_open_test(self, f, compression):
1130 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001131
1132 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001133 with zipfile.ZipFile(f, "r", compression) as zipfp:
1134 zipdata1 = []
1135 zipopen1 = zipfp.open(TESTFN)
1136 while True:
1137 read_data = zipopen1.read(randint(1, 1024))
1138 if not read_data:
1139 break
1140 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001141
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001142 testdata = b''.join(zipdata1)
1143 self.assertEqual(len(testdata), len(self.data))
1144 self.assertEqual(testdata, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001145
Ezio Melottiafd0d112009-07-15 17:17:17 +00001146 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001147 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001148 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001149
Antoine Pitrou7c8bcb62010-08-12 15:11:50 +00001150 @skipUnless(zlib, "requires zlib")
1151 def test_random_open_deflated(self):
1152 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1153 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
1154
Ezio Melotti76430242009-07-11 18:28:48 +00001155
Ezio Melotti78ea2022009-09-12 18:41:20 +00001156@skipUnless(zlib, "requires zlib")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001157class TestsWithMultipleOpens(unittest.TestCase):
1158 def setUp(self):
1159 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001160 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
1161 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
1162 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001163
Ezio Melottiafd0d112009-07-15 17:17:17 +00001164 def test_same_file(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 zopen2 = zipf.open('ones')
1170 data1 = zopen1.read(500)
1171 data2 = zopen2.read(500)
1172 data1 += zopen1.read(500)
1173 data2 += zopen2.read(500)
1174 self.assertEqual(data1, data2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001175
Ezio Melottiafd0d112009-07-15 17:17:17 +00001176 def test_different_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001177 # Verify that (when the ZipFile is in control of creating file objects)
1178 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001179 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1180 zopen1 = zipf.open('ones')
1181 zopen2 = zipf.open('twos')
1182 data1 = zopen1.read(500)
1183 data2 = zopen2.read(500)
1184 data1 += zopen1.read(500)
1185 data2 += zopen2.read(500)
1186 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1187 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001188
Ezio Melottiafd0d112009-07-15 17:17:17 +00001189 def test_interleaved(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001190 # Verify that (when the ZipFile is in control of creating file objects)
1191 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001192 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1193 zopen1 = zipf.open('ones')
1194 data1 = zopen1.read(500)
1195 zopen2 = zipf.open('twos')
1196 data2 = zopen2.read(500)
1197 data1 += zopen1.read(500)
1198 data2 += zopen2.read(500)
1199 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1200 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001201
1202 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001203 unlink(TESTFN2)
1204
Guido van Rossumd8faa362007-04-27 19:54:29 +00001205
Martin v. Löwis59e47792009-01-24 14:10:07 +00001206class TestWithDirectory(unittest.TestCase):
1207 def setUp(self):
1208 os.mkdir(TESTFN2)
1209
Ezio Melottiafd0d112009-07-15 17:17:17 +00001210 def test_extract_dir(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001211 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
1212 zipf.extractall(TESTFN2)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001213 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1214 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1215 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1216
Ezio Melottiafd0d112009-07-15 17:17:17 +00001217 def test_bug_6050(self):
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001218 # Extraction should succeed if directories already exist
1219 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottiafd0d112009-07-15 17:17:17 +00001220 self.test_extract_dir()
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001221
Ezio Melottiafd0d112009-07-15 17:17:17 +00001222 def test_store_dir(self):
Martin v. Löwis59e47792009-01-24 14:10:07 +00001223 os.mkdir(os.path.join(TESTFN2, "x"))
1224 zipf = zipfile.ZipFile(TESTFN, "w")
1225 zipf.write(os.path.join(TESTFN2, "x"), "x")
1226 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1227
1228 def tearDown(self):
1229 shutil.rmtree(TESTFN2)
1230 if os.path.exists(TESTFN):
Ezio Melotti76430242009-07-11 18:28:48 +00001231 unlink(TESTFN)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001232
Guido van Rossumd8faa362007-04-27 19:54:29 +00001233
1234class UniversalNewlineTests(unittest.TestCase):
1235 def setUp(self):
Guido van Rossum9c627722007-08-27 18:31:48 +00001236 self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001237 for i in range(FIXEDTEST_SIZE)]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001238 self.seps = ('\r', '\r\n', '\n')
1239 self.arcdata, self.arcfiles = {}, {}
1240 for n, s in enumerate(self.seps):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001241 b = s.encode("ascii")
1242 self.arcdata[s] = b.join(self.line_gen) + b
Guido van Rossumd8faa362007-04-27 19:54:29 +00001243 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001244 f = open(self.arcfiles[s], "wb")
1245 try:
1246 f.write(self.arcdata[s])
1247 finally:
1248 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001249
Ezio Melottiafd0d112009-07-15 17:17:17 +00001250 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001251 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001252 with zipfile.ZipFile(f, "w", compression) as zipfp:
1253 for fn in self.arcfiles.values():
1254 zipfp.write(fn, fn)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001255
Ezio Melottiafd0d112009-07-15 17:17:17 +00001256 def read_test(self, f, compression):
1257 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001258
1259 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001260 with zipfile.ZipFile(f, "r") as zipfp:
1261 for sep, fn in self.arcfiles.items():
1262 zipdata = zipfp.open(fn, "rU").read()
1263 self.assertEqual(self.arcdata[sep], zipdata)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001264
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001265 def readline_read_test(self, f, compression):
1266 self.make_test_archive(f, compression)
1267
1268 # Read the ZIP archive
1269 zipfp = zipfile.ZipFile(f, "r")
1270 for sep, fn in self.arcfiles.items():
1271 zipopen = zipfp.open(fn, "rU")
1272 data = b''
1273 while True:
1274 read = zipopen.readline()
1275 if not read:
1276 break
1277 data += read
1278
1279 read = zipopen.read(5)
1280 if not read:
1281 break
1282 data += read
1283
1284 self.assertEqual(data, self.arcdata['\n'])
1285
1286 zipfp.close()
1287
Ezio Melottiafd0d112009-07-15 17:17:17 +00001288 def readline_test(self, f, compression):
1289 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001290
1291 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001292 with zipfile.ZipFile(f, "r") as zipfp:
1293 for sep, fn in self.arcfiles.items():
1294 zipopen = zipfp.open(fn, "rU")
1295 for line in self.line_gen:
1296 linedata = zipopen.readline()
1297 self.assertEqual(linedata, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001298
Ezio Melottiafd0d112009-07-15 17:17:17 +00001299 def readlines_test(self, f, compression):
1300 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001301
1302 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001303 with zipfile.ZipFile(f, "r") as zipfp:
1304 for sep, fn in self.arcfiles.items():
1305 ziplines = zipfp.open(fn, "rU").readlines()
1306 for line, zipline in zip(self.line_gen, ziplines):
1307 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001308
Ezio Melottiafd0d112009-07-15 17:17:17 +00001309 def iterlines_test(self, f, compression):
1310 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001311
1312 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001313 with zipfile.ZipFile(f, "r") as zipfp:
1314 for sep, fn in self.arcfiles.items():
1315 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
1316 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001317
Ezio Melottiafd0d112009-07-15 17:17:17 +00001318 def test_read_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001319 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001320 self.read_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001321
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001322 def test_readline_read_stored(self):
1323 # Issue #7610: calls to readline() interleaved with calls to read().
1324 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1325 self.readline_read_test(f, zipfile.ZIP_STORED)
1326
Ezio Melottiafd0d112009-07-15 17:17:17 +00001327 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001328 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001329 self.readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001330
Ezio Melottiafd0d112009-07-15 17:17:17 +00001331 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001332 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001333 self.readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001334
Ezio Melottiafd0d112009-07-15 17:17:17 +00001335 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001336 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001337 self.iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001338
Ezio Melotti76430242009-07-11 18:28:48 +00001339 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001340 def test_read_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001341 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001342 self.read_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001343
Ezio Melotti76430242009-07-11 18:28:48 +00001344 @skipUnless(zlib, "requires zlib")
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001345 def test_readline_read_deflated(self):
1346 # Issue #7610: calls to readline() interleaved with calls to read().
1347 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1348 self.readline_read_test(f, zipfile.ZIP_DEFLATED)
1349
1350 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001351 def test_readline_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001352 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001353 self.readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001354
Ezio Melotti76430242009-07-11 18:28:48 +00001355 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001356 def test_readlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001357 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001358 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001359
Ezio Melotti76430242009-07-11 18:28:48 +00001360 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001361 def test_iterlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001362 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001363 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001364
1365 def tearDown(self):
1366 for sep, fn in self.arcfiles.items():
1367 os.remove(fn)
Ezio Melotti76430242009-07-11 18:28:48 +00001368 unlink(TESTFN)
1369 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001370
1371
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001372def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +00001373 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1374 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti76430242009-07-11 18:28:48 +00001375 TestWithDirectory, UniversalNewlineTests,
1376 TestsWithRandomBinaryFiles)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001377
1378if __name__ == "__main__":
1379 test_main()