blob: 652274f8fe37a92867568b3908383b9ee266b628 [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
Tim Petersa45cacf2004-08-20 03:47:14 +00006
Ezio Melottie7a0cc22009-07-04 14:58:27 +00007import os
8import sys
9import shutil
10import struct
11import zipfile
12import unittest
Tim Petersa19a1682001-03-29 04:36:09 +000013
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000014from StringIO import StringIO
15from tempfile import TemporaryFile
Martin v. Löwis3eb76482007-03-06 10:41:24 +000016from random import randint, random
Ezio Melottie7a0cc22009-07-04 14:58:27 +000017from unittest import skipUnless
Tim Petersa19a1682001-03-29 04:36:09 +000018
Ezio Melotti6cbfc122009-07-10 20:25:56 +000019from test.test_support import TESTFN, run_unittest, findfile, unlink
Guido van Rossum368f04a2000-04-10 13:23:04 +000020
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000021TESTFN2 = TESTFN + "2"
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +000022TESTFNDIR = TESTFN + "d"
Georg Brandl4b3ab6f2007-07-12 09:59:22 +000023FIXEDTEST_SIZE = 1000
Guido van Rossum368f04a2000-04-10 13:23:04 +000024
Georg Brandl62416bc2008-01-07 18:47:44 +000025SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
26 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
27 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
28 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
29
Ezio Melotti6cbfc122009-07-10 20:25:56 +000030
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000031class TestsWithSourceFile(unittest.TestCase):
32 def setUp(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +000033 self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random())
34 for i in xrange(FIXEDTEST_SIZE)]
Martin v. Löwis3eb76482007-03-06 10:41:24 +000035 self.data = '\n'.join(self.line_gen) + '\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000036
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000037 # Make a source file with some lines
38 fp = open(TESTFN, "wb")
39 fp.write(self.data)
40 fp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000041
Ezio Melottid5a23e32009-07-15 17:07:04 +000042 def make_test_archive(self, f, compression):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000043 # Create the ZIP archive
44 zipfp = zipfile.ZipFile(f, "w", compression)
45 zipfp.write(TESTFN, "another"+os.extsep+"name")
46 zipfp.write(TESTFN, TESTFN)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000047 zipfp.writestr("strfile", self.data)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000048 zipfp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +000049
Ezio Melottid5a23e32009-07-15 17:07:04 +000050 def zip_test(self, f, compression):
51 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +000052
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000053 # Read the ZIP archive
54 zipfp = zipfile.ZipFile(f, "r", compression)
55 self.assertEqual(zipfp.read(TESTFN), self.data)
56 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000057 self.assertEqual(zipfp.read("strfile"), self.data)
58
59 # Print the ZIP directory
60 fp = StringIO()
61 stdout = sys.stdout
62 try:
63 sys.stdout = fp
Ronald Oussoren143cefb2006-06-15 08:14:18 +000064 zipfp.printdir()
65 finally:
66 sys.stdout = stdout
Tim Petersa608bb22006-06-15 18:06:29 +000067
Ronald Oussoren143cefb2006-06-15 08:14:18 +000068 directory = fp.getvalue()
69 lines = directory.splitlines()
70 self.assertEquals(len(lines), 4) # Number of files + header
71
Benjamin Peterson5c8da862009-06-30 22:57:08 +000072 self.assertTrue('File Name' in lines[0])
73 self.assertTrue('Modified' in lines[0])
74 self.assertTrue('Size' in lines[0])
Ronald Oussoren143cefb2006-06-15 08:14:18 +000075
76 fn, date, time, size = lines[1].split()
77 self.assertEquals(fn, 'another.name')
78 # XXX: timestamp is not tested
79 self.assertEquals(size, str(len(self.data)))
80
81 # Check the namelist
82 names = zipfp.namelist()
83 self.assertEquals(len(names), 3)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000084 self.assertTrue(TESTFN in names)
85 self.assertTrue("another"+os.extsep+"name" in names)
86 self.assertTrue("strfile" in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000087
88 # Check infolist
89 infos = zipfp.infolist()
90 names = [ i.filename for i in infos ]
91 self.assertEquals(len(names), 3)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000092 self.assertTrue(TESTFN in names)
93 self.assertTrue("another"+os.extsep+"name" in names)
94 self.assertTrue("strfile" in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000095 for i in infos:
96 self.assertEquals(i.file_size, len(self.data))
97
98 # check getinfo
99 for nm in (TESTFN, "another"+os.extsep+"name", "strfile"):
100 info = zipfp.getinfo(nm)
101 self.assertEquals(info.filename, nm)
102 self.assertEquals(info.file_size, len(self.data))
103
104 # Check that testzip doesn't raise an exception
105 zipfp.testzip()
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000106 zipfp.close()
Tim Peters7d3bad62001-04-04 18:56:49 +0000107
Ezio Melottid5a23e32009-07-15 17:07:04 +0000108 def test_stored(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000109 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000110 self.zip_test(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000111
Ezio Melottid5a23e32009-07-15 17:07:04 +0000112 def zip_open_test(self, f, compression):
113 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000114
115 # Read the ZIP archive
116 zipfp = zipfile.ZipFile(f, "r", compression)
117 zipdata1 = []
118 zipopen1 = zipfp.open(TESTFN)
119 while 1:
120 read_data = zipopen1.read(256)
121 if not read_data:
122 break
123 zipdata1.append(read_data)
124
125 zipdata2 = []
126 zipopen2 = zipfp.open("another"+os.extsep+"name")
127 while 1:
128 read_data = zipopen2.read(256)
129 if not read_data:
130 break
131 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +0000132
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000133 self.assertEqual(''.join(zipdata1), self.data)
134 self.assertEqual(''.join(zipdata2), self.data)
135 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000136
Ezio Melottid5a23e32009-07-15 17:07:04 +0000137 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000138 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000139 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000140
Ezio Melottid5a23e32009-07-15 17:07:04 +0000141 def test_open_via_zip_info(self):
Georg Brandl112aa502008-05-20 08:25:48 +0000142 # Create the ZIP archive
143 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
144 zipfp.writestr("name", "foo")
145 zipfp.writestr("name", "bar")
146 zipfp.close()
147
148 zipfp = zipfile.ZipFile(TESTFN2, "r")
149 infos = zipfp.infolist()
150 data = ""
151 for info in infos:
152 data += zipfp.open(info).read()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000153 self.assertTrue(data == "foobar" or data == "barfoo")
Georg Brandl112aa502008-05-20 08:25:48 +0000154 data = ""
155 for info in infos:
156 data += zipfp.read(info)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000157 self.assertTrue(data == "foobar" or data == "barfoo")
Georg Brandl112aa502008-05-20 08:25:48 +0000158 zipfp.close()
159
Ezio Melottid5a23e32009-07-15 17:07:04 +0000160 def zip_random_open_test(self, f, compression):
161 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000162
163 # Read the ZIP archive
164 zipfp = zipfile.ZipFile(f, "r", compression)
165 zipdata1 = []
166 zipopen1 = zipfp.open(TESTFN)
167 while 1:
168 read_data = zipopen1.read(randint(1, 1024))
169 if not read_data:
170 break
171 zipdata1.append(read_data)
172
173 self.assertEqual(''.join(zipdata1), self.data)
174 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000175
Ezio Melottid5a23e32009-07-15 17:07:04 +0000176 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000177 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000178 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +0000179
Ezio Melottid5a23e32009-07-15 17:07:04 +0000180 def zip_readline_test(self, f, compression):
181 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000182
183 # Read the ZIP archive
184 zipfp = zipfile.ZipFile(f, "r")
185 zipopen = zipfp.open(TESTFN)
186 for line in self.line_gen:
187 linedata = zipopen.readline()
188 self.assertEqual(linedata, line + '\n')
189
190 zipfp.close()
191
Ezio Melottid5a23e32009-07-15 17:07:04 +0000192 def zip_readlines_test(self, f, compression):
193 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000194
195 # Read the ZIP archive
196 zipfp = zipfile.ZipFile(f, "r")
197 ziplines = zipfp.open(TESTFN).readlines()
198 for line, zipline in zip(self.line_gen, ziplines):
199 self.assertEqual(zipline, line + '\n')
200
201 zipfp.close()
202
Ezio Melottid5a23e32009-07-15 17:07:04 +0000203 def zip_iterlines_test(self, f, compression):
204 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000205
206 # Read the ZIP archive
207 zipfp = zipfile.ZipFile(f, "r")
208 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
209 self.assertEqual(zipline, line + '\n')
210
211 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000212
Ezio Melottid5a23e32009-07-15 17:07:04 +0000213 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000214 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000215 self.zip_readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000216
Ezio Melottid5a23e32009-07-15 17:07:04 +0000217 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000218 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000219 self.zip_readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000220
Ezio Melottid5a23e32009-07-15 17:07:04 +0000221 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000222 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000223 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000224
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000225 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000226 def test_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000227 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000228 self.zip_test(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000229
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000230 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000231 def test_open_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000232 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000233 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000234
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000235 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000236 def test_random_open_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000237 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000238 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000239
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000240 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000241 def test_readline_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000242 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000243 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000244
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000245 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000246 def test_readlines_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000247 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000248 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000249
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000250 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000251 def test_iterlines_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000252 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000253 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
Tim Petersea5962f2007-03-12 18:07:52 +0000254
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000255 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000256 def test_low_compression(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000257 # Checks for cases where compressed data is larger than original
258 # Create the ZIP archive
259 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
260 zipfp.writestr("strfile", '12')
261 zipfp.close()
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000262
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000263 # Get an open object for strfile
264 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED)
265 openobj = zipfp.open("strfile")
266 self.assertEqual(openobj.read(1), '1')
267 self.assertEqual(openobj.read(1), '2')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000268
Ezio Melottid5a23e32009-07-15 17:07:04 +0000269 def test_absolute_arcnames(self):
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000270 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
271 zipfp.write(TESTFN, "/absolute")
272 zipfp.close()
273
274 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
275 self.assertEqual(zipfp.namelist(), ["absolute"])
276 zipfp.close()
Tim Peters32cbc962006-02-20 21:42:18 +0000277
Ezio Melottid5a23e32009-07-15 17:07:04 +0000278 def test_append_to_zip_file(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000279 # Test appending to an existing zipfile
280 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
281 zipfp.write(TESTFN, TESTFN)
282 zipfp.close()
283 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
284 zipfp.writestr("strfile", self.data)
285 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
286 zipfp.close()
287
Ezio Melottid5a23e32009-07-15 17:07:04 +0000288 def test_append_to_non_zip_file(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000289 # Test appending to an existing file that is not a zipfile
290 # NOTE: this test fails if len(d) < 22 because of the first
291 # line "fpin.seek(-22, 2)" in _EndRecData
292 d = 'I am not a ZipFile!'*10
293 f = file(TESTFN2, 'wb')
294 f.write(d)
295 f.close()
296 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
297 zipfp.write(TESTFN, TESTFN)
298 zipfp.close()
299
300 f = file(TESTFN2, 'rb')
301 f.seek(len(d))
302 zipfp = zipfile.ZipFile(f, "r")
303 self.assertEqual(zipfp.namelist(), [TESTFN])
304 zipfp.close()
305 f.close()
306
Ezio Melottid5a23e32009-07-15 17:07:04 +0000307 def test_write_default_name(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000308 # Check that calling ZipFile.write without arcname specified produces the expected result
309 zipfp = zipfile.ZipFile(TESTFN2, "w")
310 zipfp.write(TESTFN)
311 self.assertEqual(zipfp.read(TESTFN), file(TESTFN).read())
312 zipfp.close()
313
Ezio Melottid5a23e32009-07-15 17:07:04 +0000314 def test_per_file_compression(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000315 # Check that files within a Zip archive can have different compression options
316 zipfp = zipfile.ZipFile(TESTFN2, "w")
317 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
318 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
319 sinfo = zipfp.getinfo('storeme')
320 dinfo = zipfp.getinfo('deflateme')
321 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
322 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
323 zipfp.close()
324
Ezio Melottid5a23e32009-07-15 17:07:04 +0000325 def test_write_to_readonly(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000326 # Check that trying to call write() on a readonly ZipFile object
327 # raises a RuntimeError
328 zipf = zipfile.ZipFile(TESTFN2, mode="w")
329 zipf.writestr("somefile.txt", "bogus")
330 zipf.close()
331 zipf = zipfile.ZipFile(TESTFN2, mode="r")
332 self.assertRaises(RuntimeError, zipf.write, TESTFN)
333 zipf.close()
334
Ezio Melottid5a23e32009-07-15 17:07:04 +0000335 def test_extract(self):
Georg Brandl62416bc2008-01-07 18:47:44 +0000336 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
337 for fpath, fdata in SMALL_TEST_DATA:
338 zipfp.writestr(fpath, fdata)
339 zipfp.close()
340
341 zipfp = zipfile.ZipFile(TESTFN2, "r")
342 for fpath, fdata in SMALL_TEST_DATA:
343 writtenfile = zipfp.extract(fpath)
344
345 # make sure it was written to the right place
346 if os.path.isabs(fpath):
347 correctfile = os.path.join(os.getcwd(), fpath[1:])
348 else:
349 correctfile = os.path.join(os.getcwd(), fpath)
Christian Heimesa2af2122008-01-26 16:43:35 +0000350 correctfile = os.path.normpath(correctfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000351
352 self.assertEqual(writtenfile, correctfile)
353
354 # make sure correct data is in correct file
355 self.assertEqual(fdata, file(writtenfile, "rb").read())
356
357 os.remove(writtenfile)
358
359 zipfp.close()
360
361 # remove the test file subdirectories
362 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
363
Ezio Melottid5a23e32009-07-15 17:07:04 +0000364 def test_extract_all(self):
Georg Brandl62416bc2008-01-07 18:47:44 +0000365 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
366 for fpath, fdata in SMALL_TEST_DATA:
367 zipfp.writestr(fpath, fdata)
368 zipfp.close()
369
370 zipfp = zipfile.ZipFile(TESTFN2, "r")
371 zipfp.extractall()
372 for fpath, fdata in SMALL_TEST_DATA:
373 if os.path.isabs(fpath):
374 outfile = os.path.join(os.getcwd(), fpath[1:])
375 else:
376 outfile = os.path.join(os.getcwd(), fpath)
377
378 self.assertEqual(fdata, file(outfile, "rb").read())
379
380 os.remove(outfile)
381
382 zipfp.close()
383
384 # remove the test file subdirectories
385 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
386
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000387 def zip_test_writestr_permissions(self, f, compression):
388 # Make sure that writestr creates files with mode 0600,
389 # when it is passed a name rather than a ZipInfo instance.
390
Ezio Melottid5a23e32009-07-15 17:07:04 +0000391 self.make_test_archive(f, compression)
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000392 zipfp = zipfile.ZipFile(f, "r")
393 zinfo = zipfp.getinfo('strfile')
394 self.assertEqual(zinfo.external_attr, 0600 << 16)
395
Ezio Melottid5a23e32009-07-15 17:07:04 +0000396 def test_writestr_permissions(self):
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000397 for f in (TESTFN2, TemporaryFile(), StringIO()):
398 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
399
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000400 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000401 unlink(TESTFN)
402 unlink(TESTFN2)
403
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000404
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000405class TestZip64InSmallFiles(unittest.TestCase):
406 # These tests test the ZIP64 functionality without using large files,
407 # see test_zipfile64 for proper tests.
408
409 def setUp(self):
410 self._limit = zipfile.ZIP64_LIMIT
411 zipfile.ZIP64_LIMIT = 5
412
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000413 line_gen = ("Test of zipfile line %d." % i for i in range(0, FIXEDTEST_SIZE))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000414 self.data = '\n'.join(line_gen)
415
416 # Make a source file with some lines
417 fp = open(TESTFN, "wb")
418 fp.write(self.data)
419 fp.close()
420
Ezio Melottid5a23e32009-07-15 17:07:04 +0000421 def large_file_exception_test(self, f, compression):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000422 zipfp = zipfile.ZipFile(f, "w", compression)
Tim Petersa608bb22006-06-15 18:06:29 +0000423 self.assertRaises(zipfile.LargeZipFile,
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000424 zipfp.write, TESTFN, "another"+os.extsep+"name")
425 zipfp.close()
426
Ezio Melottid5a23e32009-07-15 17:07:04 +0000427 def large_file_exception_test2(self, f, compression):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000428 zipfp = zipfile.ZipFile(f, "w", compression)
Tim Petersa608bb22006-06-15 18:06:29 +0000429 self.assertRaises(zipfile.LargeZipFile,
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000430 zipfp.writestr, "another"+os.extsep+"name", self.data)
431 zipfp.close()
432
Ezio Melottid5a23e32009-07-15 17:07:04 +0000433 def test_large_file_exception(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000434 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000435 self.large_file_exception_test(f, zipfile.ZIP_STORED)
436 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000437
Ezio Melottid5a23e32009-07-15 17:07:04 +0000438 def zip_test(self, f, compression):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000439 # Create the ZIP archive
440 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
441 zipfp.write(TESTFN, "another"+os.extsep+"name")
442 zipfp.write(TESTFN, TESTFN)
443 zipfp.writestr("strfile", self.data)
444 zipfp.close()
445
446 # Read the ZIP archive
447 zipfp = zipfile.ZipFile(f, "r", compression)
448 self.assertEqual(zipfp.read(TESTFN), self.data)
449 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
450 self.assertEqual(zipfp.read("strfile"), self.data)
451
452 # Print the ZIP directory
453 fp = StringIO()
454 stdout = sys.stdout
455 try:
456 sys.stdout = fp
457
458 zipfp.printdir()
459 finally:
460 sys.stdout = stdout
Tim Petersa608bb22006-06-15 18:06:29 +0000461
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000462 directory = fp.getvalue()
463 lines = directory.splitlines()
464 self.assertEquals(len(lines), 4) # Number of files + header
465
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000466 self.assertTrue('File Name' in lines[0])
467 self.assertTrue('Modified' in lines[0])
468 self.assertTrue('Size' in lines[0])
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000469
470 fn, date, time, size = lines[1].split()
471 self.assertEquals(fn, 'another.name')
472 # XXX: timestamp is not tested
473 self.assertEquals(size, str(len(self.data)))
474
475 # Check the namelist
476 names = zipfp.namelist()
477 self.assertEquals(len(names), 3)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000478 self.assertTrue(TESTFN in names)
479 self.assertTrue("another"+os.extsep+"name" in names)
480 self.assertTrue("strfile" in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000481
482 # Check infolist
483 infos = zipfp.infolist()
484 names = [ i.filename for i in infos ]
485 self.assertEquals(len(names), 3)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000486 self.assertTrue(TESTFN in names)
487 self.assertTrue("another"+os.extsep+"name" in names)
488 self.assertTrue("strfile" in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000489 for i in infos:
490 self.assertEquals(i.file_size, len(self.data))
491
492 # check getinfo
493 for nm in (TESTFN, "another"+os.extsep+"name", "strfile"):
494 info = zipfp.getinfo(nm)
495 self.assertEquals(info.filename, nm)
496 self.assertEquals(info.file_size, len(self.data))
497
498 # Check that testzip doesn't raise an exception
499 zipfp.testzip()
500
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000501 zipfp.close()
502
Ezio Melottid5a23e32009-07-15 17:07:04 +0000503 def test_stored(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000504 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000505 self.zip_test(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000506
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000507 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000508 def test_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000509 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000510 self.zip_test(f, zipfile.ZIP_DEFLATED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000511
Ezio Melottid5a23e32009-07-15 17:07:04 +0000512 def test_absolute_arcnames(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000513 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True)
514 zipfp.write(TESTFN, "/absolute")
515 zipfp.close()
516
517 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
518 self.assertEqual(zipfp.namelist(), ["absolute"])
519 zipfp.close()
520
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000521 def tearDown(self):
522 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000523 unlink(TESTFN)
524 unlink(TESTFN2)
525
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000526
527class PyZipFileTests(unittest.TestCase):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000528 def test_write_pyfile(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000529 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
530 fn = __file__
531 if fn.endswith('.pyc') or fn.endswith('.pyo'):
532 fn = fn[:-1]
533
534 zipfp.writepy(fn)
535
536 bn = os.path.basename(fn)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000537 self.assertTrue(bn not in zipfp.namelist())
538 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000539 zipfp.close()
540
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000541 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
542 fn = __file__
543 if fn.endswith('.pyc') or fn.endswith('.pyo'):
544 fn = fn[:-1]
545
546 zipfp.writepy(fn, "testpackage")
547
548 bn = "%s/%s"%("testpackage", os.path.basename(fn))
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000549 self.assertTrue(bn not in zipfp.namelist())
550 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000551 zipfp.close()
552
Ezio Melottid5a23e32009-07-15 17:07:04 +0000553 def test_write_python_package(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000554 import email
555 packagedir = os.path.dirname(email.__file__)
556
557 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
558 zipfp.writepy(packagedir)
559
560 # Check for a couple of modules at different levels of the hieararchy
561 names = zipfp.namelist()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000562 self.assertTrue('email/__init__.pyo' in names or 'email/__init__.pyc' in names)
563 self.assertTrue('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000564
Ezio Melottid5a23e32009-07-15 17:07:04 +0000565 def test_write_python_directory(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000566 os.mkdir(TESTFN2)
567 try:
568 fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
569 fp.write("print 42\n")
570 fp.close()
571
572 fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
573 fp.write("print 42 * 42\n")
574 fp.close()
575
576 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
577 fp.write("bla bla bla\n")
578 fp.close()
579
580 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
581 zipfp.writepy(TESTFN2)
582
583 names = zipfp.namelist()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000584 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
585 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
586 self.assertTrue('mod2.txt' not in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000587
588 finally:
589 shutil.rmtree(TESTFN2)
590
Ezio Melottid5a23e32009-07-15 17:07:04 +0000591 def test_write_non_pyfile(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000592 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
593 file(TESTFN, 'w').write('most definitely not a python file')
594 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
595 os.remove(TESTFN)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000596
597
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000598class OtherTests(unittest.TestCase):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000599 def test_unicode_filenames(self):
Martin v. Löwis471617d2008-05-05 17:16:58 +0000600 zf = zipfile.ZipFile(TESTFN, "w")
601 zf.writestr(u"foo.txt", "Test for unicode filename")
Martin v. Löwisc3ad68c2008-05-05 17:47:06 +0000602 zf.writestr(u"\xf6.txt", "Test for unicode filename")
603 self.assertTrue(isinstance(zf.infolist()[0].filename, unicode))
Martin v. Löwis471617d2008-05-05 17:16:58 +0000604 zf.close()
Martin v. Löwisc3ad68c2008-05-05 17:47:06 +0000605 zf = zipfile.ZipFile(TESTFN, "r")
606 self.assertEqual(zf.filelist[0].filename, "foo.txt")
607 self.assertEqual(zf.filelist[1].filename, u"\xf6.txt")
608 zf.close()
Martin v. Löwis471617d2008-05-05 17:16:58 +0000609
Ezio Melottid5a23e32009-07-15 17:07:04 +0000610 def test_create_non_existent_file_for_append(self):
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000611 if os.path.exists(TESTFN):
612 os.unlink(TESTFN)
Tim Petersea5962f2007-03-12 18:07:52 +0000613
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000614 filename = 'testfile.txt'
615 content = 'hello, world. this is some content.'
Tim Petersea5962f2007-03-12 18:07:52 +0000616
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000617 try:
618 zf = zipfile.ZipFile(TESTFN, 'a')
619 zf.writestr(filename, content)
620 zf.close()
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000621 except IOError:
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000622 self.fail('Could not append data to a non-existent zip file.')
623
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000624 self.assertTrue(os.path.exists(TESTFN))
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000625
626 zf = zipfile.ZipFile(TESTFN, 'r')
627 self.assertEqual(zf.read(filename), content)
628 zf.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000629
Ezio Melottid5a23e32009-07-15 17:07:04 +0000630 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000631 # This test checks that the ZipFile constructor closes the file object
632 # it opens if there's an error in the file. If it doesn't, the traceback
633 # holds a reference to the ZipFile object and, indirectly, the file object.
634 # On Windows, this causes the os.unlink() call to fail because the
635 # underlying file is still open. This is SF bug #412214.
636 #
637 fp = open(TESTFN, "w")
638 fp.write("this is not a legal zip file\n")
639 fp.close()
640 try:
641 zf = zipfile.ZipFile(TESTFN)
642 except zipfile.BadZipfile:
Collin Winter04a51ec2007-03-29 02:28:16 +0000643 pass
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000644
Ezio Melottid5a23e32009-07-15 17:07:04 +0000645 def test_is_zip_erroneous_file(self):
Tim Petersea5962f2007-03-12 18:07:52 +0000646 # This test checks that the is_zipfile function correctly identifies
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000647 # a file that is not a zip file
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000648
649 # - passing a filename
650 with open(TESTFN, "w") as fp:
651 fp.write("this is not a legal zip file\n")
Tim Petersea5962f2007-03-12 18:07:52 +0000652 chk = zipfile.is_zipfile(TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000653 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000654 # - passing a file object
655 with open(TESTFN, "rb") as fp:
656 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000657 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000658 # - passing a file-like object
659 fp = StringIO()
660 fp.write("this is not a legal zip file\n")
661 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000662 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000663 fp.seek(0,0)
664 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000665 self.assertTrue(not chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000666
Ezio Melottid5a23e32009-07-15 17:07:04 +0000667 def test_is_zip_valid_file(self):
Tim Petersea5962f2007-03-12 18:07:52 +0000668 # This test checks that the is_zipfile function correctly identifies
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000669 # a file that is a zip file
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000670
671 # - passing a filename
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000672 zipf = zipfile.ZipFile(TESTFN, mode="w")
673 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
674 zipf.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000675 chk = zipfile.is_zipfile(TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000676 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000677 # - passing a file object
678 with open(TESTFN, "rb") as fp:
679 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000680 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000681 fp.seek(0,0)
682 zip_contents = fp.read()
683 # - passing a file-like object
684 fp = StringIO()
685 fp.write(zip_contents)
686 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000687 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000688 fp.seek(0,0)
689 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000690 self.assertTrue(chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000691
Ezio Melottid5a23e32009-07-15 17:07:04 +0000692 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000693 # make sure we don't raise an AttributeError when a partially-constructed
694 # ZipFile instance is finalized; this tests for regression on SF tracker
695 # bug #403871.
696
697 # The bug we're testing for caused an AttributeError to be raised
698 # when a ZipFile instance was created for a file that did not
699 # exist; the .fp member was not initialized but was needed by the
700 # __del__() method. Since the AttributeError is in the __del__(),
701 # it is ignored, but the user should be sufficiently annoyed by
702 # the message on the output that regression will be noticed
703 # quickly.
704 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
705
Amaury Forgeot d'Arc3e5b0272009-07-28 22:15:30 +0000706 def test_empty_file_raises_BadZipFile(self):
707 f = open(TESTFN, 'w')
708 f.close()
709 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
710
711 f = open(TESTFN, 'w')
712 f.write("short file")
713 f.close()
714 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
715
Ezio Melottid5a23e32009-07-15 17:07:04 +0000716 def test_closed_zip_raises_RuntimeError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000717 # Verify that testzip() doesn't swallow inappropriate exceptions.
718 data = StringIO()
719 zipf = zipfile.ZipFile(data, mode="w")
720 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
721 zipf.close()
722
723 # This is correct; calling .read on a closed ZipFile should throw
724 # a RuntimeError, and so should calling .testzip. An earlier
725 # version of .testzip would swallow this exception (and any other)
726 # and report that the first file in the archive was corrupt.
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000727 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
728 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000729 self.assertRaises(RuntimeError, zipf.testzip)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000730 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
731 file(TESTFN, 'w').write('zipfile test data')
732 self.assertRaises(RuntimeError, zipf.write, TESTFN)
733
Ezio Melottid5a23e32009-07-15 17:07:04 +0000734 def test_bad_constructor_mode(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000735 # Check that bad modes passed to ZipFile constructor are caught
736 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
737
Ezio Melottid5a23e32009-07-15 17:07:04 +0000738 def test_bad_open_mode(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000739 # Check that bad modes passed to ZipFile.open are caught
740 zipf = zipfile.ZipFile(TESTFN, mode="w")
741 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
742 zipf.close()
743 zipf = zipfile.ZipFile(TESTFN, mode="r")
744 # read the data to make sure the file is there
745 zipf.read("foo.txt")
746 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
747 zipf.close()
748
Ezio Melottid5a23e32009-07-15 17:07:04 +0000749 def test_read0(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000750 # Check that calling read(0) on a ZipExtFile object returns an empty
751 # string and doesn't advance file pointer
752 zipf = zipfile.ZipFile(TESTFN, mode="w")
753 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
754 # read the data to make sure the file is there
755 f = zipf.open("foo.txt")
756 for i in xrange(FIXEDTEST_SIZE):
757 self.assertEqual(f.read(0), '')
758
759 self.assertEqual(f.read(), "O, for a Muse of Fire!")
760 zipf.close()
761
Ezio Melottid5a23e32009-07-15 17:07:04 +0000762 def test_open_non_existent_item(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000763 # Check that attempting to call open() for an item that doesn't
764 # exist in the archive raises a RuntimeError
765 zipf = zipfile.ZipFile(TESTFN, mode="w")
766 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
767
Ezio Melottid5a23e32009-07-15 17:07:04 +0000768 def test_bad_compression_mode(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000769 # Check that bad compression methods passed to ZipFile.open are caught
770 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
771
Ezio Melottid5a23e32009-07-15 17:07:04 +0000772 def test_null_byte_in_filename(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000773 # Check that a filename containing a null byte is properly terminated
774 zipf = zipfile.ZipFile(TESTFN, mode="w")
775 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
776 self.assertEqual(zipf.namelist(), ['foo.txt'])
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000777
Ezio Melottid5a23e32009-07-15 17:07:04 +0000778 def test_struct_sizes(self):
Martin v. Löwis8c436412008-07-03 12:51:14 +0000779 # check that ZIP internal structure sizes are calculated correctly
780 self.assertEqual(zipfile.sizeEndCentDir, 22)
781 self.assertEqual(zipfile.sizeCentralDir, 46)
782 self.assertEqual(zipfile.sizeEndCentDir64, 56)
783 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
784
Ezio Melottid5a23e32009-07-15 17:07:04 +0000785 def test_comments(self):
Martin v. Löwis8c436412008-07-03 12:51:14 +0000786 # This test checks that comments on the archive are handled properly
787
788 # check default comment is empty
789 zipf = zipfile.ZipFile(TESTFN, mode="w")
790 self.assertEqual(zipf.comment, '')
791 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
792 zipf.close()
793 zipfr = zipfile.ZipFile(TESTFN, mode="r")
794 self.assertEqual(zipfr.comment, '')
795 zipfr.close()
796
797 # check a simple short comment
798 comment = 'Bravely taking to his feet, he beat a very brave retreat.'
799 zipf = zipfile.ZipFile(TESTFN, mode="w")
800 zipf.comment = comment
801 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
802 zipf.close()
803 zipfr = zipfile.ZipFile(TESTFN, mode="r")
804 self.assertEqual(zipfr.comment, comment)
805 zipfr.close()
806
807 # check a comment of max length
808 comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)])
809 zipf = zipfile.ZipFile(TESTFN, mode="w")
810 zipf.comment = comment2
811 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
812 zipf.close()
813 zipfr = zipfile.ZipFile(TESTFN, mode="r")
814 self.assertEqual(zipfr.comment, comment2)
815 zipfr.close()
816
817 # check a comment that is too long is truncated
818 zipf = zipfile.ZipFile(TESTFN, mode="w")
819 zipf.comment = comment2 + 'oops'
820 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
821 zipf.close()
822 zipfr = zipfile.ZipFile(TESTFN, mode="r")
823 self.assertEqual(zipfr.comment, comment2)
824 zipfr.close()
825
Collin Winter04a51ec2007-03-29 02:28:16 +0000826 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000827 unlink(TESTFN)
828 unlink(TESTFN2)
829
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000830
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000831class DecryptionTests(unittest.TestCase):
832 # This test checks that ZIP decryption works. Since the library does not
833 # support encryption at the moment, we use a pre-generated encrypted
834 # ZIP file
835
836 data = (
837 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
838 '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
839 '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
840 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
841 '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
842 '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
843 '\x00\x00L\x00\x00\x00\x00\x00' )
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000844 data2 = (
845 'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
846 '\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
847 '\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
848 'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
849 '\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
850 '\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
851 'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
852 '\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000853
854 plain = 'zipfile.py encryption test'
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000855 plain2 = '\x00'*512
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000856
857 def setUp(self):
858 fp = open(TESTFN, "wb")
859 fp.write(self.data)
860 fp.close()
861 self.zip = zipfile.ZipFile(TESTFN, "r")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000862 fp = open(TESTFN2, "wb")
863 fp.write(self.data2)
864 fp.close()
865 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000866
867 def tearDown(self):
868 self.zip.close()
869 os.unlink(TESTFN)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000870 self.zip2.close()
871 os.unlink(TESTFN2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000872
Ezio Melottid5a23e32009-07-15 17:07:04 +0000873 def test_no_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000874 # Reading the encrypted file without password
875 # must generate a RunTime exception
876 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000877 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000878
Ezio Melottid5a23e32009-07-15 17:07:04 +0000879 def test_bad_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000880 self.zip.setpassword("perl")
881 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000882 self.zip2.setpassword("perl")
883 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Tim Petersea5962f2007-03-12 18:07:52 +0000884
Ezio Melottid5a23e32009-07-15 17:07:04 +0000885 def test_good_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000886 self.zip.setpassword("python")
887 self.assertEquals(self.zip.read("test.txt"), self.plain)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000888 self.zip2.setpassword("12345")
889 self.assertEquals(self.zip2.read("zero"), self.plain2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000890
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000891
892class TestsWithRandomBinaryFiles(unittest.TestCase):
893 def setUp(self):
894 datacount = randint(16, 64)*1024 + randint(1, 1024)
895 self.data = ''.join((struct.pack('<f', random()*randint(-1000, 1000)) for i in xrange(datacount)))
896
897 # Make a source file with some lines
898 fp = open(TESTFN, "wb")
899 fp.write(self.data)
900 fp.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000901
Collin Winter04a51ec2007-03-29 02:28:16 +0000902 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000903 unlink(TESTFN)
904 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000905
Ezio Melottid5a23e32009-07-15 17:07:04 +0000906 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000907 # Create the ZIP archive
908 zipfp = zipfile.ZipFile(f, "w", compression)
909 zipfp.write(TESTFN, "another"+os.extsep+"name")
910 zipfp.write(TESTFN, TESTFN)
911 zipfp.close()
912
Ezio Melottid5a23e32009-07-15 17:07:04 +0000913 def zip_test(self, f, compression):
914 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000915
916 # Read the ZIP archive
917 zipfp = zipfile.ZipFile(f, "r", compression)
918 testdata = zipfp.read(TESTFN)
919 self.assertEqual(len(testdata), len(self.data))
920 self.assertEqual(testdata, self.data)
921 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
922 zipfp.close()
923
Ezio Melottid5a23e32009-07-15 17:07:04 +0000924 def test_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000925 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000926 self.zip_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +0000927
Ezio Melottid5a23e32009-07-15 17:07:04 +0000928 def zip_open_test(self, f, compression):
929 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000930
931 # Read the ZIP archive
932 zipfp = zipfile.ZipFile(f, "r", compression)
933 zipdata1 = []
934 zipopen1 = zipfp.open(TESTFN)
935 while 1:
936 read_data = zipopen1.read(256)
937 if not read_data:
938 break
939 zipdata1.append(read_data)
940
941 zipdata2 = []
942 zipopen2 = zipfp.open("another"+os.extsep+"name")
943 while 1:
944 read_data = zipopen2.read(256)
945 if not read_data:
946 break
947 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +0000948
949 testdata1 = ''.join(zipdata1)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000950 self.assertEqual(len(testdata1), len(self.data))
951 self.assertEqual(testdata1, self.data)
952
Tim Petersea5962f2007-03-12 18:07:52 +0000953 testdata2 = ''.join(zipdata2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000954 self.assertEqual(len(testdata1), len(self.data))
955 self.assertEqual(testdata1, self.data)
956 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000957
Ezio Melottid5a23e32009-07-15 17:07:04 +0000958 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000959 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000960 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000961
Ezio Melottid5a23e32009-07-15 17:07:04 +0000962 def zip_random_open_test(self, f, compression):
963 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000964
965 # Read the ZIP archive
966 zipfp = zipfile.ZipFile(f, "r", compression)
967 zipdata1 = []
968 zipopen1 = zipfp.open(TESTFN)
969 while 1:
970 read_data = zipopen1.read(randint(1, 1024))
971 if not read_data:
972 break
973 zipdata1.append(read_data)
974
975 testdata = ''.join(zipdata1)
976 self.assertEqual(len(testdata), len(self.data))
977 self.assertEqual(testdata, self.data)
978 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000979
Ezio Melottid5a23e32009-07-15 17:07:04 +0000980 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000981 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000982 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000983
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000984
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000985class TestsWithMultipleOpens(unittest.TestCase):
986 def setUp(self):
987 # Create the ZIP archive
988 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
989 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
990 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
991 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000992
Ezio Melottid5a23e32009-07-15 17:07:04 +0000993 def test_same_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000994 # Verify that (when the ZipFile is in control of creating file objects)
995 # multiple open() calls can be made without interfering with each other.
996 zipf = zipfile.ZipFile(TESTFN2, mode="r")
997 zopen1 = zipf.open('ones')
998 zopen2 = zipf.open('ones')
999 data1 = zopen1.read(500)
1000 data2 = zopen2.read(500)
1001 data1 += zopen1.read(500)
1002 data2 += zopen2.read(500)
1003 self.assertEqual(data1, data2)
1004 zipf.close()
1005
Ezio Melottid5a23e32009-07-15 17:07:04 +00001006 def test_different_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001007 # Verify that (when the ZipFile is in control of creating file objects)
1008 # multiple open() calls can be made without interfering with each other.
1009 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1010 zopen1 = zipf.open('ones')
1011 zopen2 = zipf.open('twos')
1012 data1 = zopen1.read(500)
1013 data2 = zopen2.read(500)
1014 data1 += zopen1.read(500)
1015 data2 += zopen2.read(500)
1016 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1017 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
1018 zipf.close()
1019
Ezio Melottid5a23e32009-07-15 17:07:04 +00001020 def test_interleaved(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001021 # Verify that (when the ZipFile is in control of creating file objects)
1022 # multiple open() calls can be made without interfering with each other.
1023 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1024 zopen1 = zipf.open('ones')
1025 data1 = zopen1.read(500)
1026 zopen2 = zipf.open('twos')
1027 data2 = zopen2.read(500)
1028 data1 += zopen1.read(500)
1029 data2 += zopen2.read(500)
1030 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1031 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
1032 zipf.close()
Tim Petersea5962f2007-03-12 18:07:52 +00001033
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001034 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001035 unlink(TESTFN2)
1036
Tim Petersea5962f2007-03-12 18:07:52 +00001037
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001038class TestWithDirectory(unittest.TestCase):
1039 def setUp(self):
1040 os.mkdir(TESTFN2)
1041
Ezio Melottid5a23e32009-07-15 17:07:04 +00001042 def test_extract_dir(self):
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001043 zipf = zipfile.ZipFile(findfile("zipdir.zip"))
1044 zipf.extractall(TESTFN2)
1045 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1046 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1047 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1048
Ezio Melottid5a23e32009-07-15 17:07:04 +00001049 def test_bug_6050(self):
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001050 # Extraction should succeed if directories already exist
1051 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottid5a23e32009-07-15 17:07:04 +00001052 self.test_extract_dir()
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001053
Ezio Melottid5a23e32009-07-15 17:07:04 +00001054 def test_store_dir(self):
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001055 os.mkdir(os.path.join(TESTFN2, "x"))
1056 zipf = zipfile.ZipFile(TESTFN, "w")
1057 zipf.write(os.path.join(TESTFN2, "x"), "x")
1058 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1059
1060 def tearDown(self):
1061 shutil.rmtree(TESTFN2)
1062 if os.path.exists(TESTFN):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001063 unlink(TESTFN)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001064
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001065
1066class UniversalNewlineTests(unittest.TestCase):
1067 def setUp(self):
1068 self.line_gen = ["Test of zipfile line %d." % i for i in xrange(FIXEDTEST_SIZE)]
1069 self.seps = ('\r', '\r\n', '\n')
1070 self.arcdata, self.arcfiles = {}, {}
1071 for n, s in enumerate(self.seps):
1072 self.arcdata[s] = s.join(self.line_gen) + s
1073 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Brett Cannon6cef0762007-05-25 20:17:15 +00001074 open(self.arcfiles[s], "wb").write(self.arcdata[s])
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001075
Ezio Melottid5a23e32009-07-15 17:07:04 +00001076 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001077 # Create the ZIP archive
1078 zipfp = zipfile.ZipFile(f, "w", compression)
1079 for fn in self.arcfiles.values():
1080 zipfp.write(fn, fn)
1081 zipfp.close()
1082
Ezio Melottid5a23e32009-07-15 17:07:04 +00001083 def read_test(self, f, compression):
1084 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001085
1086 # Read the ZIP archive
1087 zipfp = zipfile.ZipFile(f, "r")
1088 for sep, fn in self.arcfiles.items():
1089 zipdata = zipfp.open(fn, "rU").read()
1090 self.assertEqual(self.arcdata[sep], zipdata)
1091
1092 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +00001093
Ezio Melottid5a23e32009-07-15 17:07:04 +00001094 def readline_test(self, f, compression):
1095 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001096
1097 # Read the ZIP archive
1098 zipfp = zipfile.ZipFile(f, "r")
1099 for sep, fn in self.arcfiles.items():
1100 zipopen = zipfp.open(fn, "rU")
1101 for line in self.line_gen:
1102 linedata = zipopen.readline()
1103 self.assertEqual(linedata, line + '\n')
1104
1105 zipfp.close()
1106
Ezio Melottid5a23e32009-07-15 17:07:04 +00001107 def readlines_test(self, f, compression):
1108 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001109
1110 # Read the ZIP archive
1111 zipfp = zipfile.ZipFile(f, "r")
1112 for sep, fn in self.arcfiles.items():
1113 ziplines = zipfp.open(fn, "rU").readlines()
1114 for line, zipline in zip(self.line_gen, ziplines):
1115 self.assertEqual(zipline, line + '\n')
1116
1117 zipfp.close()
1118
Ezio Melottid5a23e32009-07-15 17:07:04 +00001119 def iterlines_test(self, f, compression):
1120 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001121
1122 # Read the ZIP archive
1123 zipfp = zipfile.ZipFile(f, "r")
1124 for sep, fn in self.arcfiles.items():
1125 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
1126 self.assertEqual(zipline, line + '\n')
1127
1128 zipfp.close()
1129
Ezio Melottid5a23e32009-07-15 17:07:04 +00001130 def test_read_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001131 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001132 self.read_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001133
Ezio Melottid5a23e32009-07-15 17:07:04 +00001134 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001135 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001136 self.readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001137
Ezio Melottid5a23e32009-07-15 17:07:04 +00001138 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001139 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001140 self.readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001141
Ezio Melottid5a23e32009-07-15 17:07:04 +00001142 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001143 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001144 self.iterlines_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001145
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001146 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001147 def test_read_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001148 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001149 self.read_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001150
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001151 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001152 def test_readline_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001153 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001154 self.readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001155
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001156 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001157 def test_readlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001158 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001159 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001160
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001161 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001162 def test_iterlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001163 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001164 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001165
1166 def tearDown(self):
1167 for sep, fn in self.arcfiles.items():
1168 os.remove(fn)
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001169 unlink(TESTFN)
1170 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001171
1172
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001173def test_main():
Tim Petersea5962f2007-03-12 18:07:52 +00001174 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1175 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001176 TestWithDirectory, UniversalNewlineTests,
1177 TestsWithRandomBinaryFiles)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001178
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001179if __name__ == "__main__":
1180 test_main()