blob: db2b34b71f17ab1c9e26747429e6a0c46949f881 [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
Guido van Rossumd6ca5462007-05-22 01:29:33 +00006import zipfile, os, unittest, sys, shutil, struct, io
Tim Petersa45cacf2004-08-20 03:47:14 +00007
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00008from tempfile import TemporaryFile
Guido van Rossumd8faa362007-04-27 19:54:29 +00009from random import randint, random
Tim Petersa19a1682001-03-29 04:36:09 +000010
Guido van Rossumd8faa362007-04-27 19:54:29 +000011import test.test_support as support
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000012from test.test_support import TESTFN, run_unittest
Guido van Rossum368f04a2000-04-10 13:23:04 +000013
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000014TESTFN2 = TESTFN + "2"
Guido van Rossumb5a755e2007-07-18 18:15:48 +000015FIXEDTEST_SIZE = 1000
Guido van Rossum368f04a2000-04-10 13:23:04 +000016
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000017class TestsWithSourceFile(unittest.TestCase):
18 def setUp(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +000019 self.line_gen = (bytes("Zipfile test line %d. random float: %f" %
Guido van Rossum9c627722007-08-27 18:31:48 +000020 (i, random()), "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +000021 for i in range(FIXEDTEST_SIZE))
22 self.data = b'\n'.join(self.line_gen) + b'\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000023
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000024 # Make a source file with some lines
25 fp = open(TESTFN, "wb")
26 fp.write(self.data)
27 fp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000028
Guido van Rossumd8faa362007-04-27 19:54:29 +000029 def makeTestArchive(self, f, compression):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000030 # Create the ZIP archive
31 zipfp = zipfile.ZipFile(f, "w", compression)
Skip Montanaro7a98be22007-08-16 14:35:24 +000032 zipfp.write(TESTFN, "another.name")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000033 zipfp.write(TESTFN, TESTFN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000034 zipfp.writestr("strfile", self.data)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000035 zipfp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000036
Guido van Rossumd8faa362007-04-27 19:54:29 +000037 def zipTest(self, f, compression):
38 self.makeTestArchive(f, compression)
39
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000040 # Read the ZIP archive
41 zipfp = zipfile.ZipFile(f, "r", compression)
42 self.assertEqual(zipfp.read(TESTFN), self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +000043 self.assertEqual(zipfp.read("another.name"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000044 self.assertEqual(zipfp.read("strfile"), self.data)
45
46 # Print the ZIP directory
Guido van Rossumd6ca5462007-05-22 01:29:33 +000047 fp = io.StringIO()
48 zipfp.printdir(file=fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000049
50 directory = fp.getvalue()
51 lines = directory.splitlines()
52 self.assertEquals(len(lines), 4) # Number of files + header
53
54 self.assert_('File Name' in lines[0])
55 self.assert_('Modified' in lines[0])
56 self.assert_('Size' in lines[0])
57
58 fn, date, time, size = lines[1].split()
59 self.assertEquals(fn, 'another.name')
60 # XXX: timestamp is not tested
61 self.assertEquals(size, str(len(self.data)))
62
63 # Check the namelist
64 names = zipfp.namelist()
65 self.assertEquals(len(names), 3)
66 self.assert_(TESTFN in names)
Skip Montanaro7a98be22007-08-16 14:35:24 +000067 self.assert_("another.name" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068 self.assert_("strfile" in names)
69
70 # Check infolist
71 infos = zipfp.infolist()
72 names = [ i.filename for i in infos ]
73 self.assertEquals(len(names), 3)
74 self.assert_(TESTFN in names)
Skip Montanaro7a98be22007-08-16 14:35:24 +000075 self.assert_("another.name" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076 self.assert_("strfile" in names)
77 for i in infos:
78 self.assertEquals(i.file_size, len(self.data))
79
80 # check getinfo
Skip Montanaro7a98be22007-08-16 14:35:24 +000081 for nm in (TESTFN, "another.name", "strfile"):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000082 info = zipfp.getinfo(nm)
83 self.assertEquals(info.filename, nm)
84 self.assertEquals(info.file_size, len(self.data))
85
86 # Check that testzip doesn't raise an exception
87 zipfp.testzip()
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000088 zipfp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000089
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000090 def testStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +000091 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000092 self.zipTest(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +000093
Guido van Rossumd8faa362007-04-27 19:54:29 +000094 def zipOpenTest(self, f, compression):
95 self.makeTestArchive(f, compression)
96
97 # Read the ZIP archive
98 zipfp = zipfile.ZipFile(f, "r", compression)
99 zipdata1 = []
100 zipopen1 = zipfp.open(TESTFN)
101 while 1:
102 read_data = zipopen1.read(256)
103 if not read_data:
104 break
105 zipdata1.append(read_data)
106
107 zipdata2 = []
Skip Montanaro7a98be22007-08-16 14:35:24 +0000108 zipopen2 = zipfp.open("another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000109 while 1:
110 read_data = zipopen2.read(256)
111 if not read_data:
112 break
113 zipdata2.append(read_data)
114
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000115 self.assertEqual(b''.join(zipdata1), self.data)
116 self.assertEqual(b''.join(zipdata2), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000117 zipfp.close()
118
119 def testOpenStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000120 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000121 self.zipOpenTest(f, zipfile.ZIP_STORED)
122
123 def zipRandomOpenTest(self, f, compression):
124 self.makeTestArchive(f, compression)
125
126 # Read the ZIP archive
127 zipfp = zipfile.ZipFile(f, "r", compression)
128 zipdata1 = []
129 zipopen1 = zipfp.open(TESTFN)
130 while 1:
131 read_data = zipopen1.read(randint(1, 1024))
132 if not read_data:
133 break
134 zipdata1.append(read_data)
135
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000136 self.assertEqual(b''.join(zipdata1), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000137 zipfp.close()
138
139 def testRandomOpenStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000140 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000141 self.zipRandomOpenTest(f, zipfile.ZIP_STORED)
142
143 def zipReadlineTest(self, f, compression):
144 self.makeTestArchive(f, compression)
145
146 # Read the ZIP archive
147 zipfp = zipfile.ZipFile(f, "r")
148 zipopen = zipfp.open(TESTFN)
149 for line in self.line_gen:
150 linedata = zipopen.readline()
151 self.assertEqual(linedata, line + '\n')
152
153 zipfp.close()
154
155 def zipReadlinesTest(self, f, compression):
156 self.makeTestArchive(f, compression)
157
158 # Read the ZIP archive
159 zipfp = zipfile.ZipFile(f, "r")
160 ziplines = zipfp.open(TESTFN).readlines()
161 for line, zipline in zip(self.line_gen, ziplines):
162 self.assertEqual(zipline, line + '\n')
163
164 zipfp.close()
165
166 def zipIterlinesTest(self, f, compression):
167 self.makeTestArchive(f, compression)
168
169 # Read the ZIP archive
170 zipfp = zipfile.ZipFile(f, "r")
171 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
172 self.assertEqual(zipline, line + '\n')
173
174 zipfp.close()
175
176 def testReadlineStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000177 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000178 self.zipReadlineTest(f, zipfile.ZIP_STORED)
179
180 def testReadlinesStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000181 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000182 self.zipReadlinesTest(f, zipfile.ZIP_STORED)
183
184 def testIterlinesStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000185 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000186 self.zipIterlinesTest(f, zipfile.ZIP_STORED)
187
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000188 if zlib:
189 def testDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000190 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000191 self.zipTest(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000192
Guido van Rossumd8faa362007-04-27 19:54:29 +0000193 def testOpenDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000194 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000195 self.zipOpenTest(f, zipfile.ZIP_DEFLATED)
196
197 def testRandomOpenDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000198 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000199 self.zipRandomOpenTest(f, zipfile.ZIP_DEFLATED)
200
201 def testReadlineDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000202 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000203 self.zipReadlineTest(f, zipfile.ZIP_DEFLATED)
204
205 def testReadlinesDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000206 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000207 self.zipReadlinesTest(f, zipfile.ZIP_DEFLATED)
208
209 def testIterlinesDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000210 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000211 self.zipIterlinesTest(f, zipfile.ZIP_DEFLATED)
212
213 def testLowCompression(self):
214 # Checks for cases where compressed data is larger than original
215 # Create the ZIP archive
216 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
217 zipfp.writestr("strfile", '12')
218 zipfp.close()
219
220 # Get an open object for strfile
221 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED)
222 openobj = zipfp.open("strfile")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000223 self.assertEqual(openobj.read(1), b'1')
224 self.assertEqual(openobj.read(1), b'2')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000225
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000226 def testAbsoluteArcnames(self):
227 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
228 zipfp.write(TESTFN, "/absolute")
229 zipfp.close()
230
231 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
232 self.assertEqual(zipfp.namelist(), ["absolute"])
233 zipfp.close()
Tim Peters32cbc962006-02-20 21:42:18 +0000234
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000235 def testAppendToZipFile(self):
236 # Test appending to an existing zipfile
237 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
238 zipfp.write(TESTFN, TESTFN)
239 zipfp.close()
240 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
241 zipfp.writestr("strfile", self.data)
242 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
243 zipfp.close()
244
245 def testAppendToNonZipFile(self):
246 # Test appending to an existing file that is not a zipfile
247 # NOTE: this test fails if len(d) < 22 because of the first
248 # line "fpin.seek(-22, 2)" in _EndRecData
Guido van Rossum9c627722007-08-27 18:31:48 +0000249 d = b'I am not a ZipFile!'*10
Guido van Rossum814661e2007-07-18 22:07:29 +0000250 f = open(TESTFN2, 'wb')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000251 f.write(d)
252 f.close()
253 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
254 zipfp.write(TESTFN, TESTFN)
255 zipfp.close()
256
Guido van Rossum814661e2007-07-18 22:07:29 +0000257 f = open(TESTFN2, 'rb')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000258 f.seek(len(d))
259 zipfp = zipfile.ZipFile(f, "r")
260 self.assertEqual(zipfp.namelist(), [TESTFN])
261 zipfp.close()
262 f.close()
263
264 def test_WriteDefaultName(self):
265 # Check that calling ZipFile.write without arcname specified produces the expected result
266 zipfp = zipfile.ZipFile(TESTFN2, "w")
267 zipfp.write(TESTFN)
Guido van Rossum814661e2007-07-18 22:07:29 +0000268 self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read())
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000269 zipfp.close()
270
271 def test_PerFileCompression(self):
272 # Check that files within a Zip archive can have different compression options
273 zipfp = zipfile.ZipFile(TESTFN2, "w")
274 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
275 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
276 sinfo = zipfp.getinfo('storeme')
277 dinfo = zipfp.getinfo('deflateme')
278 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
279 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
280 zipfp.close()
281
282 def test_WriteToReadonly(self):
283 # Check that trying to call write() on a readonly ZipFile object
284 # raises a RuntimeError
285 zipf = zipfile.ZipFile(TESTFN2, mode="w")
286 zipf.writestr("somefile.txt", "bogus")
287 zipf.close()
288 zipf = zipfile.ZipFile(TESTFN2, mode="r")
289 self.assertRaises(RuntimeError, zipf.write, TESTFN)
290 zipf.close()
291
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000292 def tearDown(self):
293 os.remove(TESTFN)
294 os.remove(TESTFN2)
295
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000296class TestZip64InSmallFiles(unittest.TestCase):
297 # These tests test the ZIP64 functionality without using large files,
298 # see test_zipfile64 for proper tests.
299
300 def setUp(self):
301 self._limit = zipfile.ZIP64_LIMIT
302 zipfile.ZIP64_LIMIT = 5
303
Guido van Rossum9c627722007-08-27 18:31:48 +0000304 line_gen = (bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000305 for i in range(0, FIXEDTEST_SIZE))
306 self.data = b'\n'.join(line_gen)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000307
308 # Make a source file with some lines
309 fp = open(TESTFN, "wb")
310 fp.write(self.data)
311 fp.close()
312
313 def largeFileExceptionTest(self, f, compression):
314 zipfp = zipfile.ZipFile(f, "w", compression)
315 self.assertRaises(zipfile.LargeZipFile,
Skip Montanaro7a98be22007-08-16 14:35:24 +0000316 zipfp.write, TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000317 zipfp.close()
318
319 def largeFileExceptionTest2(self, f, compression):
320 zipfp = zipfile.ZipFile(f, "w", compression)
321 self.assertRaises(zipfile.LargeZipFile,
Skip Montanaro7a98be22007-08-16 14:35:24 +0000322 zipfp.writestr, "another.name", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000323 zipfp.close()
324
325 def testLargeFileException(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000326 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000327 self.largeFileExceptionTest(f, zipfile.ZIP_STORED)
328 self.largeFileExceptionTest2(f, zipfile.ZIP_STORED)
329
330 def zipTest(self, f, compression):
331 # Create the ZIP archive
332 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000333 zipfp.write(TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000334 zipfp.write(TESTFN, TESTFN)
335 zipfp.writestr("strfile", self.data)
336 zipfp.close()
337
338 # Read the ZIP archive
339 zipfp = zipfile.ZipFile(f, "r", compression)
340 self.assertEqual(zipfp.read(TESTFN), self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000341 self.assertEqual(zipfp.read("another.name"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000342 self.assertEqual(zipfp.read("strfile"), self.data)
343
344 # Print the ZIP directory
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000345 fp = io.StringIO()
346 zipfp.printdir(fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000347
348 directory = fp.getvalue()
349 lines = directory.splitlines()
350 self.assertEquals(len(lines), 4) # Number of files + header
351
352 self.assert_('File Name' in lines[0])
353 self.assert_('Modified' in lines[0])
354 self.assert_('Size' in lines[0])
355
356 fn, date, time, size = lines[1].split()
357 self.assertEquals(fn, 'another.name')
358 # XXX: timestamp is not tested
359 self.assertEquals(size, str(len(self.data)))
360
361 # Check the namelist
362 names = zipfp.namelist()
363 self.assertEquals(len(names), 3)
364 self.assert_(TESTFN in names)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000365 self.assert_("another.name" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000366 self.assert_("strfile" in names)
367
368 # Check infolist
369 infos = zipfp.infolist()
370 names = [ i.filename for i in infos ]
371 self.assertEquals(len(names), 3)
372 self.assert_(TESTFN in names)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000373 self.assert_("another.name" in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000374 self.assert_("strfile" in names)
375 for i in infos:
376 self.assertEquals(i.file_size, len(self.data))
377
378 # check getinfo
Skip Montanaro7a98be22007-08-16 14:35:24 +0000379 for nm in (TESTFN, "another.name", "strfile"):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000380 info = zipfp.getinfo(nm)
381 self.assertEquals(info.filename, nm)
382 self.assertEquals(info.file_size, len(self.data))
383
384 # Check that testzip doesn't raise an exception
385 zipfp.testzip()
386
387
388 zipfp.close()
389
390 def testStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000391 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000392 self.zipTest(f, zipfile.ZIP_STORED)
393
394
395 if zlib:
396 def testDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000397 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000398 self.zipTest(f, zipfile.ZIP_DEFLATED)
399
400 def testAbsoluteArcnames(self):
401 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True)
402 zipfp.write(TESTFN, "/absolute")
403 zipfp.close()
404
405 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
406 self.assertEqual(zipfp.namelist(), ["absolute"])
407 zipfp.close()
408
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000409 def tearDown(self):
410 zipfile.ZIP64_LIMIT = self._limit
411 os.remove(TESTFN)
412 os.remove(TESTFN2)
413
414class PyZipFileTests(unittest.TestCase):
415 def testWritePyfile(self):
416 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
417 fn = __file__
418 if fn.endswith('.pyc') or fn.endswith('.pyo'):
419 fn = fn[:-1]
420
421 zipfp.writepy(fn)
422
423 bn = os.path.basename(fn)
424 self.assert_(bn not in zipfp.namelist())
425 self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
426 zipfp.close()
427
428
429 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
430 fn = __file__
431 if fn.endswith('.pyc') or fn.endswith('.pyo'):
432 fn = fn[:-1]
433
434 zipfp.writepy(fn, "testpackage")
435
436 bn = "%s/%s"%("testpackage", os.path.basename(fn))
437 self.assert_(bn not in zipfp.namelist())
438 self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
439 zipfp.close()
440
441 def testWritePythonPackage(self):
Neal Norwitzfe61bb92007-08-25 17:57:37 +0000442 return # XXX(nnorwitz): disable test until email is checked in again.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000443 import email
444 packagedir = os.path.dirname(email.__file__)
445
446 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
447 zipfp.writepy(packagedir)
448
449 # Check for a couple of modules at different levels of the hieararchy
450 names = zipfp.namelist()
451 self.assert_('email/__init__.pyo' in names or 'email/__init__.pyc' in names)
452 self.assert_('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
453
454 def testWritePythonDirectory(self):
455 os.mkdir(TESTFN2)
456 try:
457 fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000458 fp.write("print(42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000459 fp.close()
460
461 fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
Guido van Rossum43fc78d2007-02-09 22:18:41 +0000462 fp.write("print(42 * 42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000463 fp.close()
464
465 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
466 fp.write("bla bla bla\n")
467 fp.close()
468
469 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
470 zipfp.writepy(TESTFN2)
471
472 names = zipfp.namelist()
473 self.assert_('mod1.pyc' in names or 'mod1.pyo' in names)
474 self.assert_('mod2.pyc' in names or 'mod2.pyo' in names)
475 self.assert_('mod2.txt' not in names)
476
477 finally:
478 shutil.rmtree(TESTFN2)
479
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000480 def testWriteNonPyfile(self):
481 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
Guido van Rossum814661e2007-07-18 22:07:29 +0000482 open(TESTFN, 'w').write('most definitely not a python file')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000483 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
484 os.remove(TESTFN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000485
486
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000487class OtherTests(unittest.TestCase):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000488 def testCreateNonExistentFileForAppend(self):
489 if os.path.exists(TESTFN):
490 os.unlink(TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000491
Thomas Wouterscf297e42007-02-23 15:07:44 +0000492 filename = 'testfile.txt'
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000493 content = b'hello, world. this is some content.'
Guido van Rossumd8faa362007-04-27 19:54:29 +0000494
Thomas Wouterscf297e42007-02-23 15:07:44 +0000495 try:
496 zf = zipfile.ZipFile(TESTFN, 'a')
497 zf.writestr(filename, content)
498 zf.close()
499 except IOError:
500 self.fail('Could not append data to a non-existent zip file.')
501
502 self.assert_(os.path.exists(TESTFN))
503
504 zf = zipfile.ZipFile(TESTFN, 'r')
505 self.assertEqual(zf.read(filename), content)
506 zf.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000507
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000508 def testCloseErroneousFile(self):
509 # This test checks that the ZipFile constructor closes the file object
510 # it opens if there's an error in the file. If it doesn't, the traceback
511 # holds a reference to the ZipFile object and, indirectly, the file object.
512 # On Windows, this causes the os.unlink() call to fail because the
513 # underlying file is still open. This is SF bug #412214.
514 #
515 fp = open(TESTFN, "w")
516 fp.write("this is not a legal zip file\n")
517 fp.close()
518 try:
519 zf = zipfile.ZipFile(TESTFN)
520 except zipfile.BadZipfile:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000521 pass
522
523 def testIsZipErroneousFile(self):
524 # This test checks that the is_zipfile function correctly identifies
525 # a file that is not a zip file
526 fp = open(TESTFN, "w")
527 fp.write("this is not a legal zip file\n")
528 fp.close()
529 chk = zipfile.is_zipfile(TESTFN)
530 self.assert_(chk is False)
531
532 def testIsZipValidFile(self):
533 # This test checks that the is_zipfile function correctly identifies
534 # a file that is a zip file
535 zipf = zipfile.ZipFile(TESTFN, mode="w")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000536 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000537 zipf.close()
538 chk = zipfile.is_zipfile(TESTFN)
539 self.assert_(chk is True)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000540
541 def testNonExistentFileRaisesIOError(self):
542 # make sure we don't raise an AttributeError when a partially-constructed
543 # ZipFile instance is finalized; this tests for regression on SF tracker
544 # bug #403871.
545
546 # The bug we're testing for caused an AttributeError to be raised
547 # when a ZipFile instance was created for a file that did not
548 # exist; the .fp member was not initialized but was needed by the
549 # __del__() method. Since the AttributeError is in the __del__(),
550 # it is ignored, but the user should be sufficiently annoyed by
551 # the message on the output that regression will be noticed
552 # quickly.
553 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
554
555 def testClosedZipRaisesRuntimeError(self):
556 # Verify that testzip() doesn't swallow inappropriate exceptions.
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000557 data = io.BytesIO()
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000558 zipf = zipfile.ZipFile(data, mode="w")
559 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
560 zipf.close()
561
562 # This is correct; calling .read on a closed ZipFile should throw
563 # a RuntimeError, and so should calling .testzip. An earlier
564 # version of .testzip would swallow this exception (and any other)
565 # and report that the first file in the archive was corrupt.
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000566 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
567 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000568 self.assertRaises(RuntimeError, zipf.testzip)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000569 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Guido van Rossum814661e2007-07-18 22:07:29 +0000570 open(TESTFN, 'w').write('zipfile test data')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000571 self.assertRaises(RuntimeError, zipf.write, TESTFN)
572
573 def test_BadConstructorMode(self):
574 # Check that bad modes passed to ZipFile constructor are caught
575 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
576
577 def test_BadOpenMode(self):
578 # Check that bad modes passed to ZipFile.open are caught
579 zipf = zipfile.ZipFile(TESTFN, mode="w")
580 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
581 zipf.close()
582 zipf = zipfile.ZipFile(TESTFN, mode="r")
583 # read the data to make sure the file is there
584 zipf.read("foo.txt")
585 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
586 zipf.close()
587
588 def test_Read0(self):
589 # Check that calling read(0) on a ZipExtFile object returns an empty
590 # string and doesn't advance file pointer
591 zipf = zipfile.ZipFile(TESTFN, mode="w")
592 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
593 # read the data to make sure the file is there
594 f = zipf.open("foo.txt")
595 for i in range(FIXEDTEST_SIZE):
Guido van Rossum814661e2007-07-18 22:07:29 +0000596 self.assertEqual(f.read(0), b'')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000597
Guido van Rossum814661e2007-07-18 22:07:29 +0000598 self.assertEqual(f.read(), b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000599 zipf.close()
600
601 def test_OpenNonexistentItem(self):
602 # Check that attempting to call open() for an item that doesn't
603 # exist in the archive raises a RuntimeError
604 zipf = zipfile.ZipFile(TESTFN, mode="w")
605 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
606
607 def test_BadCompressionMode(self):
608 # Check that bad compression methods passed to ZipFile.open are caught
609 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
610
611 def test_NullByteInFilename(self):
612 # Check that a filename containing a null byte is properly terminated
613 zipf = zipfile.ZipFile(TESTFN, mode="w")
Guido van Rossum814661e2007-07-18 22:07:29 +0000614 zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000615 self.assertEqual(zipf.namelist(), ['foo.txt'])
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000616
Guido van Rossumd8faa362007-04-27 19:54:29 +0000617 def tearDown(self):
618 support.unlink(TESTFN)
619 support.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000620
621class DecryptionTests(unittest.TestCase):
622 # This test checks that ZIP decryption works. Since the library does not
623 # support encryption at the moment, we use a pre-generated encrypted
624 # ZIP file
625
626 data = (
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000627 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
628 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
629 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
630 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
631 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
632 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
633 b'\x00\x00L\x00\x00\x00\x00\x00' )
Thomas Wouterscf297e42007-02-23 15:07:44 +0000634
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000635 plain = b'zipfile.py encryption test'
Thomas Wouterscf297e42007-02-23 15:07:44 +0000636
637 def setUp(self):
638 fp = open(TESTFN, "wb")
639 fp.write(self.data)
640 fp.close()
641 self.zip = zipfile.ZipFile(TESTFN, "r")
642
643 def tearDown(self):
644 self.zip.close()
645 os.unlink(TESTFN)
646
647 def testNoPassword(self):
648 # Reading the encrypted file without password
649 # must generate a RunTime exception
650 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
651
652 def testBadPassword(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000653 self.zip.setpassword(b"perl")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000654 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000655
Thomas Wouterscf297e42007-02-23 15:07:44 +0000656 def testGoodPassword(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000657 self.zip.setpassword(b"python")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000658 self.assertEquals(self.zip.read("test.txt"), self.plain)
659
Guido van Rossumd8faa362007-04-27 19:54:29 +0000660
661class TestsWithRandomBinaryFiles(unittest.TestCase):
662 def setUp(self):
663 datacount = randint(16, 64)*1024 + randint(1, 1024)
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000664 self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
665 for i in range(datacount))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000666
667 # Make a source file with some lines
668 fp = open(TESTFN, "wb")
669 fp.write(self.data)
670 fp.close()
671
672 def tearDown(self):
673 support.unlink(TESTFN)
674 support.unlink(TESTFN2)
675
676 def makeTestArchive(self, f, compression):
677 # Create the ZIP archive
678 zipfp = zipfile.ZipFile(f, "w", compression)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000679 zipfp.write(TESTFN, "another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000680 zipfp.write(TESTFN, TESTFN)
681 zipfp.close()
682
683 def zipTest(self, f, compression):
684 self.makeTestArchive(f, compression)
685
686 # Read the ZIP archive
687 zipfp = zipfile.ZipFile(f, "r", compression)
688 testdata = zipfp.read(TESTFN)
689 self.assertEqual(len(testdata), len(self.data))
690 self.assertEqual(testdata, self.data)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000691 self.assertEqual(zipfp.read("another.name"), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000692 zipfp.close()
693
694 def testStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000695 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000696 self.zipTest(f, zipfile.ZIP_STORED)
697
698 def zipOpenTest(self, f, compression):
699 self.makeTestArchive(f, compression)
700
701 # Read the ZIP archive
702 zipfp = zipfile.ZipFile(f, "r", compression)
703 zipdata1 = []
704 zipopen1 = zipfp.open(TESTFN)
705 while 1:
706 read_data = zipopen1.read(256)
707 if not read_data:
708 break
709 zipdata1.append(read_data)
710
711 zipdata2 = []
Skip Montanaro7a98be22007-08-16 14:35:24 +0000712 zipopen2 = zipfp.open("another.name")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000713 while 1:
714 read_data = zipopen2.read(256)
715 if not read_data:
716 break
717 zipdata2.append(read_data)
718
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000719 testdata1 = b''.join(zipdata1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000720 self.assertEqual(len(testdata1), len(self.data))
721 self.assertEqual(testdata1, self.data)
722
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000723 testdata2 = b''.join(zipdata2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000724 self.assertEqual(len(testdata1), len(self.data))
725 self.assertEqual(testdata1, self.data)
726 zipfp.close()
727
728 def testOpenStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000729 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000730 self.zipOpenTest(f, zipfile.ZIP_STORED)
731
732 def zipRandomOpenTest(self, f, compression):
733 self.makeTestArchive(f, compression)
734
735 # Read the ZIP archive
736 zipfp = zipfile.ZipFile(f, "r", compression)
737 zipdata1 = []
738 zipopen1 = zipfp.open(TESTFN)
739 while 1:
740 read_data = zipopen1.read(randint(1, 1024))
741 if not read_data:
742 break
743 zipdata1.append(read_data)
744
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000745 testdata = b''.join(zipdata1)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000746 self.assertEqual(len(testdata), len(self.data))
747 self.assertEqual(testdata, self.data)
748 zipfp.close()
749
750 def testRandomOpenStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000751 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000752 self.zipRandomOpenTest(f, zipfile.ZIP_STORED)
753
754class TestsWithMultipleOpens(unittest.TestCase):
755 def setUp(self):
756 # Create the ZIP archive
757 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
758 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
759 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
760 zipfp.close()
761
762 def testSameFile(self):
763 # Verify that (when the ZipFile is in control of creating file objects)
764 # multiple open() calls can be made without interfering with each other.
765 zipf = zipfile.ZipFile(TESTFN2, mode="r")
766 zopen1 = zipf.open('ones')
767 zopen2 = zipf.open('ones')
768 data1 = zopen1.read(500)
769 data2 = zopen2.read(500)
770 data1 += zopen1.read(500)
771 data2 += zopen2.read(500)
772 self.assertEqual(data1, data2)
773 zipf.close()
774
775 def testDifferentFile(self):
776 # Verify that (when the ZipFile is in control of creating file objects)
777 # multiple open() calls can be made without interfering with each other.
778 zipf = zipfile.ZipFile(TESTFN2, mode="r")
779 zopen1 = zipf.open('ones')
780 zopen2 = zipf.open('twos')
781 data1 = zopen1.read(500)
782 data2 = zopen2.read(500)
783 data1 += zopen1.read(500)
784 data2 += zopen2.read(500)
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000785 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
786 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000787 zipf.close()
788
789 def testInterleaved(self):
790 # Verify that (when the ZipFile is in control of creating file objects)
791 # multiple open() calls can be made without interfering with each other.
792 zipf = zipfile.ZipFile(TESTFN2, mode="r")
793 zopen1 = zipf.open('ones')
794 data1 = zopen1.read(500)
795 zopen2 = zipf.open('twos')
796 data2 = zopen2.read(500)
797 data1 += zopen1.read(500)
798 data2 += zopen2.read(500)
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000799 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
800 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000801 zipf.close()
802
803 def tearDown(self):
804 os.remove(TESTFN2)
805
806
807class UniversalNewlineTests(unittest.TestCase):
808 def setUp(self):
Guido van Rossum9c627722007-08-27 18:31:48 +0000809 self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000810 for i in range(FIXEDTEST_SIZE)]
Guido van Rossumd8faa362007-04-27 19:54:29 +0000811 self.seps = ('\r', '\r\n', '\n')
812 self.arcdata, self.arcfiles = {}, {}
813 for n, s in enumerate(self.seps):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000814 b = s.encode("ascii")
815 self.arcdata[s] = b.join(self.line_gen) + b
Guido van Rossumd8faa362007-04-27 19:54:29 +0000816 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000817 f = open(self.arcfiles[s], "wb")
818 try:
819 f.write(self.arcdata[s])
820 finally:
821 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000822
823 def makeTestArchive(self, f, compression):
824 # Create the ZIP archive
825 zipfp = zipfile.ZipFile(f, "w", compression)
826 for fn in self.arcfiles.values():
827 zipfp.write(fn, fn)
828 zipfp.close()
829
830 def readTest(self, f, compression):
831 self.makeTestArchive(f, compression)
832
833 # Read the ZIP archive
834 zipfp = zipfile.ZipFile(f, "r")
835 for sep, fn in self.arcfiles.items():
836 zipdata = zipfp.open(fn, "rU").read()
837 self.assertEqual(self.arcdata[sep], zipdata)
838
839 zipfp.close()
840
841 def readlineTest(self, f, compression):
842 self.makeTestArchive(f, compression)
843
844 # Read the ZIP archive
845 zipfp = zipfile.ZipFile(f, "r")
846 for sep, fn in self.arcfiles.items():
847 zipopen = zipfp.open(fn, "rU")
848 for line in self.line_gen:
849 linedata = zipopen.readline()
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000850 self.assertEqual(linedata, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000851
852 zipfp.close()
853
854 def readlinesTest(self, f, compression):
855 self.makeTestArchive(f, compression)
856
857 # Read the ZIP archive
858 zipfp = zipfile.ZipFile(f, "r")
859 for sep, fn in self.arcfiles.items():
860 ziplines = zipfp.open(fn, "rU").readlines()
861 for line, zipline in zip(self.line_gen, ziplines):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000862 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000863
864 zipfp.close()
865
866 def iterlinesTest(self, f, compression):
867 self.makeTestArchive(f, compression)
868
869 # Read the ZIP archive
870 zipfp = zipfile.ZipFile(f, "r")
871 for sep, fn in self.arcfiles.items():
872 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000873 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000874
875 zipfp.close()
876
877 def testReadStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000878 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000879 self.readTest(f, zipfile.ZIP_STORED)
880
881 def testReadlineStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000882 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000883 self.readlineTest(f, zipfile.ZIP_STORED)
884
885 def testReadlinesStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000886 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000887 self.readlinesTest(f, zipfile.ZIP_STORED)
888
889 def testIterlinesStored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000890 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000891 self.iterlinesTest(f, zipfile.ZIP_STORED)
892
893 if zlib:
894 def testReadDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000895 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000896 self.readTest(f, zipfile.ZIP_DEFLATED)
897
898 def testReadlineDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000899 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000900 self.readlineTest(f, zipfile.ZIP_DEFLATED)
901
902 def testReadlinesDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000903 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000904 self.readlinesTest(f, zipfile.ZIP_DEFLATED)
905
906 def testIterlinesDeflated(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000907 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000908 self.iterlinesTest(f, zipfile.ZIP_DEFLATED)
909
910 def tearDown(self):
911 for sep, fn in self.arcfiles.items():
912 os.remove(fn)
913 support.unlink(TESTFN)
914 support.unlink(TESTFN2)
915
916
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000917def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +0000918 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
919 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
920 UniversalNewlineTests, TestsWithRandomBinaryFiles)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000921
922if __name__ == "__main__":
923 test_main()