blob: ee1eecb983d0069f699aafa51bd55bff2e3849f3 [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
Ezio Melottid5a23e32009-07-15 17:07:04 +0000706 def test_closed_zip_raises_RuntimeError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000707 # Verify that testzip() doesn't swallow inappropriate exceptions.
708 data = StringIO()
709 zipf = zipfile.ZipFile(data, mode="w")
710 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
711 zipf.close()
712
713 # This is correct; calling .read on a closed ZipFile should throw
714 # a RuntimeError, and so should calling .testzip. An earlier
715 # version of .testzip would swallow this exception (and any other)
716 # and report that the first file in the archive was corrupt.
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000717 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
718 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000719 self.assertRaises(RuntimeError, zipf.testzip)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000720 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
721 file(TESTFN, 'w').write('zipfile test data')
722 self.assertRaises(RuntimeError, zipf.write, TESTFN)
723
Ezio Melottid5a23e32009-07-15 17:07:04 +0000724 def test_bad_constructor_mode(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000725 # Check that bad modes passed to ZipFile constructor are caught
726 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
727
Ezio Melottid5a23e32009-07-15 17:07:04 +0000728 def test_bad_open_mode(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000729 # Check that bad modes passed to ZipFile.open are caught
730 zipf = zipfile.ZipFile(TESTFN, mode="w")
731 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
732 zipf.close()
733 zipf = zipfile.ZipFile(TESTFN, mode="r")
734 # read the data to make sure the file is there
735 zipf.read("foo.txt")
736 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
737 zipf.close()
738
Ezio Melottid5a23e32009-07-15 17:07:04 +0000739 def test_read0(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000740 # Check that calling read(0) on a ZipExtFile object returns an empty
741 # string and doesn't advance file pointer
742 zipf = zipfile.ZipFile(TESTFN, mode="w")
743 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
744 # read the data to make sure the file is there
745 f = zipf.open("foo.txt")
746 for i in xrange(FIXEDTEST_SIZE):
747 self.assertEqual(f.read(0), '')
748
749 self.assertEqual(f.read(), "O, for a Muse of Fire!")
750 zipf.close()
751
Ezio Melottid5a23e32009-07-15 17:07:04 +0000752 def test_open_non_existent_item(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000753 # Check that attempting to call open() for an item that doesn't
754 # exist in the archive raises a RuntimeError
755 zipf = zipfile.ZipFile(TESTFN, mode="w")
756 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
757
Ezio Melottid5a23e32009-07-15 17:07:04 +0000758 def test_bad_compression_mode(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000759 # Check that bad compression methods passed to ZipFile.open are caught
760 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
761
Ezio Melottid5a23e32009-07-15 17:07:04 +0000762 def test_null_byte_in_filename(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000763 # Check that a filename containing a null byte is properly terminated
764 zipf = zipfile.ZipFile(TESTFN, mode="w")
765 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
766 self.assertEqual(zipf.namelist(), ['foo.txt'])
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000767
Ezio Melottid5a23e32009-07-15 17:07:04 +0000768 def test_struct_sizes(self):
Martin v. Löwis8c436412008-07-03 12:51:14 +0000769 # check that ZIP internal structure sizes are calculated correctly
770 self.assertEqual(zipfile.sizeEndCentDir, 22)
771 self.assertEqual(zipfile.sizeCentralDir, 46)
772 self.assertEqual(zipfile.sizeEndCentDir64, 56)
773 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
774
Ezio Melottid5a23e32009-07-15 17:07:04 +0000775 def test_comments(self):
Martin v. Löwis8c436412008-07-03 12:51:14 +0000776 # This test checks that comments on the archive are handled properly
777
778 # check default comment is empty
779 zipf = zipfile.ZipFile(TESTFN, mode="w")
780 self.assertEqual(zipf.comment, '')
781 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
782 zipf.close()
783 zipfr = zipfile.ZipFile(TESTFN, mode="r")
784 self.assertEqual(zipfr.comment, '')
785 zipfr.close()
786
787 # check a simple short comment
788 comment = 'Bravely taking to his feet, he beat a very brave retreat.'
789 zipf = zipfile.ZipFile(TESTFN, mode="w")
790 zipf.comment = 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, comment)
795 zipfr.close()
796
797 # check a comment of max length
798 comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)])
799 zipf = zipfile.ZipFile(TESTFN, mode="w")
800 zipf.comment = comment2
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, comment2)
805 zipfr.close()
806
807 # check a comment that is too long is truncated
808 zipf = zipfile.ZipFile(TESTFN, mode="w")
809 zipf.comment = comment2 + 'oops'
810 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
811 zipf.close()
812 zipfr = zipfile.ZipFile(TESTFN, mode="r")
813 self.assertEqual(zipfr.comment, comment2)
814 zipfr.close()
815
Collin Winter04a51ec2007-03-29 02:28:16 +0000816 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000817 unlink(TESTFN)
818 unlink(TESTFN2)
819
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000820
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000821class DecryptionTests(unittest.TestCase):
822 # This test checks that ZIP decryption works. Since the library does not
823 # support encryption at the moment, we use a pre-generated encrypted
824 # ZIP file
825
826 data = (
827 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
828 '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
829 '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
830 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
831 '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
832 '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
833 '\x00\x00L\x00\x00\x00\x00\x00' )
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000834 data2 = (
835 'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
836 '\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
837 '\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
838 'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
839 '\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
840 '\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
841 'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
842 '\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000843
844 plain = 'zipfile.py encryption test'
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000845 plain2 = '\x00'*512
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000846
847 def setUp(self):
848 fp = open(TESTFN, "wb")
849 fp.write(self.data)
850 fp.close()
851 self.zip = zipfile.ZipFile(TESTFN, "r")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000852 fp = open(TESTFN2, "wb")
853 fp.write(self.data2)
854 fp.close()
855 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000856
857 def tearDown(self):
858 self.zip.close()
859 os.unlink(TESTFN)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000860 self.zip2.close()
861 os.unlink(TESTFN2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000862
Ezio Melottid5a23e32009-07-15 17:07:04 +0000863 def test_no_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000864 # Reading the encrypted file without password
865 # must generate a RunTime exception
866 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000867 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000868
Ezio Melottid5a23e32009-07-15 17:07:04 +0000869 def test_bad_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000870 self.zip.setpassword("perl")
871 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000872 self.zip2.setpassword("perl")
873 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Tim Petersea5962f2007-03-12 18:07:52 +0000874
Ezio Melottid5a23e32009-07-15 17:07:04 +0000875 def test_good_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000876 self.zip.setpassword("python")
877 self.assertEquals(self.zip.read("test.txt"), self.plain)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000878 self.zip2.setpassword("12345")
879 self.assertEquals(self.zip2.read("zero"), self.plain2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000880
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000881
882class TestsWithRandomBinaryFiles(unittest.TestCase):
883 def setUp(self):
884 datacount = randint(16, 64)*1024 + randint(1, 1024)
885 self.data = ''.join((struct.pack('<f', random()*randint(-1000, 1000)) for i in xrange(datacount)))
886
887 # Make a source file with some lines
888 fp = open(TESTFN, "wb")
889 fp.write(self.data)
890 fp.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000891
Collin Winter04a51ec2007-03-29 02:28:16 +0000892 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000893 unlink(TESTFN)
894 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000895
Ezio Melottid5a23e32009-07-15 17:07:04 +0000896 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000897 # Create the ZIP archive
898 zipfp = zipfile.ZipFile(f, "w", compression)
899 zipfp.write(TESTFN, "another"+os.extsep+"name")
900 zipfp.write(TESTFN, TESTFN)
901 zipfp.close()
902
Ezio Melottid5a23e32009-07-15 17:07:04 +0000903 def zip_test(self, f, compression):
904 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000905
906 # Read the ZIP archive
907 zipfp = zipfile.ZipFile(f, "r", compression)
908 testdata = zipfp.read(TESTFN)
909 self.assertEqual(len(testdata), len(self.data))
910 self.assertEqual(testdata, self.data)
911 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
912 zipfp.close()
913
Ezio Melottid5a23e32009-07-15 17:07:04 +0000914 def test_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000915 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000916 self.zip_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +0000917
Ezio Melottid5a23e32009-07-15 17:07:04 +0000918 def zip_open_test(self, f, compression):
919 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000920
921 # Read the ZIP archive
922 zipfp = zipfile.ZipFile(f, "r", compression)
923 zipdata1 = []
924 zipopen1 = zipfp.open(TESTFN)
925 while 1:
926 read_data = zipopen1.read(256)
927 if not read_data:
928 break
929 zipdata1.append(read_data)
930
931 zipdata2 = []
932 zipopen2 = zipfp.open("another"+os.extsep+"name")
933 while 1:
934 read_data = zipopen2.read(256)
935 if not read_data:
936 break
937 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +0000938
939 testdata1 = ''.join(zipdata1)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000940 self.assertEqual(len(testdata1), len(self.data))
941 self.assertEqual(testdata1, self.data)
942
Tim Petersea5962f2007-03-12 18:07:52 +0000943 testdata2 = ''.join(zipdata2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000944 self.assertEqual(len(testdata1), len(self.data))
945 self.assertEqual(testdata1, self.data)
946 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000947
Ezio Melottid5a23e32009-07-15 17:07:04 +0000948 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000949 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000950 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000951
Ezio Melottid5a23e32009-07-15 17:07:04 +0000952 def zip_random_open_test(self, f, compression):
953 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000954
955 # Read the ZIP archive
956 zipfp = zipfile.ZipFile(f, "r", compression)
957 zipdata1 = []
958 zipopen1 = zipfp.open(TESTFN)
959 while 1:
960 read_data = zipopen1.read(randint(1, 1024))
961 if not read_data:
962 break
963 zipdata1.append(read_data)
964
965 testdata = ''.join(zipdata1)
966 self.assertEqual(len(testdata), len(self.data))
967 self.assertEqual(testdata, self.data)
968 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000969
Ezio Melottid5a23e32009-07-15 17:07:04 +0000970 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000971 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000972 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000973
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000974
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000975class TestsWithMultipleOpens(unittest.TestCase):
976 def setUp(self):
977 # Create the ZIP archive
978 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
979 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
980 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
981 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000982
Ezio Melottid5a23e32009-07-15 17:07:04 +0000983 def test_same_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000984 # Verify that (when the ZipFile is in control of creating file objects)
985 # multiple open() calls can be made without interfering with each other.
986 zipf = zipfile.ZipFile(TESTFN2, mode="r")
987 zopen1 = zipf.open('ones')
988 zopen2 = zipf.open('ones')
989 data1 = zopen1.read(500)
990 data2 = zopen2.read(500)
991 data1 += zopen1.read(500)
992 data2 += zopen2.read(500)
993 self.assertEqual(data1, data2)
994 zipf.close()
995
Ezio Melottid5a23e32009-07-15 17:07:04 +0000996 def test_different_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000997 # Verify that (when the ZipFile is in control of creating file objects)
998 # multiple open() calls can be made without interfering with each other.
999 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1000 zopen1 = zipf.open('ones')
1001 zopen2 = zipf.open('twos')
1002 data1 = zopen1.read(500)
1003 data2 = zopen2.read(500)
1004 data1 += zopen1.read(500)
1005 data2 += zopen2.read(500)
1006 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1007 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
1008 zipf.close()
1009
Ezio Melottid5a23e32009-07-15 17:07:04 +00001010 def test_interleaved(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001011 # Verify that (when the ZipFile is in control of creating file objects)
1012 # multiple open() calls can be made without interfering with each other.
1013 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1014 zopen1 = zipf.open('ones')
1015 data1 = zopen1.read(500)
1016 zopen2 = zipf.open('twos')
1017 data2 = zopen2.read(500)
1018 data1 += zopen1.read(500)
1019 data2 += zopen2.read(500)
1020 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1021 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
1022 zipf.close()
Tim Petersea5962f2007-03-12 18:07:52 +00001023
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001024 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001025 unlink(TESTFN2)
1026
Tim Petersea5962f2007-03-12 18:07:52 +00001027
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001028class TestWithDirectory(unittest.TestCase):
1029 def setUp(self):
1030 os.mkdir(TESTFN2)
1031
Ezio Melottid5a23e32009-07-15 17:07:04 +00001032 def test_extract_dir(self):
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001033 zipf = zipfile.ZipFile(findfile("zipdir.zip"))
1034 zipf.extractall(TESTFN2)
1035 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1036 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1037 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1038
Ezio Melottid5a23e32009-07-15 17:07:04 +00001039 def test_bug_6050(self):
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001040 # Extraction should succeed if directories already exist
1041 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottid5a23e32009-07-15 17:07:04 +00001042 self.test_extract_dir()
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001043
Ezio Melottid5a23e32009-07-15 17:07:04 +00001044 def test_store_dir(self):
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001045 os.mkdir(os.path.join(TESTFN2, "x"))
1046 zipf = zipfile.ZipFile(TESTFN, "w")
1047 zipf.write(os.path.join(TESTFN2, "x"), "x")
1048 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1049
1050 def tearDown(self):
1051 shutil.rmtree(TESTFN2)
1052 if os.path.exists(TESTFN):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001053 unlink(TESTFN)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001054
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001055
1056class UniversalNewlineTests(unittest.TestCase):
1057 def setUp(self):
1058 self.line_gen = ["Test of zipfile line %d." % i for i in xrange(FIXEDTEST_SIZE)]
1059 self.seps = ('\r', '\r\n', '\n')
1060 self.arcdata, self.arcfiles = {}, {}
1061 for n, s in enumerate(self.seps):
1062 self.arcdata[s] = s.join(self.line_gen) + s
1063 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Brett Cannon6cef0762007-05-25 20:17:15 +00001064 open(self.arcfiles[s], "wb").write(self.arcdata[s])
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001065
Ezio Melottid5a23e32009-07-15 17:07:04 +00001066 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001067 # Create the ZIP archive
1068 zipfp = zipfile.ZipFile(f, "w", compression)
1069 for fn in self.arcfiles.values():
1070 zipfp.write(fn, fn)
1071 zipfp.close()
1072
Ezio Melottid5a23e32009-07-15 17:07:04 +00001073 def read_test(self, f, compression):
1074 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001075
1076 # Read the ZIP archive
1077 zipfp = zipfile.ZipFile(f, "r")
1078 for sep, fn in self.arcfiles.items():
1079 zipdata = zipfp.open(fn, "rU").read()
1080 self.assertEqual(self.arcdata[sep], zipdata)
1081
1082 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +00001083
Ezio Melottid5a23e32009-07-15 17:07:04 +00001084 def readline_test(self, f, compression):
1085 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001086
1087 # Read the ZIP archive
1088 zipfp = zipfile.ZipFile(f, "r")
1089 for sep, fn in self.arcfiles.items():
1090 zipopen = zipfp.open(fn, "rU")
1091 for line in self.line_gen:
1092 linedata = zipopen.readline()
1093 self.assertEqual(linedata, line + '\n')
1094
1095 zipfp.close()
1096
Ezio Melottid5a23e32009-07-15 17:07:04 +00001097 def readlines_test(self, f, compression):
1098 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001099
1100 # Read the ZIP archive
1101 zipfp = zipfile.ZipFile(f, "r")
1102 for sep, fn in self.arcfiles.items():
1103 ziplines = zipfp.open(fn, "rU").readlines()
1104 for line, zipline in zip(self.line_gen, ziplines):
1105 self.assertEqual(zipline, line + '\n')
1106
1107 zipfp.close()
1108
Ezio Melottid5a23e32009-07-15 17:07:04 +00001109 def iterlines_test(self, f, compression):
1110 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001111
1112 # Read the ZIP archive
1113 zipfp = zipfile.ZipFile(f, "r")
1114 for sep, fn in self.arcfiles.items():
1115 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
1116 self.assertEqual(zipline, line + '\n')
1117
1118 zipfp.close()
1119
Ezio Melottid5a23e32009-07-15 17:07:04 +00001120 def test_read_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001121 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001122 self.read_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001123
Ezio Melottid5a23e32009-07-15 17:07:04 +00001124 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001125 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001126 self.readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001127
Ezio Melottid5a23e32009-07-15 17:07:04 +00001128 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001129 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001130 self.readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001131
Ezio Melottid5a23e32009-07-15 17:07:04 +00001132 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001133 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001134 self.iterlines_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001135
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001136 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001137 def test_read_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001138 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001139 self.read_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001140
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001141 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001142 def test_readline_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001143 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001144 self.readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001145
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001146 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001147 def test_readlines_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.readlines_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_iterlines_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.iterlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001155
1156 def tearDown(self):
1157 for sep, fn in self.arcfiles.items():
1158 os.remove(fn)
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001159 unlink(TESTFN)
1160 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001161
1162
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001163def test_main():
Tim Petersea5962f2007-03-12 18:07:52 +00001164 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1165 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001166 TestWithDirectory, UniversalNewlineTests,
1167 TestsWithRandomBinaryFiles)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001168
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001169if __name__ == "__main__":
1170 test_main()