blob: ddd713f974c01395b181cad4e58f575a9681756c [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
Antoine Pitroue1436d12010-08-12 15:25:51 +00008import io
Ezio Melottie7a0cc22009-07-04 14:58:27 +00009import sys
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000010import time
Ezio Melottie7a0cc22009-07-04 14:58:27 +000011import shutil
12import struct
13import zipfile
14import unittest
Tim Petersa19a1682001-03-29 04:36:09 +000015
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000016from StringIO import StringIO
17from tempfile import TemporaryFile
Martin v. Löwis3eb76482007-03-06 10:41:24 +000018from random import randint, random
Ezio Melottie7a0cc22009-07-04 14:58:27 +000019from unittest import skipUnless
Tim Petersa19a1682001-03-29 04:36:09 +000020
Ezio Melotti6cbfc122009-07-10 20:25:56 +000021from test.test_support import TESTFN, run_unittest, findfile, unlink
Guido van Rossum368f04a2000-04-10 13:23:04 +000022
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000023TESTFN2 = TESTFN + "2"
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +000024TESTFNDIR = TESTFN + "d"
Georg Brandl4b3ab6f2007-07-12 09:59:22 +000025FIXEDTEST_SIZE = 1000
Guido van Rossum368f04a2000-04-10 13:23:04 +000026
Georg Brandl62416bc2008-01-07 18:47:44 +000027SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
28 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
29 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
30 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
31
Ezio Melotti6cbfc122009-07-10 20:25:56 +000032
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000033class TestsWithSourceFile(unittest.TestCase):
34 def setUp(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +000035 self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random())
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000036 for i in xrange(FIXEDTEST_SIZE)]
Martin v. Löwis3eb76482007-03-06 10:41:24 +000037 self.data = '\n'.join(self.line_gen) + '\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000038
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000039 # Make a source file with some lines
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000040 with open(TESTFN, "wb") as fp:
41 fp.write(self.data)
Tim Peters7d3bad62001-04-04 18:56:49 +000042
Ezio Melottid5a23e32009-07-15 17:07:04 +000043 def make_test_archive(self, f, compression):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000044 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +000045 with zipfile.ZipFile(f, "w", compression) as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000046 zipfp.write(TESTFN, "another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +000047 zipfp.write(TESTFN, TESTFN)
48 zipfp.writestr("strfile", self.data)
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
Ezio Melotti569e61f2009-12-30 06:14:51 +000054 with zipfile.ZipFile(f, "r", compression) as zipfp:
55 self.assertEqual(zipfp.read(TESTFN), self.data)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000056 self.assertEqual(zipfp.read("another.name"), self.data)
Ezio Melotti569e61f2009-12-30 06:14:51 +000057 self.assertEqual(zipfp.read("strfile"), self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000058
Ezio Melotti569e61f2009-12-30 06:14:51 +000059 # Print the ZIP directory
60 fp = StringIO()
61 stdout = sys.stdout
62 try:
63 sys.stdout = fp
64 zipfp.printdir()
65 finally:
66 sys.stdout = stdout
Tim Petersa608bb22006-06-15 18:06:29 +000067
Ezio Melotti569e61f2009-12-30 06:14:51 +000068 directory = fp.getvalue()
69 lines = directory.splitlines()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000070 self.assertEqual(len(lines), 4) # Number of files + header
Ronald Oussoren143cefb2006-06-15 08:14:18 +000071
Ezio Melottiaa980582010-01-23 23:04:36 +000072 self.assertIn('File Name', lines[0])
73 self.assertIn('Modified', lines[0])
74 self.assertIn('Size', lines[0])
Ronald Oussoren143cefb2006-06-15 08:14:18 +000075
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000076 fn, date, time_, size = lines[1].split()
77 self.assertEqual(fn, 'another.name')
78 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
79 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
80 self.assertEqual(size, str(len(self.data)))
Ronald Oussoren143cefb2006-06-15 08:14:18 +000081
Ezio Melotti569e61f2009-12-30 06:14:51 +000082 # Check the namelist
83 names = zipfp.namelist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000084 self.assertEqual(len(names), 3)
Ezio Melottiaa980582010-01-23 23:04:36 +000085 self.assertIn(TESTFN, names)
86 self.assertIn("another.name", names)
87 self.assertIn("strfile", names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000088
Ezio Melotti569e61f2009-12-30 06:14:51 +000089 # Check infolist
90 infos = zipfp.infolist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000091 names = [i.filename for i in infos]
92 self.assertEqual(len(names), 3)
Ezio Melottiaa980582010-01-23 23:04:36 +000093 self.assertIn(TESTFN, names)
94 self.assertIn("another.name", names)
95 self.assertIn("strfile", names)
Ezio Melotti569e61f2009-12-30 06:14:51 +000096 for i in infos:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000097 self.assertEqual(i.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +000098
Ezio Melotti569e61f2009-12-30 06:14:51 +000099 # check getinfo
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000100 for nm in (TESTFN, "another.name", "strfile"):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000101 info = zipfp.getinfo(nm)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000102 self.assertEqual(info.filename, nm)
103 self.assertEqual(info.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000104
Ezio Melotti569e61f2009-12-30 06:14:51 +0000105 # Check that testzip doesn't raise an exception
106 zipfp.testzip()
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
Ezio Melotti569e61f2009-12-30 06:14:51 +0000116 with zipfile.ZipFile(f, "r", compression) as zipfp:
117 zipdata1 = []
118 zipopen1 = zipfp.open(TESTFN)
119 while True:
120 read_data = zipopen1.read(256)
121 if not read_data:
122 break
123 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000124
Ezio Melotti569e61f2009-12-30 06:14:51 +0000125 zipdata2 = []
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000126 zipopen2 = zipfp.open("another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +0000127 while True:
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
Ezio Melotti569e61f2009-12-30 06:14:51 +0000133 self.assertEqual(''.join(zipdata1), self.data)
134 self.assertEqual(''.join(zipdata2), self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000135
Ezio Melottid5a23e32009-07-15 17:07:04 +0000136 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000137 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000138 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000139
Ezio Melottid5a23e32009-07-15 17:07:04 +0000140 def test_open_via_zip_info(self):
Georg Brandl112aa502008-05-20 08:25:48 +0000141 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000142 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
143 zipfp.writestr("name", "foo")
144 zipfp.writestr("name", "bar")
Georg Brandl112aa502008-05-20 08:25:48 +0000145
Ezio Melotti569e61f2009-12-30 06:14:51 +0000146 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
147 infos = zipfp.infolist()
148 data = ""
149 for info in infos:
150 data += zipfp.open(info).read()
151 self.assertTrue(data == "foobar" or data == "barfoo")
152 data = ""
153 for info in infos:
154 data += zipfp.read(info)
155 self.assertTrue(data == "foobar" or data == "barfoo")
Georg Brandl112aa502008-05-20 08:25:48 +0000156
Ezio Melottid5a23e32009-07-15 17:07:04 +0000157 def zip_random_open_test(self, f, compression):
158 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000159
160 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000161 with zipfile.ZipFile(f, "r", compression) as zipfp:
162 zipdata1 = []
163 zipopen1 = zipfp.open(TESTFN)
164 while True:
165 read_data = zipopen1.read(randint(1, 1024))
166 if not read_data:
167 break
168 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000169
Ezio Melotti569e61f2009-12-30 06:14:51 +0000170 self.assertEqual(''.join(zipdata1), self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000171
Ezio Melottid5a23e32009-07-15 17:07:04 +0000172 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000173 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000174 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +0000175
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000176 def test_univeral_readaheads(self):
177 f = StringIO()
178
179 data = 'a\r\n' * 16 * 1024
180 zipfp = zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED)
181 zipfp.writestr(TESTFN, data)
182 zipfp.close()
183
184 data2 = ''
185 zipfp = zipfile.ZipFile(f, 'r')
186 zipopen = zipfp.open(TESTFN, 'rU')
187 for line in zipopen:
188 data2 += line
189 zipfp.close()
190
191 self.assertEqual(data, data2.replace('\n', '\r\n'))
192
193 def zip_readline_read_test(self, f, compression):
194 self.make_test_archive(f, compression)
195
196 # Read the ZIP archive
197 zipfp = zipfile.ZipFile(f, "r")
198 zipopen = zipfp.open(TESTFN)
199
200 data = ''
201 while True:
202 read = zipopen.readline()
203 if not read:
204 break
205 data += read
206
207 read = zipopen.read(100)
208 if not read:
209 break
210 data += read
211
212 self.assertEqual(data, self.data)
213 zipfp.close()
214
Ezio Melottid5a23e32009-07-15 17:07:04 +0000215 def zip_readline_test(self, f, compression):
216 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000217
218 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000219 with zipfile.ZipFile(f, "r") as zipfp:
220 zipopen = zipfp.open(TESTFN)
221 for line in self.line_gen:
222 linedata = zipopen.readline()
223 self.assertEqual(linedata, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000224
Ezio Melottid5a23e32009-07-15 17:07:04 +0000225 def zip_readlines_test(self, f, compression):
226 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000227
228 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000229 with zipfile.ZipFile(f, "r") as zipfp:
230 ziplines = zipfp.open(TESTFN).readlines()
231 for line, zipline in zip(self.line_gen, ziplines):
232 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000233
Ezio Melottid5a23e32009-07-15 17:07:04 +0000234 def zip_iterlines_test(self, f, compression):
235 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000236
237 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000238 with zipfile.ZipFile(f, "r") as zipfp:
239 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
240 self.assertEqual(zipline, line + '\n')
Tim Petersea5962f2007-03-12 18:07:52 +0000241
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000242 def test_readline_read_stored(self):
243 # Issue #7610: calls to readline() interleaved with calls to read().
244 for f in (TESTFN2, TemporaryFile(), StringIO()):
245 self.zip_readline_read_test(f, zipfile.ZIP_STORED)
246
Ezio Melottid5a23e32009-07-15 17:07:04 +0000247 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000248 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000249 self.zip_readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000250
Ezio Melottid5a23e32009-07-15 17:07:04 +0000251 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000252 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000253 self.zip_readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000254
Ezio Melottid5a23e32009-07-15 17:07:04 +0000255 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000256 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000257 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000258
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000259 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000260 def test_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000261 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000262 self.zip_test(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000263
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000264 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000265 def test_open_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000266 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000267 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000268
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000269 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000270 def test_random_open_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000271 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000272 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000273
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000274 @skipUnless(zlib, "requires zlib")
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000275 def test_readline_read_deflated(self):
276 # Issue #7610: calls to readline() interleaved with calls to read().
277 for f in (TESTFN2, TemporaryFile(), StringIO()):
278 self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED)
279
280 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000281 def test_readline_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000282 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000283 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000284
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000285 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000286 def test_readlines_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000287 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000288 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000289
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000290 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000291 def test_iterlines_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000292 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000293 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
Tim Petersea5962f2007-03-12 18:07:52 +0000294
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000295 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000296 def test_low_compression(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000297 """Check for cases where compressed data is larger than original."""
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000298 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000299 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
300 zipfp.writestr("strfile", '12')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000301
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000302 # Get an open object for strfile
Ezio Melotti569e61f2009-12-30 06:14:51 +0000303 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
304 openobj = zipfp.open("strfile")
305 self.assertEqual(openobj.read(1), '1')
306 self.assertEqual(openobj.read(1), '2')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000307
Ezio Melottid5a23e32009-07-15 17:07:04 +0000308 def test_absolute_arcnames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000309 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
310 zipfp.write(TESTFN, "/absolute")
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000311
Ezio Melotti569e61f2009-12-30 06:14:51 +0000312 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
313 self.assertEqual(zipfp.namelist(), ["absolute"])
Tim Peters32cbc962006-02-20 21:42:18 +0000314
Ezio Melottid5a23e32009-07-15 17:07:04 +0000315 def test_append_to_zip_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000316 """Test appending to an existing zipfile."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000317 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
318 zipfp.write(TESTFN, TESTFN)
319
320 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
321 zipfp.writestr("strfile", self.data)
322 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000323
Ezio Melottid5a23e32009-07-15 17:07:04 +0000324 def test_append_to_non_zip_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000325 """Test appending to an existing file that is not a zipfile."""
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000326 # NOTE: this test fails if len(d) < 22 because of the first
327 # line "fpin.seek(-22, 2)" in _EndRecData
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000328 data = 'I am not a ZipFile!'*10
329 with open(TESTFN2, 'wb') as f:
330 f.write(data)
331
Ezio Melotti569e61f2009-12-30 06:14:51 +0000332 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
333 zipfp.write(TESTFN, TESTFN)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000334
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000335 with open(TESTFN2, 'rb') as f:
336 f.seek(len(data))
337 with zipfile.ZipFile(f, "r") as zipfp:
338 self.assertEqual(zipfp.namelist(), [TESTFN])
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000339
Ezio Melottid5a23e32009-07-15 17:07:04 +0000340 def test_write_default_name(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000341 """Check that calling ZipFile.write without arcname specified
342 produces the expected result."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000343 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
344 zipfp.write(TESTFN)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000345 self.assertEqual(zipfp.read(TESTFN), open(TESTFN).read())
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000346
Ezio Melotti1036a7f2009-09-12 14:43:43 +0000347 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000348 def test_per_file_compression(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000349 """Check that files within a Zip archive can have different
350 compression options."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000351 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
352 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
353 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
354 sinfo = zipfp.getinfo('storeme')
355 dinfo = zipfp.getinfo('deflateme')
356 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
357 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000358
Ezio Melottid5a23e32009-07-15 17:07:04 +0000359 def test_write_to_readonly(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000360 """Check that trying to call write() on a readonly ZipFile object
361 raises a RuntimeError."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000362 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
363 zipfp.writestr("somefile.txt", "bogus")
364
365 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
366 self.assertRaises(RuntimeError, zipfp.write, TESTFN)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000367
Ezio Melottid5a23e32009-07-15 17:07:04 +0000368 def test_extract(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000369 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
370 for fpath, fdata in SMALL_TEST_DATA:
371 zipfp.writestr(fpath, fdata)
Georg Brandl62416bc2008-01-07 18:47:44 +0000372
Ezio Melotti569e61f2009-12-30 06:14:51 +0000373 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
374 for fpath, fdata in SMALL_TEST_DATA:
375 writtenfile = zipfp.extract(fpath)
Georg Brandl62416bc2008-01-07 18:47:44 +0000376
Ezio Melotti569e61f2009-12-30 06:14:51 +0000377 # make sure it was written to the right place
378 if os.path.isabs(fpath):
379 correctfile = os.path.join(os.getcwd(), fpath[1:])
380 else:
381 correctfile = os.path.join(os.getcwd(), fpath)
382 correctfile = os.path.normpath(correctfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000383
Ezio Melotti569e61f2009-12-30 06:14:51 +0000384 self.assertEqual(writtenfile, correctfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000385
Ezio Melotti569e61f2009-12-30 06:14:51 +0000386 # make sure correct data is in correct file
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000387 self.assertEqual(fdata, open(writtenfile, "rb").read())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000388 os.remove(writtenfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000389
390 # remove the test file subdirectories
391 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
392
Ezio Melottid5a23e32009-07-15 17:07:04 +0000393 def test_extract_all(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000394 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
395 for fpath, fdata in SMALL_TEST_DATA:
396 zipfp.writestr(fpath, fdata)
Georg Brandl62416bc2008-01-07 18:47:44 +0000397
Ezio Melotti569e61f2009-12-30 06:14:51 +0000398 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
399 zipfp.extractall()
400 for fpath, fdata in SMALL_TEST_DATA:
401 if os.path.isabs(fpath):
402 outfile = os.path.join(os.getcwd(), fpath[1:])
403 else:
404 outfile = os.path.join(os.getcwd(), fpath)
Georg Brandl62416bc2008-01-07 18:47:44 +0000405
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000406 self.assertEqual(fdata, open(outfile, "rb").read())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000407 os.remove(outfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000408
409 # remove the test file subdirectories
410 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
411
Ronald Oussorendd25e862010-02-07 20:18:02 +0000412 def test_writestr_compression(self):
413 zipfp = zipfile.ZipFile(TESTFN2, "w")
414 zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED)
415 if zlib:
416 zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED)
417
418 info = zipfp.getinfo('a.txt')
419 self.assertEqual(info.compress_type, zipfile.ZIP_STORED)
420
421 if zlib:
422 info = zipfp.getinfo('b.txt')
423 self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED)
424
425
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000426 def zip_test_writestr_permissions(self, f, compression):
427 # Make sure that writestr creates files with mode 0600,
428 # when it is passed a name rather than a ZipInfo instance.
429
Ezio Melottid5a23e32009-07-15 17:07:04 +0000430 self.make_test_archive(f, compression)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000431 with zipfile.ZipFile(f, "r") as zipfp:
432 zinfo = zipfp.getinfo('strfile')
433 self.assertEqual(zinfo.external_attr, 0600 << 16)
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000434
Ezio Melottid5a23e32009-07-15 17:07:04 +0000435 def test_writestr_permissions(self):
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000436 for f in (TESTFN2, TemporaryFile(), StringIO()):
437 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
438
Ezio Melotti569e61f2009-12-30 06:14:51 +0000439 def test_close(self):
440 """Check that the zipfile is closed after the 'with' block."""
441 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
442 for fpath, fdata in SMALL_TEST_DATA:
443 zipfp.writestr(fpath, fdata)
444 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
445 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
446
447 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
448 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
449 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
450
451 def test_close_on_exception(self):
452 """Check that the zipfile is closed if an exception is raised in the
453 'with' block."""
454 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
455 for fpath, fdata in SMALL_TEST_DATA:
456 zipfp.writestr(fpath, fdata)
457
458 try:
459 with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
460 raise zipfile.BadZipfile()
461 except zipfile.BadZipfile:
462 self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
463
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000464 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000465 unlink(TESTFN)
466 unlink(TESTFN2)
467
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000468
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000469class TestZip64InSmallFiles(unittest.TestCase):
470 # These tests test the ZIP64 functionality without using large files,
471 # see test_zipfile64 for proper tests.
472
473 def setUp(self):
474 self._limit = zipfile.ZIP64_LIMIT
475 zipfile.ZIP64_LIMIT = 5
476
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000477 line_gen = ("Test of zipfile line %d." % i
478 for i in range(0, FIXEDTEST_SIZE))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000479 self.data = '\n'.join(line_gen)
480
481 # Make a source file with some lines
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000482 with open(TESTFN, "wb") as fp:
483 fp.write(self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000484
Ezio Melottid5a23e32009-07-15 17:07:04 +0000485 def large_file_exception_test(self, f, compression):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000486 with zipfile.ZipFile(f, "w", compression) as zipfp:
487 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000488 zipfp.write, TESTFN, "another.name")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000489
Ezio Melottid5a23e32009-07-15 17:07:04 +0000490 def large_file_exception_test2(self, f, compression):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000491 with zipfile.ZipFile(f, "w", compression) as zipfp:
492 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000493 zipfp.writestr, "another.name", self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000494
Ezio Melottid5a23e32009-07-15 17:07:04 +0000495 def test_large_file_exception(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000496 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000497 self.large_file_exception_test(f, zipfile.ZIP_STORED)
498 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000499
Ezio Melottid5a23e32009-07-15 17:07:04 +0000500 def zip_test(self, f, compression):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000501 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000502 with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000503 zipfp.write(TESTFN, "another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +0000504 zipfp.write(TESTFN, TESTFN)
505 zipfp.writestr("strfile", self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000506
507 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000508 with zipfile.ZipFile(f, "r", compression) as zipfp:
509 self.assertEqual(zipfp.read(TESTFN), self.data)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000510 self.assertEqual(zipfp.read("another.name"), self.data)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000511 self.assertEqual(zipfp.read("strfile"), self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000512
Ezio Melotti569e61f2009-12-30 06:14:51 +0000513 # Print the ZIP directory
514 fp = StringIO()
515 stdout = sys.stdout
516 try:
517 sys.stdout = fp
518 zipfp.printdir()
519 finally:
520 sys.stdout = stdout
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000521
Ezio Melotti569e61f2009-12-30 06:14:51 +0000522 directory = fp.getvalue()
523 lines = directory.splitlines()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000524 self.assertEqual(len(lines), 4) # Number of files + header
Tim Petersa608bb22006-06-15 18:06:29 +0000525
Ezio Melottiaa980582010-01-23 23:04:36 +0000526 self.assertIn('File Name', lines[0])
527 self.assertIn('Modified', lines[0])
528 self.assertIn('Size', lines[0])
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000529
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000530 fn, date, time_, size = lines[1].split()
531 self.assertEqual(fn, 'another.name')
532 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
533 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
534 self.assertEqual(size, str(len(self.data)))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000535
Ezio Melotti569e61f2009-12-30 06:14:51 +0000536 # Check the namelist
537 names = zipfp.namelist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000538 self.assertEqual(len(names), 3)
Ezio Melottiaa980582010-01-23 23:04:36 +0000539 self.assertIn(TESTFN, names)
540 self.assertIn("another.name", names)
541 self.assertIn("strfile", names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000542
Ezio Melotti569e61f2009-12-30 06:14:51 +0000543 # Check infolist
544 infos = zipfp.infolist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000545 names = [i.filename for i in infos]
546 self.assertEqual(len(names), 3)
Ezio Melottiaa980582010-01-23 23:04:36 +0000547 self.assertIn(TESTFN, names)
548 self.assertIn("another.name", names)
549 self.assertIn("strfile", names)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000550 for i in infos:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000551 self.assertEqual(i.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000552
Ezio Melotti569e61f2009-12-30 06:14:51 +0000553 # check getinfo
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000554 for nm in (TESTFN, "another.name", "strfile"):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000555 info = zipfp.getinfo(nm)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000556 self.assertEqual(info.filename, nm)
557 self.assertEqual(info.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000558
Ezio Melotti569e61f2009-12-30 06:14:51 +0000559 # Check that testzip doesn't raise an exception
560 zipfp.testzip()
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000561
Ezio Melottid5a23e32009-07-15 17:07:04 +0000562 def test_stored(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000563 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000564 self.zip_test(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000565
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000566 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000567 def test_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000568 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000569 self.zip_test(f, zipfile.ZIP_DEFLATED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000570
Ezio Melottid5a23e32009-07-15 17:07:04 +0000571 def test_absolute_arcnames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000572 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
573 allowZip64=True) as zipfp:
574 zipfp.write(TESTFN, "/absolute")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000575
Ezio Melotti569e61f2009-12-30 06:14:51 +0000576 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
577 self.assertEqual(zipfp.namelist(), ["absolute"])
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000578
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000579 def tearDown(self):
580 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000581 unlink(TESTFN)
582 unlink(TESTFN2)
583
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000584
585class PyZipFileTests(unittest.TestCase):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000586 def test_write_pyfile(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000587 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
588 fn = __file__
589 if fn.endswith('.pyc') or fn.endswith('.pyo'):
590 fn = fn[:-1]
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000591
Ezio Melotti569e61f2009-12-30 06:14:51 +0000592 zipfp.writepy(fn)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000593
Ezio Melotti569e61f2009-12-30 06:14:51 +0000594 bn = os.path.basename(fn)
Ezio Melottiaa980582010-01-23 23:04:36 +0000595 self.assertNotIn(bn, zipfp.namelist())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000596 self.assertTrue(bn + 'o' in zipfp.namelist() or
597 bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000598
Ezio Melotti569e61f2009-12-30 06:14:51 +0000599 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
600 fn = __file__
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000601 if fn.endswith(('.pyc', '.pyo')):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000602 fn = fn[:-1]
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000603
Ezio Melotti569e61f2009-12-30 06:14:51 +0000604 zipfp.writepy(fn, "testpackage")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000605
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000606 bn = "%s/%s" % ("testpackage", os.path.basename(fn))
Ezio Melottiaa980582010-01-23 23:04:36 +0000607 self.assertNotIn(bn, zipfp.namelist())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000608 self.assertTrue(bn + 'o' in zipfp.namelist() or
609 bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000610
Ezio Melottid5a23e32009-07-15 17:07:04 +0000611 def test_write_python_package(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000612 import email
613 packagedir = os.path.dirname(email.__file__)
614
Ezio Melotti569e61f2009-12-30 06:14:51 +0000615 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
616 zipfp.writepy(packagedir)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000617
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000618 # Check for a couple of modules at different levels of the
619 # hierarchy
Ezio Melotti569e61f2009-12-30 06:14:51 +0000620 names = zipfp.namelist()
621 self.assertTrue('email/__init__.pyo' in names or
622 'email/__init__.pyc' in names)
623 self.assertTrue('email/mime/text.pyo' in names or
624 'email/mime/text.pyc' in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000625
Ezio Melottid5a23e32009-07-15 17:07:04 +0000626 def test_write_python_directory(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000627 os.mkdir(TESTFN2)
628 try:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000629 with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
Ezio Melotti763f1e82009-12-31 13:27:41 +0000630 fp.write("print(42)\n")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000631
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000632 with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
Ezio Melotti763f1e82009-12-31 13:27:41 +0000633 fp.write("print(42 * 42)\n")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000634
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000635 with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp:
636 fp.write("bla bla bla\n")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000637
638 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
639 zipfp.writepy(TESTFN2)
640
641 names = zipfp.namelist()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000642 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
643 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
Ezio Melottiaa980582010-01-23 23:04:36 +0000644 self.assertNotIn('mod2.txt', names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000645
646 finally:
647 shutil.rmtree(TESTFN2)
648
Ezio Melottid5a23e32009-07-15 17:07:04 +0000649 def test_write_non_pyfile(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000650 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000651 open(TESTFN, 'w').write('most definitely not a python file')
Ezio Melotti569e61f2009-12-30 06:14:51 +0000652 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
653 os.remove(TESTFN)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000654
655
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000656class OtherTests(unittest.TestCase):
Antoine Pitroue1436d12010-08-12 15:25:51 +0000657 zips_with_bad_crc = {
658 zipfile.ZIP_STORED: (
659 b'PK\003\004\024\0\0\0\0\0 \213\212;:r'
660 b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af'
661 b'ilehello,AworldP'
662 b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:'
663 b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0'
664 b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi'
665 b'lePK\005\006\0\0\0\0\001\0\001\0003\000'
666 b'\0\0/\0\0\0\0\0'),
667 zipfile.ZIP_DEFLATED: (
668 b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA'
669 b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
670 b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0'
671 b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n'
672 b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05'
673 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00'
674 b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00'
675 b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'),
676 }
677
Ezio Melottid5a23e32009-07-15 17:07:04 +0000678 def test_unicode_filenames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000679 with zipfile.ZipFile(TESTFN, "w") as zf:
680 zf.writestr(u"foo.txt", "Test for unicode filename")
681 zf.writestr(u"\xf6.txt", "Test for unicode filename")
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000682 self.assertIsInstance(zf.infolist()[0].filename, unicode)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000683
684 with zipfile.ZipFile(TESTFN, "r") as zf:
685 self.assertEqual(zf.filelist[0].filename, "foo.txt")
686 self.assertEqual(zf.filelist[1].filename, u"\xf6.txt")
Martin v. Löwis471617d2008-05-05 17:16:58 +0000687
Ezio Melottid5a23e32009-07-15 17:07:04 +0000688 def test_create_non_existent_file_for_append(self):
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000689 if os.path.exists(TESTFN):
690 os.unlink(TESTFN)
Tim Petersea5962f2007-03-12 18:07:52 +0000691
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000692 filename = 'testfile.txt'
693 content = 'hello, world. this is some content.'
Tim Petersea5962f2007-03-12 18:07:52 +0000694
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000695 try:
Ezio Melotti569e61f2009-12-30 06:14:51 +0000696 with zipfile.ZipFile(TESTFN, 'a') as zf:
697 zf.writestr(filename, content)
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000698 except IOError:
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000699 self.fail('Could not append data to a non-existent zip file.')
700
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000701 self.assertTrue(os.path.exists(TESTFN))
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000702
Ezio Melotti569e61f2009-12-30 06:14:51 +0000703 with zipfile.ZipFile(TESTFN, 'r') as zf:
704 self.assertEqual(zf.read(filename), content)
Tim Petersea5962f2007-03-12 18:07:52 +0000705
Ezio Melottid5a23e32009-07-15 17:07:04 +0000706 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000707 # This test checks that the ZipFile constructor closes the file object
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000708 # it opens if there's an error in the file. If it doesn't, the
709 # traceback holds a reference to the ZipFile object and, indirectly,
710 # the file object.
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000711 # On Windows, this causes the os.unlink() call to fail because the
712 # underlying file is still open. This is SF bug #412214.
713 #
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000714 with open(TESTFN, "w") as fp:
715 fp.write("this is not a legal zip file\n")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000716 try:
717 zf = zipfile.ZipFile(TESTFN)
718 except zipfile.BadZipfile:
Collin Winter04a51ec2007-03-29 02:28:16 +0000719 pass
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000720
Ezio Melottid5a23e32009-07-15 17:07:04 +0000721 def test_is_zip_erroneous_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000722 """Check that is_zipfile() correctly identifies non-zip files."""
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000723 # - passing a filename
724 with open(TESTFN, "w") as fp:
725 fp.write("this is not a legal zip file\n")
Tim Petersea5962f2007-03-12 18:07:52 +0000726 chk = zipfile.is_zipfile(TESTFN)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000727 self.assertFalse(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000728 # - passing a file object
729 with open(TESTFN, "rb") as fp:
730 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000731 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000732 # - passing a file-like object
733 fp = StringIO()
734 fp.write("this is not a legal zip file\n")
735 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000736 self.assertTrue(not chk)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000737 fp.seek(0, 0)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000738 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000739 self.assertTrue(not chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000740
Ezio Melottid5a23e32009-07-15 17:07:04 +0000741 def test_is_zip_valid_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000742 """Check that is_zipfile() correctly identifies zip files."""
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000743 # - passing a filename
Ezio Melotti569e61f2009-12-30 06:14:51 +0000744 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
745 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Tim Petersea5962f2007-03-12 18:07:52 +0000746 chk = zipfile.is_zipfile(TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000747 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000748 # - passing a file object
749 with open(TESTFN, "rb") as fp:
750 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000751 self.assertTrue(chk)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000752 fp.seek(0, 0)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000753 zip_contents = fp.read()
754 # - passing a file-like object
755 fp = StringIO()
756 fp.write(zip_contents)
757 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000758 self.assertTrue(chk)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000759 fp.seek(0, 0)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000760 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000761 self.assertTrue(chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000762
Ezio Melottid5a23e32009-07-15 17:07:04 +0000763 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000764 # make sure we don't raise an AttributeError when a partially-constructed
765 # ZipFile instance is finalized; this tests for regression on SF tracker
766 # bug #403871.
767
768 # The bug we're testing for caused an AttributeError to be raised
769 # when a ZipFile instance was created for a file that did not
770 # exist; the .fp member was not initialized but was needed by the
771 # __del__() method. Since the AttributeError is in the __del__(),
772 # it is ignored, but the user should be sufficiently annoyed by
773 # the message on the output that regression will be noticed
774 # quickly.
775 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
776
Amaury Forgeot d'Arc3e5b0272009-07-28 22:15:30 +0000777 def test_empty_file_raises_BadZipFile(self):
778 f = open(TESTFN, 'w')
779 f.close()
780 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
781
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000782 with open(TESTFN, 'w') as fp:
783 fp.write("short file")
Amaury Forgeot d'Arc3e5b0272009-07-28 22:15:30 +0000784 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
785
Ezio Melottid5a23e32009-07-15 17:07:04 +0000786 def test_closed_zip_raises_RuntimeError(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000787 """Verify that testzip() doesn't swallow inappropriate exceptions."""
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000788 data = StringIO()
Ezio Melotti569e61f2009-12-30 06:14:51 +0000789 with zipfile.ZipFile(data, mode="w") as zipf:
790 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000791
792 # This is correct; calling .read on a closed ZipFile should throw
793 # a RuntimeError, and so should calling .testzip. An earlier
794 # version of .testzip would swallow this exception (and any other)
795 # and report that the first file in the archive was corrupt.
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000796 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
797 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000798 self.assertRaises(RuntimeError, zipf.testzip)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000799 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000800 open(TESTFN, 'w').write('zipfile test data')
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000801 self.assertRaises(RuntimeError, zipf.write, TESTFN)
802
Ezio Melottid5a23e32009-07-15 17:07:04 +0000803 def test_bad_constructor_mode(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000804 """Check that bad modes passed to ZipFile constructor are caught."""
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000805 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
806
Ezio Melottid5a23e32009-07-15 17:07:04 +0000807 def test_bad_open_mode(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000808 """Check that bad modes passed to ZipFile.open are caught."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000809 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
810 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
811
812 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000813 # read the data to make sure the file is there
Ezio Melotti569e61f2009-12-30 06:14:51 +0000814 zipf.read("foo.txt")
815 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000816
Ezio Melottid5a23e32009-07-15 17:07:04 +0000817 def test_read0(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000818 """Check that calling read(0) on a ZipExtFile object returns an empty
819 string and doesn't advance file pointer."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000820 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
821 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
822 # read the data to make sure the file is there
823 f = zipf.open("foo.txt")
824 for i in xrange(FIXEDTEST_SIZE):
825 self.assertEqual(f.read(0), '')
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000826
Ezio Melotti569e61f2009-12-30 06:14:51 +0000827 self.assertEqual(f.read(), "O, for a Muse of Fire!")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000828
Ezio Melottid5a23e32009-07-15 17:07:04 +0000829 def test_open_non_existent_item(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000830 """Check that attempting to call open() for an item that doesn't
831 exist in the archive raises a RuntimeError."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000832 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
833 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000834
Ezio Melottid5a23e32009-07-15 17:07:04 +0000835 def test_bad_compression_mode(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000836 """Check that bad compression methods passed to ZipFile.open are
837 caught."""
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000838 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
839
Ezio Melottid5a23e32009-07-15 17:07:04 +0000840 def test_null_byte_in_filename(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000841 """Check that a filename containing a null byte is properly
842 terminated."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000843 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
844 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
845 self.assertEqual(zipf.namelist(), ['foo.txt'])
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000846
Ezio Melottid5a23e32009-07-15 17:07:04 +0000847 def test_struct_sizes(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000848 """Check that ZIP internal structure sizes are calculated correctly."""
Martin v. Löwis8c436412008-07-03 12:51:14 +0000849 self.assertEqual(zipfile.sizeEndCentDir, 22)
850 self.assertEqual(zipfile.sizeCentralDir, 46)
851 self.assertEqual(zipfile.sizeEndCentDir64, 56)
852 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
853
Ezio Melottid5a23e32009-07-15 17:07:04 +0000854 def test_comments(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000855 """Check that comments on the archive are handled properly."""
Martin v. Löwis8c436412008-07-03 12:51:14 +0000856
857 # check default comment is empty
Ezio Melotti569e61f2009-12-30 06:14:51 +0000858 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
859 self.assertEqual(zipf.comment, '')
860 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
861
862 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
863 self.assertEqual(zipf.comment, '')
Martin v. Löwis8c436412008-07-03 12:51:14 +0000864
865 # check a simple short comment
866 comment = 'Bravely taking to his feet, he beat a very brave retreat.'
Ezio Melotti569e61f2009-12-30 06:14:51 +0000867 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
868 zipf.comment = comment
869 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
870 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
871 self.assertEqual(zipf.comment, comment)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000872
873 # check a comment of max length
874 comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)])
Ezio Melotti569e61f2009-12-30 06:14:51 +0000875 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
876 zipf.comment = comment2
877 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
878
879 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
880 self.assertEqual(zipf.comment, comment2)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000881
882 # check a comment that is too long is truncated
Ezio Melotti569e61f2009-12-30 06:14:51 +0000883 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
884 zipf.comment = comment2 + 'oops'
885 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
886 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
887 self.assertEqual(zipf.comment, comment2)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000888
Antoine Pitroue1436d12010-08-12 15:25:51 +0000889 def check_testzip_with_bad_crc(self, compression):
890 """Tests that files with bad CRCs return their name from testzip."""
891 zipdata = self.zips_with_bad_crc[compression]
892
893 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
894 # testzip returns the name of the first corrupt file, or None
895 self.assertEqual('afile', zipf.testzip())
896
897 def test_testzip_with_bad_crc_stored(self):
898 self.check_testzip_with_bad_crc(zipfile.ZIP_STORED)
899
900 @skipUnless(zlib, "requires zlib")
901 def test_testzip_with_bad_crc_deflated(self):
902 self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED)
903
904 def check_read_with_bad_crc(self, compression):
905 """Tests that files with bad CRCs raise a BadZipfile exception when read."""
906 zipdata = self.zips_with_bad_crc[compression]
907
908 # Using ZipFile.read()
909 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
910 self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile')
911
912 # Using ZipExtFile.read()
913 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
914 with zipf.open('afile', 'r') as corrupt_file:
915 self.assertRaises(zipfile.BadZipfile, corrupt_file.read)
916
917 # Same with small reads (in order to exercise the buffering logic)
918 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
919 with zipf.open('afile', 'r') as corrupt_file:
920 corrupt_file.MIN_READ_SIZE = 2
921 with self.assertRaises(zipfile.BadZipfile):
922 while corrupt_file.read(2):
923 pass
924
925 def test_read_with_bad_crc_stored(self):
926 self.check_read_with_bad_crc(zipfile.ZIP_STORED)
927
928 @skipUnless(zlib, "requires zlib")
929 def test_read_with_bad_crc_deflated(self):
930 self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED)
931
Antoine Pitroue4195e82010-09-12 14:56:27 +0000932 def check_read_return_size(self, compression):
933 # Issue #9837: ZipExtFile.read() shouldn't return more bytes
934 # than requested.
935 for test_size in (1, 4095, 4096, 4097, 16384):
936 file_size = test_size + 1
937 junk = b''.join(struct.pack('B', randint(0, 255))
938 for x in range(file_size))
939 with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf:
940 zipf.writestr('foo', junk)
941 with zipf.open('foo', 'r') as fp:
942 buf = fp.read(test_size)
943 self.assertEqual(len(buf), test_size)
944
945 def test_read_return_size_stored(self):
946 self.check_read_return_size(zipfile.ZIP_STORED)
947
948 @skipUnless(zlib, "requires zlib")
949 def test_read_return_size_deflated(self):
950 self.check_read_return_size(zipfile.ZIP_DEFLATED)
951
Georg Brandl86e0c892010-11-26 07:22:28 +0000952 def test_empty_zipfile(self):
953 # Check that creating a file in 'w' or 'a' mode and closing without
954 # adding any files to the archives creates a valid empty ZIP file
955 zipf = zipfile.ZipFile(TESTFN, mode="w")
956 zipf.close()
957 try:
958 zipf = zipfile.ZipFile(TESTFN, mode="r")
959 except zipfile.BadZipFile:
960 self.fail("Unable to create empty ZIP file in 'w' mode")
961
962 zipf = zipfile.ZipFile(TESTFN, mode="a")
963 zipf.close()
964 try:
965 zipf = zipfile.ZipFile(TESTFN, mode="r")
966 except:
967 self.fail("Unable to create empty ZIP file in 'a' mode")
968
969 def test_open_empty_file(self):
970 # Issue 1710703: Check that opening a file with less than 22 bytes
971 # raises a BadZipfile exception (rather than the previously unhelpful
972 # IOError)
973 f = open(TESTFN, 'w')
974 f.close()
975 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r')
976
Collin Winter04a51ec2007-03-29 02:28:16 +0000977 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000978 unlink(TESTFN)
979 unlink(TESTFN2)
980
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000981
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000982class DecryptionTests(unittest.TestCase):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000983 """Check that ZIP decryption works. Since the library does not
984 support encryption at the moment, we use a pre-generated encrypted
985 ZIP file."""
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000986
987 data = (
988 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
989 '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
990 '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
991 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
992 '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
993 '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
994 '\x00\x00L\x00\x00\x00\x00\x00' )
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000995 data2 = (
996 'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
997 '\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
998 '\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
999 'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
1000 '\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
1001 '\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
1002 'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
1003 '\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001004
1005 plain = 'zipfile.py encryption test'
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001006 plain2 = '\x00'*512
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001007
1008 def setUp(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001009 with open(TESTFN, "wb") as fp:
1010 fp.write(self.data)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001011 self.zip = zipfile.ZipFile(TESTFN, "r")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001012 with open(TESTFN2, "wb") as fp:
1013 fp.write(self.data2)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001014 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001015
1016 def tearDown(self):
1017 self.zip.close()
1018 os.unlink(TESTFN)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001019 self.zip2.close()
1020 os.unlink(TESTFN2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001021
Ezio Melottid5a23e32009-07-15 17:07:04 +00001022 def test_no_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001023 # Reading the encrypted file without password
1024 # must generate a RunTime exception
1025 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001026 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001027
Ezio Melottid5a23e32009-07-15 17:07:04 +00001028 def test_bad_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001029 self.zip.setpassword("perl")
1030 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001031 self.zip2.setpassword("perl")
1032 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Tim Petersea5962f2007-03-12 18:07:52 +00001033
Ezio Melotti1036a7f2009-09-12 14:43:43 +00001034 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001035 def test_good_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001036 self.zip.setpassword("python")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001037 self.assertEqual(self.zip.read("test.txt"), self.plain)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001038 self.zip2.setpassword("12345")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001039 self.assertEqual(self.zip2.read("zero"), self.plain2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001040
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001041
1042class TestsWithRandomBinaryFiles(unittest.TestCase):
1043 def setUp(self):
1044 datacount = randint(16, 64)*1024 + randint(1, 1024)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001045 self.data = ''.join(struct.pack('<f', random()*randint(-1000, 1000))
Ezio Melotti763f1e82009-12-31 13:27:41 +00001046 for i in xrange(datacount))
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001047
1048 # Make a source file with some lines
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001049 with open(TESTFN, "wb") as fp:
1050 fp.write(self.data)
Neal Norwitz0d4c06e2007-04-25 06:30:05 +00001051
Collin Winter04a51ec2007-03-29 02:28:16 +00001052 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001053 unlink(TESTFN)
1054 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001055
Ezio Melottid5a23e32009-07-15 17:07:04 +00001056 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001057 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001058 with zipfile.ZipFile(f, "w", compression) as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001059 zipfp.write(TESTFN, "another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +00001060 zipfp.write(TESTFN, TESTFN)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001061
Ezio Melottid5a23e32009-07-15 17:07:04 +00001062 def zip_test(self, f, compression):
1063 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001064
1065 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001066 with zipfile.ZipFile(f, "r", compression) as zipfp:
1067 testdata = zipfp.read(TESTFN)
1068 self.assertEqual(len(testdata), len(self.data))
1069 self.assertEqual(testdata, self.data)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001070 self.assertEqual(zipfp.read("another.name"), self.data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001071
Ezio Melottid5a23e32009-07-15 17:07:04 +00001072 def test_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001073 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001074 self.zip_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001075
Antoine Pitroue1436d12010-08-12 15:25:51 +00001076 @skipUnless(zlib, "requires zlib")
1077 def test_deflated(self):
1078 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1079 self.zip_test(f, zipfile.ZIP_DEFLATED)
1080
Ezio Melottid5a23e32009-07-15 17:07:04 +00001081 def zip_open_test(self, f, compression):
1082 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001083
1084 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001085 with zipfile.ZipFile(f, "r", compression) as zipfp:
1086 zipdata1 = []
1087 zipopen1 = zipfp.open(TESTFN)
1088 while True:
1089 read_data = zipopen1.read(256)
1090 if not read_data:
1091 break
1092 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001093
Ezio Melotti569e61f2009-12-30 06:14:51 +00001094 zipdata2 = []
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001095 zipopen2 = zipfp.open("another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +00001096 while True:
1097 read_data = zipopen2.read(256)
1098 if not read_data:
1099 break
1100 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +00001101
Ezio Melotti569e61f2009-12-30 06:14:51 +00001102 testdata1 = ''.join(zipdata1)
1103 self.assertEqual(len(testdata1), len(self.data))
1104 self.assertEqual(testdata1, self.data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001105
Ezio Melotti569e61f2009-12-30 06:14:51 +00001106 testdata2 = ''.join(zipdata2)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001107 self.assertEqual(len(testdata2), len(self.data))
1108 self.assertEqual(testdata2, self.data)
Tim Petersea5962f2007-03-12 18:07:52 +00001109
Ezio Melottid5a23e32009-07-15 17:07:04 +00001110 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001111 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001112 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001113
Antoine Pitroue1436d12010-08-12 15:25:51 +00001114 @skipUnless(zlib, "requires zlib")
1115 def test_open_deflated(self):
1116 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1117 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
1118
Ezio Melottid5a23e32009-07-15 17:07:04 +00001119 def zip_random_open_test(self, f, compression):
1120 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001121
1122 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001123 with zipfile.ZipFile(f, "r", compression) as zipfp:
1124 zipdata1 = []
1125 zipopen1 = zipfp.open(TESTFN)
1126 while True:
1127 read_data = zipopen1.read(randint(1, 1024))
1128 if not read_data:
1129 break
1130 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001131
Ezio Melotti569e61f2009-12-30 06:14:51 +00001132 testdata = ''.join(zipdata1)
1133 self.assertEqual(len(testdata), len(self.data))
1134 self.assertEqual(testdata, self.data)
Tim Petersea5962f2007-03-12 18:07:52 +00001135
Ezio Melottid5a23e32009-07-15 17:07:04 +00001136 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001137 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001138 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001139
Antoine Pitroue1436d12010-08-12 15:25:51 +00001140 @skipUnless(zlib, "requires zlib")
1141 def test_random_open_deflated(self):
1142 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1143 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
1144
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001145
Ezio Melotti1036a7f2009-09-12 14:43:43 +00001146@skipUnless(zlib, "requires zlib")
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001147class TestsWithMultipleOpens(unittest.TestCase):
1148 def setUp(self):
1149 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001150 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
1151 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
1152 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
Tim Petersea5962f2007-03-12 18:07:52 +00001153
Ezio Melottid5a23e32009-07-15 17:07:04 +00001154 def test_same_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001155 # Verify that (when the ZipFile is in control of creating file objects)
1156 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +00001157 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1158 zopen1 = zipf.open('ones')
1159 zopen2 = zipf.open('ones')
1160 data1 = zopen1.read(500)
1161 data2 = zopen2.read(500)
1162 data1 += zopen1.read(500)
1163 data2 += zopen2.read(500)
1164 self.assertEqual(data1, data2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001165
Ezio Melottid5a23e32009-07-15 17:07:04 +00001166 def test_different_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001167 # Verify that (when the ZipFile is in control of creating file objects)
1168 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +00001169 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1170 zopen1 = zipf.open('ones')
1171 zopen2 = zipf.open('twos')
1172 data1 = zopen1.read(500)
1173 data2 = zopen2.read(500)
1174 data1 += zopen1.read(500)
1175 data2 += zopen2.read(500)
1176 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1177 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001178
Ezio Melottid5a23e32009-07-15 17:07:04 +00001179 def test_interleaved(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001180 # Verify that (when the ZipFile is in control of creating file objects)
1181 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +00001182 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1183 zopen1 = zipf.open('ones')
1184 data1 = zopen1.read(500)
1185 zopen2 = zipf.open('twos')
1186 data2 = zopen2.read(500)
1187 data1 += zopen1.read(500)
1188 data2 += zopen2.read(500)
1189 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1190 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
Tim Petersea5962f2007-03-12 18:07:52 +00001191
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001192 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001193 unlink(TESTFN2)
1194
Tim Petersea5962f2007-03-12 18:07:52 +00001195
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001196class TestWithDirectory(unittest.TestCase):
1197 def setUp(self):
1198 os.mkdir(TESTFN2)
1199
Ezio Melottid5a23e32009-07-15 17:07:04 +00001200 def test_extract_dir(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +00001201 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
1202 zipf.extractall(TESTFN2)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001203 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1204 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1205 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1206
Ezio Melottid5a23e32009-07-15 17:07:04 +00001207 def test_bug_6050(self):
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001208 # Extraction should succeed if directories already exist
1209 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottid5a23e32009-07-15 17:07:04 +00001210 self.test_extract_dir()
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001211
Ezio Melottid5a23e32009-07-15 17:07:04 +00001212 def test_store_dir(self):
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001213 os.mkdir(os.path.join(TESTFN2, "x"))
1214 zipf = zipfile.ZipFile(TESTFN, "w")
1215 zipf.write(os.path.join(TESTFN2, "x"), "x")
1216 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1217
1218 def tearDown(self):
1219 shutil.rmtree(TESTFN2)
1220 if os.path.exists(TESTFN):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001221 unlink(TESTFN)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001222
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001223
1224class UniversalNewlineTests(unittest.TestCase):
1225 def setUp(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001226 self.line_gen = ["Test of zipfile line %d." % i
1227 for i in xrange(FIXEDTEST_SIZE)]
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001228 self.seps = ('\r', '\r\n', '\n')
1229 self.arcdata, self.arcfiles = {}, {}
1230 for n, s in enumerate(self.seps):
1231 self.arcdata[s] = s.join(self.line_gen) + s
1232 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Brett Cannon6cef0762007-05-25 20:17:15 +00001233 open(self.arcfiles[s], "wb").write(self.arcdata[s])
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001234
Ezio Melottid5a23e32009-07-15 17:07:04 +00001235 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001236 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001237 with zipfile.ZipFile(f, "w", compression) as zipfp:
1238 for fn in self.arcfiles.values():
1239 zipfp.write(fn, fn)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001240
Ezio Melottid5a23e32009-07-15 17:07:04 +00001241 def read_test(self, f, compression):
1242 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001243
1244 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001245 with zipfile.ZipFile(f, "r") as zipfp:
1246 for sep, fn in self.arcfiles.items():
1247 zipdata = zipfp.open(fn, "rU").read()
1248 self.assertEqual(self.arcdata[sep], zipdata)
Tim Petersea5962f2007-03-12 18:07:52 +00001249
Antoine Pitrou94c33eb2010-01-27 20:59:50 +00001250 def readline_read_test(self, f, compression):
1251 self.make_test_archive(f, compression)
1252
1253 # Read the ZIP archive
1254 zipfp = zipfile.ZipFile(f, "r")
1255 for sep, fn in self.arcfiles.items():
1256 zipopen = zipfp.open(fn, "rU")
1257 data = ''
1258 while True:
1259 read = zipopen.readline()
1260 if not read:
1261 break
1262 data += read
1263
1264 read = zipopen.read(5)
1265 if not read:
1266 break
1267 data += read
1268
1269 self.assertEqual(data, self.arcdata['\n'])
1270
1271 zipfp.close()
1272
Ezio Melottid5a23e32009-07-15 17:07:04 +00001273 def readline_test(self, f, compression):
1274 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001275
1276 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001277 with zipfile.ZipFile(f, "r") as zipfp:
1278 for sep, fn in self.arcfiles.items():
1279 zipopen = zipfp.open(fn, "rU")
1280 for line in self.line_gen:
1281 linedata = zipopen.readline()
1282 self.assertEqual(linedata, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001283
Ezio Melottid5a23e32009-07-15 17:07:04 +00001284 def readlines_test(self, f, compression):
1285 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001286
1287 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001288 with zipfile.ZipFile(f, "r") as zipfp:
1289 for sep, fn in self.arcfiles.items():
1290 ziplines = zipfp.open(fn, "rU").readlines()
1291 for line, zipline in zip(self.line_gen, ziplines):
1292 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001293
Ezio Melottid5a23e32009-07-15 17:07:04 +00001294 def iterlines_test(self, f, compression):
1295 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001296
1297 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001298 with zipfile.ZipFile(f, "r") as zipfp:
1299 for sep, fn in self.arcfiles.items():
1300 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
1301 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001302
Ezio Melottid5a23e32009-07-15 17:07:04 +00001303 def test_read_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001304 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001305 self.read_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001306
Antoine Pitrou94c33eb2010-01-27 20:59:50 +00001307 def test_readline_read_stored(self):
1308 # Issue #7610: calls to readline() interleaved with calls to read().
1309 for f in (TESTFN2, TemporaryFile(), StringIO()):
1310 self.readline_read_test(f, zipfile.ZIP_STORED)
1311
Ezio Melottid5a23e32009-07-15 17:07:04 +00001312 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001313 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001314 self.readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001315
Ezio Melottid5a23e32009-07-15 17:07:04 +00001316 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001317 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001318 self.readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001319
Ezio Melottid5a23e32009-07-15 17:07:04 +00001320 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001321 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001322 self.iterlines_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001323
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001324 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001325 def test_read_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001326 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001327 self.read_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001328
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001329 @skipUnless(zlib, "requires zlib")
Antoine Pitrou94c33eb2010-01-27 20:59:50 +00001330 def test_readline_read_deflated(self):
1331 # Issue #7610: calls to readline() interleaved with calls to read().
1332 for f in (TESTFN2, TemporaryFile(), StringIO()):
1333 self.readline_read_test(f, zipfile.ZIP_DEFLATED)
1334
1335 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001336 def test_readline_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001337 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001338 self.readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001339
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001340 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001341 def test_readlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001342 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001343 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001344
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001345 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001346 def test_iterlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001347 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001348 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001349
1350 def tearDown(self):
1351 for sep, fn in self.arcfiles.items():
1352 os.remove(fn)
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001353 unlink(TESTFN)
1354 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001355
1356
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001357def test_main():
Tim Petersea5962f2007-03-12 18:07:52 +00001358 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1359 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001360 TestWithDirectory, UniversalNewlineTests,
1361 TestsWithRandomBinaryFiles)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001362
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001363if __name__ == "__main__":
1364 test_main()