blob: 3f86b32adb8c52aa368e5c69bac23445014540f0 [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 Melotti1036a7f2009-09-12 14:43:43 +0000314 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000315 def test_per_file_compression(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000316 # Check that files within a Zip archive can have different compression options
317 zipfp = zipfile.ZipFile(TESTFN2, "w")
318 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
319 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
320 sinfo = zipfp.getinfo('storeme')
321 dinfo = zipfp.getinfo('deflateme')
322 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
323 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
324 zipfp.close()
325
Ezio Melottid5a23e32009-07-15 17:07:04 +0000326 def test_write_to_readonly(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000327 # Check that trying to call write() on a readonly ZipFile object
328 # raises a RuntimeError
329 zipf = zipfile.ZipFile(TESTFN2, mode="w")
330 zipf.writestr("somefile.txt", "bogus")
331 zipf.close()
332 zipf = zipfile.ZipFile(TESTFN2, mode="r")
333 self.assertRaises(RuntimeError, zipf.write, TESTFN)
334 zipf.close()
335
Ezio Melottid5a23e32009-07-15 17:07:04 +0000336 def test_extract(self):
Georg Brandl62416bc2008-01-07 18:47:44 +0000337 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
338 for fpath, fdata in SMALL_TEST_DATA:
339 zipfp.writestr(fpath, fdata)
340 zipfp.close()
341
342 zipfp = zipfile.ZipFile(TESTFN2, "r")
343 for fpath, fdata in SMALL_TEST_DATA:
344 writtenfile = zipfp.extract(fpath)
345
346 # make sure it was written to the right place
347 if os.path.isabs(fpath):
348 correctfile = os.path.join(os.getcwd(), fpath[1:])
349 else:
350 correctfile = os.path.join(os.getcwd(), fpath)
Christian Heimesa2af2122008-01-26 16:43:35 +0000351 correctfile = os.path.normpath(correctfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000352
353 self.assertEqual(writtenfile, correctfile)
354
355 # make sure correct data is in correct file
356 self.assertEqual(fdata, file(writtenfile, "rb").read())
357
358 os.remove(writtenfile)
359
360 zipfp.close()
361
362 # remove the test file subdirectories
363 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
364
Ezio Melottid5a23e32009-07-15 17:07:04 +0000365 def test_extract_all(self):
Georg Brandl62416bc2008-01-07 18:47:44 +0000366 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
367 for fpath, fdata in SMALL_TEST_DATA:
368 zipfp.writestr(fpath, fdata)
369 zipfp.close()
370
371 zipfp = zipfile.ZipFile(TESTFN2, "r")
372 zipfp.extractall()
373 for fpath, fdata in SMALL_TEST_DATA:
374 if os.path.isabs(fpath):
375 outfile = os.path.join(os.getcwd(), fpath[1:])
376 else:
377 outfile = os.path.join(os.getcwd(), fpath)
378
379 self.assertEqual(fdata, file(outfile, "rb").read())
380
381 os.remove(outfile)
382
383 zipfp.close()
384
385 # remove the test file subdirectories
386 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
387
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000388 def zip_test_writestr_permissions(self, f, compression):
389 # Make sure that writestr creates files with mode 0600,
390 # when it is passed a name rather than a ZipInfo instance.
391
Ezio Melottid5a23e32009-07-15 17:07:04 +0000392 self.make_test_archive(f, compression)
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000393 zipfp = zipfile.ZipFile(f, "r")
394 zinfo = zipfp.getinfo('strfile')
395 self.assertEqual(zinfo.external_attr, 0600 << 16)
396
Ezio Melottid5a23e32009-07-15 17:07:04 +0000397 def test_writestr_permissions(self):
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000398 for f in (TESTFN2, TemporaryFile(), StringIO()):
399 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
400
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000401 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000402 unlink(TESTFN)
403 unlink(TESTFN2)
404
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000405
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000406class TestZip64InSmallFiles(unittest.TestCase):
407 # These tests test the ZIP64 functionality without using large files,
408 # see test_zipfile64 for proper tests.
409
410 def setUp(self):
411 self._limit = zipfile.ZIP64_LIMIT
412 zipfile.ZIP64_LIMIT = 5
413
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000414 line_gen = ("Test of zipfile line %d." % i for i in range(0, FIXEDTEST_SIZE))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000415 self.data = '\n'.join(line_gen)
416
417 # Make a source file with some lines
418 fp = open(TESTFN, "wb")
419 fp.write(self.data)
420 fp.close()
421
Ezio Melottid5a23e32009-07-15 17:07:04 +0000422 def large_file_exception_test(self, f, compression):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000423 zipfp = zipfile.ZipFile(f, "w", compression)
Tim Petersa608bb22006-06-15 18:06:29 +0000424 self.assertRaises(zipfile.LargeZipFile,
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000425 zipfp.write, TESTFN, "another"+os.extsep+"name")
426 zipfp.close()
427
Ezio Melottid5a23e32009-07-15 17:07:04 +0000428 def large_file_exception_test2(self, f, compression):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000429 zipfp = zipfile.ZipFile(f, "w", compression)
Tim Petersa608bb22006-06-15 18:06:29 +0000430 self.assertRaises(zipfile.LargeZipFile,
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000431 zipfp.writestr, "another"+os.extsep+"name", self.data)
432 zipfp.close()
433
Ezio Melottid5a23e32009-07-15 17:07:04 +0000434 def test_large_file_exception(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000435 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000436 self.large_file_exception_test(f, zipfile.ZIP_STORED)
437 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000438
Ezio Melottid5a23e32009-07-15 17:07:04 +0000439 def zip_test(self, f, compression):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000440 # Create the ZIP archive
441 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
442 zipfp.write(TESTFN, "another"+os.extsep+"name")
443 zipfp.write(TESTFN, TESTFN)
444 zipfp.writestr("strfile", self.data)
445 zipfp.close()
446
447 # Read the ZIP archive
448 zipfp = zipfile.ZipFile(f, "r", compression)
449 self.assertEqual(zipfp.read(TESTFN), self.data)
450 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
451 self.assertEqual(zipfp.read("strfile"), self.data)
452
453 # Print the ZIP directory
454 fp = StringIO()
455 stdout = sys.stdout
456 try:
457 sys.stdout = fp
458
459 zipfp.printdir()
460 finally:
461 sys.stdout = stdout
Tim Petersa608bb22006-06-15 18:06:29 +0000462
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000463 directory = fp.getvalue()
464 lines = directory.splitlines()
465 self.assertEquals(len(lines), 4) # Number of files + header
466
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000467 self.assertTrue('File Name' in lines[0])
468 self.assertTrue('Modified' in lines[0])
469 self.assertTrue('Size' in lines[0])
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000470
471 fn, date, time, size = lines[1].split()
472 self.assertEquals(fn, 'another.name')
473 # XXX: timestamp is not tested
474 self.assertEquals(size, str(len(self.data)))
475
476 # Check the namelist
477 names = zipfp.namelist()
478 self.assertEquals(len(names), 3)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000479 self.assertTrue(TESTFN in names)
480 self.assertTrue("another"+os.extsep+"name" in names)
481 self.assertTrue("strfile" in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000482
483 # Check infolist
484 infos = zipfp.infolist()
485 names = [ i.filename for i in infos ]
486 self.assertEquals(len(names), 3)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000487 self.assertTrue(TESTFN in names)
488 self.assertTrue("another"+os.extsep+"name" in names)
489 self.assertTrue("strfile" in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000490 for i in infos:
491 self.assertEquals(i.file_size, len(self.data))
492
493 # check getinfo
494 for nm in (TESTFN, "another"+os.extsep+"name", "strfile"):
495 info = zipfp.getinfo(nm)
496 self.assertEquals(info.filename, nm)
497 self.assertEquals(info.file_size, len(self.data))
498
499 # Check that testzip doesn't raise an exception
500 zipfp.testzip()
501
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000502 zipfp.close()
503
Ezio Melottid5a23e32009-07-15 17:07:04 +0000504 def test_stored(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000505 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000506 self.zip_test(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000507
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000508 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000509 def test_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000510 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000511 self.zip_test(f, zipfile.ZIP_DEFLATED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000512
Ezio Melottid5a23e32009-07-15 17:07:04 +0000513 def test_absolute_arcnames(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000514 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True)
515 zipfp.write(TESTFN, "/absolute")
516 zipfp.close()
517
518 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
519 self.assertEqual(zipfp.namelist(), ["absolute"])
520 zipfp.close()
521
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000522 def tearDown(self):
523 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000524 unlink(TESTFN)
525 unlink(TESTFN2)
526
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000527
528class PyZipFileTests(unittest.TestCase):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000529 def test_write_pyfile(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000530 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
531 fn = __file__
532 if fn.endswith('.pyc') or fn.endswith('.pyo'):
533 fn = fn[:-1]
534
535 zipfp.writepy(fn)
536
537 bn = os.path.basename(fn)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000538 self.assertTrue(bn not in zipfp.namelist())
539 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000540 zipfp.close()
541
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000542 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
543 fn = __file__
544 if fn.endswith('.pyc') or fn.endswith('.pyo'):
545 fn = fn[:-1]
546
547 zipfp.writepy(fn, "testpackage")
548
549 bn = "%s/%s"%("testpackage", os.path.basename(fn))
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000550 self.assertTrue(bn not in zipfp.namelist())
551 self.assertTrue(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000552 zipfp.close()
553
Ezio Melottid5a23e32009-07-15 17:07:04 +0000554 def test_write_python_package(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000555 import email
556 packagedir = os.path.dirname(email.__file__)
557
558 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
559 zipfp.writepy(packagedir)
560
561 # Check for a couple of modules at different levels of the hieararchy
562 names = zipfp.namelist()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000563 self.assertTrue('email/__init__.pyo' in names or 'email/__init__.pyc' in names)
564 self.assertTrue('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000565
Ezio Melottid5a23e32009-07-15 17:07:04 +0000566 def test_write_python_directory(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000567 os.mkdir(TESTFN2)
568 try:
569 fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
570 fp.write("print 42\n")
571 fp.close()
572
573 fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
574 fp.write("print 42 * 42\n")
575 fp.close()
576
577 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
578 fp.write("bla bla bla\n")
579 fp.close()
580
581 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
582 zipfp.writepy(TESTFN2)
583
584 names = zipfp.namelist()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000585 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
586 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
587 self.assertTrue('mod2.txt' not in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000588
589 finally:
590 shutil.rmtree(TESTFN2)
591
Ezio Melottid5a23e32009-07-15 17:07:04 +0000592 def test_write_non_pyfile(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000593 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
594 file(TESTFN, 'w').write('most definitely not a python file')
595 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
596 os.remove(TESTFN)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000597
598
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000599class OtherTests(unittest.TestCase):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000600 def test_unicode_filenames(self):
Martin v. Löwis471617d2008-05-05 17:16:58 +0000601 zf = zipfile.ZipFile(TESTFN, "w")
602 zf.writestr(u"foo.txt", "Test for unicode filename")
Martin v. Löwisc3ad68c2008-05-05 17:47:06 +0000603 zf.writestr(u"\xf6.txt", "Test for unicode filename")
604 self.assertTrue(isinstance(zf.infolist()[0].filename, unicode))
Martin v. Löwis471617d2008-05-05 17:16:58 +0000605 zf.close()
Martin v. Löwisc3ad68c2008-05-05 17:47:06 +0000606 zf = zipfile.ZipFile(TESTFN, "r")
607 self.assertEqual(zf.filelist[0].filename, "foo.txt")
608 self.assertEqual(zf.filelist[1].filename, u"\xf6.txt")
609 zf.close()
Martin v. Löwis471617d2008-05-05 17:16:58 +0000610
Ezio Melottid5a23e32009-07-15 17:07:04 +0000611 def test_create_non_existent_file_for_append(self):
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000612 if os.path.exists(TESTFN):
613 os.unlink(TESTFN)
Tim Petersea5962f2007-03-12 18:07:52 +0000614
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000615 filename = 'testfile.txt'
616 content = 'hello, world. this is some content.'
Tim Petersea5962f2007-03-12 18:07:52 +0000617
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000618 try:
619 zf = zipfile.ZipFile(TESTFN, 'a')
620 zf.writestr(filename, content)
621 zf.close()
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000622 except IOError:
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000623 self.fail('Could not append data to a non-existent zip file.')
624
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000625 self.assertTrue(os.path.exists(TESTFN))
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000626
627 zf = zipfile.ZipFile(TESTFN, 'r')
628 self.assertEqual(zf.read(filename), content)
629 zf.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000630
Ezio Melottid5a23e32009-07-15 17:07:04 +0000631 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000632 # This test checks that the ZipFile constructor closes the file object
633 # it opens if there's an error in the file. If it doesn't, the traceback
634 # holds a reference to the ZipFile object and, indirectly, the file object.
635 # On Windows, this causes the os.unlink() call to fail because the
636 # underlying file is still open. This is SF bug #412214.
637 #
638 fp = open(TESTFN, "w")
639 fp.write("this is not a legal zip file\n")
640 fp.close()
641 try:
642 zf = zipfile.ZipFile(TESTFN)
643 except zipfile.BadZipfile:
Collin Winter04a51ec2007-03-29 02:28:16 +0000644 pass
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000645
Ezio Melottid5a23e32009-07-15 17:07:04 +0000646 def test_is_zip_erroneous_file(self):
Tim Petersea5962f2007-03-12 18:07:52 +0000647 # This test checks that the is_zipfile function correctly identifies
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000648 # a file that is not a zip file
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000649
650 # - passing a filename
651 with open(TESTFN, "w") as fp:
652 fp.write("this is not a legal zip file\n")
Tim Petersea5962f2007-03-12 18:07:52 +0000653 chk = zipfile.is_zipfile(TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000654 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000655 # - passing a file object
656 with open(TESTFN, "rb") as fp:
657 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000658 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000659 # - passing a file-like object
660 fp = StringIO()
661 fp.write("this is not a legal zip file\n")
662 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000663 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000664 fp.seek(0,0)
665 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000666 self.assertTrue(not chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000667
Ezio Melottid5a23e32009-07-15 17:07:04 +0000668 def test_is_zip_valid_file(self):
Tim Petersea5962f2007-03-12 18:07:52 +0000669 # This test checks that the is_zipfile function correctly identifies
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000670 # a file that is a zip file
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000671
672 # - passing a filename
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000673 zipf = zipfile.ZipFile(TESTFN, mode="w")
674 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
675 zipf.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000676 chk = zipfile.is_zipfile(TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000677 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000678 # - passing a file object
679 with open(TESTFN, "rb") as fp:
680 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000681 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000682 fp.seek(0,0)
683 zip_contents = fp.read()
684 # - passing a file-like object
685 fp = StringIO()
686 fp.write(zip_contents)
687 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000688 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000689 fp.seek(0,0)
690 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000691 self.assertTrue(chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000692
Ezio Melottid5a23e32009-07-15 17:07:04 +0000693 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000694 # make sure we don't raise an AttributeError when a partially-constructed
695 # ZipFile instance is finalized; this tests for regression on SF tracker
696 # bug #403871.
697
698 # The bug we're testing for caused an AttributeError to be raised
699 # when a ZipFile instance was created for a file that did not
700 # exist; the .fp member was not initialized but was needed by the
701 # __del__() method. Since the AttributeError is in the __del__(),
702 # it is ignored, but the user should be sufficiently annoyed by
703 # the message on the output that regression will be noticed
704 # quickly.
705 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
706
Amaury Forgeot d'Arc3e5b0272009-07-28 22:15:30 +0000707 def test_empty_file_raises_BadZipFile(self):
708 f = open(TESTFN, 'w')
709 f.close()
710 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
711
712 f = open(TESTFN, 'w')
713 f.write("short file")
714 f.close()
715 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
716
Ezio Melottid5a23e32009-07-15 17:07:04 +0000717 def test_closed_zip_raises_RuntimeError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000718 # Verify that testzip() doesn't swallow inappropriate exceptions.
719 data = StringIO()
720 zipf = zipfile.ZipFile(data, mode="w")
721 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
722 zipf.close()
723
724 # This is correct; calling .read on a closed ZipFile should throw
725 # a RuntimeError, and so should calling .testzip. An earlier
726 # version of .testzip would swallow this exception (and any other)
727 # and report that the first file in the archive was corrupt.
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000728 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
729 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000730 self.assertRaises(RuntimeError, zipf.testzip)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000731 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
732 file(TESTFN, 'w').write('zipfile test data')
733 self.assertRaises(RuntimeError, zipf.write, TESTFN)
734
Ezio Melottid5a23e32009-07-15 17:07:04 +0000735 def test_bad_constructor_mode(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000736 # Check that bad modes passed to ZipFile constructor are caught
737 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
738
Ezio Melottid5a23e32009-07-15 17:07:04 +0000739 def test_bad_open_mode(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000740 # Check that bad modes passed to ZipFile.open are caught
741 zipf = zipfile.ZipFile(TESTFN, mode="w")
742 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
743 zipf.close()
744 zipf = zipfile.ZipFile(TESTFN, mode="r")
745 # read the data to make sure the file is there
746 zipf.read("foo.txt")
747 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
748 zipf.close()
749
Ezio Melottid5a23e32009-07-15 17:07:04 +0000750 def test_read0(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000751 # Check that calling read(0) on a ZipExtFile object returns an empty
752 # string and doesn't advance file pointer
753 zipf = zipfile.ZipFile(TESTFN, mode="w")
754 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
755 # read the data to make sure the file is there
756 f = zipf.open("foo.txt")
757 for i in xrange(FIXEDTEST_SIZE):
758 self.assertEqual(f.read(0), '')
759
760 self.assertEqual(f.read(), "O, for a Muse of Fire!")
761 zipf.close()
762
Ezio Melottid5a23e32009-07-15 17:07:04 +0000763 def test_open_non_existent_item(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000764 # Check that attempting to call open() for an item that doesn't
765 # exist in the archive raises a RuntimeError
766 zipf = zipfile.ZipFile(TESTFN, mode="w")
767 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
768
Ezio Melottid5a23e32009-07-15 17:07:04 +0000769 def test_bad_compression_mode(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000770 # Check that bad compression methods passed to ZipFile.open are caught
771 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
772
Ezio Melottid5a23e32009-07-15 17:07:04 +0000773 def test_null_byte_in_filename(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000774 # Check that a filename containing a null byte is properly terminated
775 zipf = zipfile.ZipFile(TESTFN, mode="w")
776 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
777 self.assertEqual(zipf.namelist(), ['foo.txt'])
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000778
Ezio Melottid5a23e32009-07-15 17:07:04 +0000779 def test_struct_sizes(self):
Martin v. Löwis8c436412008-07-03 12:51:14 +0000780 # check that ZIP internal structure sizes are calculated correctly
781 self.assertEqual(zipfile.sizeEndCentDir, 22)
782 self.assertEqual(zipfile.sizeCentralDir, 46)
783 self.assertEqual(zipfile.sizeEndCentDir64, 56)
784 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
785
Ezio Melottid5a23e32009-07-15 17:07:04 +0000786 def test_comments(self):
Martin v. Löwis8c436412008-07-03 12:51:14 +0000787 # This test checks that comments on the archive are handled properly
788
789 # check default comment is empty
790 zipf = zipfile.ZipFile(TESTFN, mode="w")
791 self.assertEqual(zipf.comment, '')
792 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
793 zipf.close()
794 zipfr = zipfile.ZipFile(TESTFN, mode="r")
795 self.assertEqual(zipfr.comment, '')
796 zipfr.close()
797
798 # check a simple short comment
799 comment = 'Bravely taking to his feet, he beat a very brave retreat.'
800 zipf = zipfile.ZipFile(TESTFN, mode="w")
801 zipf.comment = comment
802 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
803 zipf.close()
804 zipfr = zipfile.ZipFile(TESTFN, mode="r")
805 self.assertEqual(zipfr.comment, comment)
806 zipfr.close()
807
808 # check a comment of max length
809 comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)])
810 zipf = zipfile.ZipFile(TESTFN, mode="w")
811 zipf.comment = comment2
812 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
813 zipf.close()
814 zipfr = zipfile.ZipFile(TESTFN, mode="r")
815 self.assertEqual(zipfr.comment, comment2)
816 zipfr.close()
817
818 # check a comment that is too long is truncated
819 zipf = zipfile.ZipFile(TESTFN, mode="w")
820 zipf.comment = comment2 + 'oops'
821 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
822 zipf.close()
823 zipfr = zipfile.ZipFile(TESTFN, mode="r")
824 self.assertEqual(zipfr.comment, comment2)
825 zipfr.close()
826
Collin Winter04a51ec2007-03-29 02:28:16 +0000827 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000828 unlink(TESTFN)
829 unlink(TESTFN2)
830
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000831
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000832class DecryptionTests(unittest.TestCase):
833 # This test checks that ZIP decryption works. Since the library does not
834 # support encryption at the moment, we use a pre-generated encrypted
835 # ZIP file
836
837 data = (
838 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
839 '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
840 '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
841 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
842 '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
843 '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
844 '\x00\x00L\x00\x00\x00\x00\x00' )
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000845 data2 = (
846 'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
847 '\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
848 '\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
849 'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
850 '\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
851 '\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
852 'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
853 '\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000854
855 plain = 'zipfile.py encryption test'
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000856 plain2 = '\x00'*512
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000857
858 def setUp(self):
859 fp = open(TESTFN, "wb")
860 fp.write(self.data)
861 fp.close()
862 self.zip = zipfile.ZipFile(TESTFN, "r")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000863 fp = open(TESTFN2, "wb")
864 fp.write(self.data2)
865 fp.close()
866 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000867
868 def tearDown(self):
869 self.zip.close()
870 os.unlink(TESTFN)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000871 self.zip2.close()
872 os.unlink(TESTFN2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000873
Ezio Melottid5a23e32009-07-15 17:07:04 +0000874 def test_no_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000875 # Reading the encrypted file without password
876 # must generate a RunTime exception
877 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000878 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000879
Ezio Melottid5a23e32009-07-15 17:07:04 +0000880 def test_bad_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000881 self.zip.setpassword("perl")
882 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000883 self.zip2.setpassword("perl")
884 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Tim Petersea5962f2007-03-12 18:07:52 +0000885
Ezio Melotti1036a7f2009-09-12 14:43:43 +0000886 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000887 def test_good_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000888 self.zip.setpassword("python")
889 self.assertEquals(self.zip.read("test.txt"), self.plain)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000890 self.zip2.setpassword("12345")
891 self.assertEquals(self.zip2.read("zero"), self.plain2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000892
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000893
894class TestsWithRandomBinaryFiles(unittest.TestCase):
895 def setUp(self):
896 datacount = randint(16, 64)*1024 + randint(1, 1024)
897 self.data = ''.join((struct.pack('<f', random()*randint(-1000, 1000)) for i in xrange(datacount)))
898
899 # Make a source file with some lines
900 fp = open(TESTFN, "wb")
901 fp.write(self.data)
902 fp.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000903
Collin Winter04a51ec2007-03-29 02:28:16 +0000904 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000905 unlink(TESTFN)
906 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000907
Ezio Melottid5a23e32009-07-15 17:07:04 +0000908 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000909 # Create the ZIP archive
910 zipfp = zipfile.ZipFile(f, "w", compression)
911 zipfp.write(TESTFN, "another"+os.extsep+"name")
912 zipfp.write(TESTFN, TESTFN)
913 zipfp.close()
914
Ezio Melottid5a23e32009-07-15 17:07:04 +0000915 def zip_test(self, f, compression):
916 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000917
918 # Read the ZIP archive
919 zipfp = zipfile.ZipFile(f, "r", compression)
920 testdata = zipfp.read(TESTFN)
921 self.assertEqual(len(testdata), len(self.data))
922 self.assertEqual(testdata, self.data)
923 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
924 zipfp.close()
925
Ezio Melottid5a23e32009-07-15 17:07:04 +0000926 def test_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000927 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000928 self.zip_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +0000929
Ezio Melottid5a23e32009-07-15 17:07:04 +0000930 def zip_open_test(self, f, compression):
931 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000932
933 # Read the ZIP archive
934 zipfp = zipfile.ZipFile(f, "r", compression)
935 zipdata1 = []
936 zipopen1 = zipfp.open(TESTFN)
937 while 1:
938 read_data = zipopen1.read(256)
939 if not read_data:
940 break
941 zipdata1.append(read_data)
942
943 zipdata2 = []
944 zipopen2 = zipfp.open("another"+os.extsep+"name")
945 while 1:
946 read_data = zipopen2.read(256)
947 if not read_data:
948 break
949 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +0000950
951 testdata1 = ''.join(zipdata1)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000952 self.assertEqual(len(testdata1), len(self.data))
953 self.assertEqual(testdata1, self.data)
954
Tim Petersea5962f2007-03-12 18:07:52 +0000955 testdata2 = ''.join(zipdata2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000956 self.assertEqual(len(testdata1), len(self.data))
957 self.assertEqual(testdata1, self.data)
958 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000959
Ezio Melottid5a23e32009-07-15 17:07:04 +0000960 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000961 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000962 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000963
Ezio Melottid5a23e32009-07-15 17:07:04 +0000964 def zip_random_open_test(self, f, compression):
965 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000966
967 # Read the ZIP archive
968 zipfp = zipfile.ZipFile(f, "r", compression)
969 zipdata1 = []
970 zipopen1 = zipfp.open(TESTFN)
971 while 1:
972 read_data = zipopen1.read(randint(1, 1024))
973 if not read_data:
974 break
975 zipdata1.append(read_data)
976
977 testdata = ''.join(zipdata1)
978 self.assertEqual(len(testdata), len(self.data))
979 self.assertEqual(testdata, self.data)
980 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000981
Ezio Melottid5a23e32009-07-15 17:07:04 +0000982 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000983 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000984 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000985
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000986
Ezio Melotti1036a7f2009-09-12 14:43:43 +0000987@skipUnless(zlib, "requires zlib")
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000988class TestsWithMultipleOpens(unittest.TestCase):
989 def setUp(self):
990 # Create the ZIP archive
991 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
992 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
993 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
994 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +0000995
Ezio Melottid5a23e32009-07-15 17:07:04 +0000996 def test_same_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('ones')
1002 data1 = zopen1.read(500)
1003 data2 = zopen2.read(500)
1004 data1 += zopen1.read(500)
1005 data2 += zopen2.read(500)
1006 self.assertEqual(data1, data2)
1007 zipf.close()
1008
Ezio Melottid5a23e32009-07-15 17:07:04 +00001009 def test_different_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001010 # Verify that (when the ZipFile is in control of creating file objects)
1011 # multiple open() calls can be made without interfering with each other.
1012 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1013 zopen1 = zipf.open('ones')
1014 zopen2 = zipf.open('twos')
1015 data1 = zopen1.read(500)
1016 data2 = zopen2.read(500)
1017 data1 += zopen1.read(500)
1018 data2 += zopen2.read(500)
1019 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1020 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
1021 zipf.close()
1022
Ezio Melottid5a23e32009-07-15 17:07:04 +00001023 def test_interleaved(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001024 # Verify that (when the ZipFile is in control of creating file objects)
1025 # multiple open() calls can be made without interfering with each other.
1026 zipf = zipfile.ZipFile(TESTFN2, mode="r")
1027 zopen1 = zipf.open('ones')
1028 data1 = zopen1.read(500)
1029 zopen2 = zipf.open('twos')
1030 data2 = zopen2.read(500)
1031 data1 += zopen1.read(500)
1032 data2 += zopen2.read(500)
1033 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1034 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
1035 zipf.close()
Tim Petersea5962f2007-03-12 18:07:52 +00001036
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001037 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001038 unlink(TESTFN2)
1039
Tim Petersea5962f2007-03-12 18:07:52 +00001040
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001041class TestWithDirectory(unittest.TestCase):
1042 def setUp(self):
1043 os.mkdir(TESTFN2)
1044
Ezio Melottid5a23e32009-07-15 17:07:04 +00001045 def test_extract_dir(self):
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001046 zipf = zipfile.ZipFile(findfile("zipdir.zip"))
1047 zipf.extractall(TESTFN2)
1048 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1049 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1050 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1051
Ezio Melottid5a23e32009-07-15 17:07:04 +00001052 def test_bug_6050(self):
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001053 # Extraction should succeed if directories already exist
1054 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottid5a23e32009-07-15 17:07:04 +00001055 self.test_extract_dir()
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001056
Ezio Melottid5a23e32009-07-15 17:07:04 +00001057 def test_store_dir(self):
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001058 os.mkdir(os.path.join(TESTFN2, "x"))
1059 zipf = zipfile.ZipFile(TESTFN, "w")
1060 zipf.write(os.path.join(TESTFN2, "x"), "x")
1061 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1062
1063 def tearDown(self):
1064 shutil.rmtree(TESTFN2)
1065 if os.path.exists(TESTFN):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001066 unlink(TESTFN)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001067
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001068
1069class UniversalNewlineTests(unittest.TestCase):
1070 def setUp(self):
1071 self.line_gen = ["Test of zipfile line %d." % i for i in xrange(FIXEDTEST_SIZE)]
1072 self.seps = ('\r', '\r\n', '\n')
1073 self.arcdata, self.arcfiles = {}, {}
1074 for n, s in enumerate(self.seps):
1075 self.arcdata[s] = s.join(self.line_gen) + s
1076 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Brett Cannon6cef0762007-05-25 20:17:15 +00001077 open(self.arcfiles[s], "wb").write(self.arcdata[s])
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001078
Ezio Melottid5a23e32009-07-15 17:07:04 +00001079 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001080 # Create the ZIP archive
1081 zipfp = zipfile.ZipFile(f, "w", compression)
1082 for fn in self.arcfiles.values():
1083 zipfp.write(fn, fn)
1084 zipfp.close()
1085
Ezio Melottid5a23e32009-07-15 17:07:04 +00001086 def read_test(self, f, compression):
1087 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001088
1089 # Read the ZIP archive
1090 zipfp = zipfile.ZipFile(f, "r")
1091 for sep, fn in self.arcfiles.items():
1092 zipdata = zipfp.open(fn, "rU").read()
1093 self.assertEqual(self.arcdata[sep], zipdata)
1094
1095 zipfp.close()
Tim Petersea5962f2007-03-12 18:07:52 +00001096
Ezio Melottid5a23e32009-07-15 17:07:04 +00001097 def readline_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 zipopen = zipfp.open(fn, "rU")
1104 for line in self.line_gen:
1105 linedata = zipopen.readline()
1106 self.assertEqual(linedata, line + '\n')
1107
1108 zipfp.close()
1109
Ezio Melottid5a23e32009-07-15 17:07:04 +00001110 def readlines_test(self, f, compression):
1111 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001112
1113 # Read the ZIP archive
1114 zipfp = zipfile.ZipFile(f, "r")
1115 for sep, fn in self.arcfiles.items():
1116 ziplines = zipfp.open(fn, "rU").readlines()
1117 for line, zipline in zip(self.line_gen, ziplines):
1118 self.assertEqual(zipline, line + '\n')
1119
1120 zipfp.close()
1121
Ezio Melottid5a23e32009-07-15 17:07:04 +00001122 def iterlines_test(self, f, compression):
1123 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001124
1125 # Read the ZIP archive
1126 zipfp = zipfile.ZipFile(f, "r")
1127 for sep, fn in self.arcfiles.items():
1128 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
1129 self.assertEqual(zipline, line + '\n')
1130
1131 zipfp.close()
1132
Ezio Melottid5a23e32009-07-15 17:07:04 +00001133 def test_read_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001134 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001135 self.read_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001136
Ezio Melottid5a23e32009-07-15 17:07:04 +00001137 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001138 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001139 self.readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001140
Ezio Melottid5a23e32009-07-15 17:07:04 +00001141 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001142 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001143 self.readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001144
Ezio Melottid5a23e32009-07-15 17:07:04 +00001145 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001146 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001147 self.iterlines_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001148
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001149 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001150 def test_read_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001151 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001152 self.read_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001153
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001154 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001155 def test_readline_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001156 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001157 self.readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001158
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001159 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001160 def test_readlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001161 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001162 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001163
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001164 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001165 def test_iterlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001166 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001167 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001168
1169 def tearDown(self):
1170 for sep, fn in self.arcfiles.items():
1171 os.remove(fn)
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001172 unlink(TESTFN)
1173 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001174
1175
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001176def test_main():
Tim Petersea5962f2007-03-12 18:07:52 +00001177 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1178 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001179 TestWithDirectory, UniversalNewlineTests,
1180 TestsWithRandomBinaryFiles)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001181
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001182if __name__ == "__main__":
1183 test_main()