blob: 3c3bae2c2b1df746a20236cfd27bfe1e47ecbaec [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
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00009import time
Ezio Melottie7a0cc22009-07-04 14:58:27 +000010import shutil
11import struct
12import zipfile
13import unittest
Tim Petersa19a1682001-03-29 04:36:09 +000014
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000015from StringIO import StringIO
16from tempfile import TemporaryFile
Martin v. Löwis3eb76482007-03-06 10:41:24 +000017from random import randint, random
Ezio Melottie7a0cc22009-07-04 14:58:27 +000018from unittest import skipUnless
Tim Petersa19a1682001-03-29 04:36:09 +000019
Ezio Melotti6cbfc122009-07-10 20:25:56 +000020from test.test_support import TESTFN, run_unittest, findfile, unlink
Guido van Rossum368f04a2000-04-10 13:23:04 +000021
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000022TESTFN2 = TESTFN + "2"
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +000023TESTFNDIR = TESTFN + "d"
Georg Brandl4b3ab6f2007-07-12 09:59:22 +000024FIXEDTEST_SIZE = 1000
Guido van Rossum368f04a2000-04-10 13:23:04 +000025
Georg Brandl62416bc2008-01-07 18:47:44 +000026SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
27 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
28 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
29 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
30
Ezio Melotti6cbfc122009-07-10 20:25:56 +000031
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000032class TestsWithSourceFile(unittest.TestCase):
33 def setUp(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +000034 self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random())
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000035 for i in xrange(FIXEDTEST_SIZE)]
Martin v. Löwis3eb76482007-03-06 10:41:24 +000036 self.data = '\n'.join(self.line_gen) + '\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000037
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000038 # Make a source file with some lines
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000039 with open(TESTFN, "wb") as fp:
40 fp.write(self.data)
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
Ezio Melotti569e61f2009-12-30 06:14:51 +000044 with zipfile.ZipFile(f, "w", compression) as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000045 zipfp.write(TESTFN, "another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +000046 zipfp.write(TESTFN, TESTFN)
47 zipfp.writestr("strfile", self.data)
Tim Peters7d3bad62001-04-04 18:56:49 +000048
Ezio Melottid5a23e32009-07-15 17:07:04 +000049 def zip_test(self, f, compression):
50 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +000051
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000052 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +000053 with zipfile.ZipFile(f, "r", compression) as zipfp:
54 self.assertEqual(zipfp.read(TESTFN), self.data)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000055 self.assertEqual(zipfp.read("another.name"), self.data)
Ezio Melotti569e61f2009-12-30 06:14:51 +000056 self.assertEqual(zipfp.read("strfile"), self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000057
Ezio Melotti569e61f2009-12-30 06:14:51 +000058 # Print the ZIP directory
59 fp = StringIO()
60 stdout = sys.stdout
61 try:
62 sys.stdout = fp
63 zipfp.printdir()
64 finally:
65 sys.stdout = stdout
Tim Petersa608bb22006-06-15 18:06:29 +000066
Ezio Melotti569e61f2009-12-30 06:14:51 +000067 directory = fp.getvalue()
68 lines = directory.splitlines()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000069 self.assertEqual(len(lines), 4) # Number of files + header
Ronald Oussoren143cefb2006-06-15 08:14:18 +000070
Ezio Melottiaa980582010-01-23 23:04:36 +000071 self.assertIn('File Name', lines[0])
72 self.assertIn('Modified', lines[0])
73 self.assertIn('Size', lines[0])
Ronald Oussoren143cefb2006-06-15 08:14:18 +000074
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000075 fn, date, time_, size = lines[1].split()
76 self.assertEqual(fn, 'another.name')
77 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
78 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
79 self.assertEqual(size, str(len(self.data)))
Ronald Oussoren143cefb2006-06-15 08:14:18 +000080
Ezio Melotti569e61f2009-12-30 06:14:51 +000081 # Check the namelist
82 names = zipfp.namelist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000083 self.assertEqual(len(names), 3)
Ezio Melottiaa980582010-01-23 23:04:36 +000084 self.assertIn(TESTFN, names)
85 self.assertIn("another.name", names)
86 self.assertIn("strfile", names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000087
Ezio Melotti569e61f2009-12-30 06:14:51 +000088 # Check infolist
89 infos = zipfp.infolist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000090 names = [i.filename for i in infos]
91 self.assertEqual(len(names), 3)
Ezio Melottiaa980582010-01-23 23:04:36 +000092 self.assertIn(TESTFN, names)
93 self.assertIn("another.name", names)
94 self.assertIn("strfile", names)
Ezio Melotti569e61f2009-12-30 06:14:51 +000095 for i in infos:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000096 self.assertEqual(i.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +000097
Ezio Melotti569e61f2009-12-30 06:14:51 +000098 # check getinfo
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000099 for nm in (TESTFN, "another.name", "strfile"):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000100 info = zipfp.getinfo(nm)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000101 self.assertEqual(info.filename, nm)
102 self.assertEqual(info.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000103
Ezio Melotti569e61f2009-12-30 06:14:51 +0000104 # Check that testzip doesn't raise an exception
105 zipfp.testzip()
Tim Peters7d3bad62001-04-04 18:56:49 +0000106
Ezio Melottid5a23e32009-07-15 17:07:04 +0000107 def test_stored(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000108 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000109 self.zip_test(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000110
Ezio Melottid5a23e32009-07-15 17:07:04 +0000111 def zip_open_test(self, f, compression):
112 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000113
114 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000115 with zipfile.ZipFile(f, "r", compression) as zipfp:
116 zipdata1 = []
117 zipopen1 = zipfp.open(TESTFN)
118 while True:
119 read_data = zipopen1.read(256)
120 if not read_data:
121 break
122 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000123
Ezio Melotti569e61f2009-12-30 06:14:51 +0000124 zipdata2 = []
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000125 zipopen2 = zipfp.open("another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +0000126 while True:
127 read_data = zipopen2.read(256)
128 if not read_data:
129 break
130 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +0000131
Ezio Melotti569e61f2009-12-30 06:14:51 +0000132 self.assertEqual(''.join(zipdata1), self.data)
133 self.assertEqual(''.join(zipdata2), self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000134
Ezio Melottid5a23e32009-07-15 17:07:04 +0000135 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000136 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000137 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000138
Ezio Melottid5a23e32009-07-15 17:07:04 +0000139 def test_open_via_zip_info(self):
Georg Brandl112aa502008-05-20 08:25:48 +0000140 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000141 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
142 zipfp.writestr("name", "foo")
143 zipfp.writestr("name", "bar")
Georg Brandl112aa502008-05-20 08:25:48 +0000144
Ezio Melotti569e61f2009-12-30 06:14:51 +0000145 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
146 infos = zipfp.infolist()
147 data = ""
148 for info in infos:
149 data += zipfp.open(info).read()
150 self.assertTrue(data == "foobar" or data == "barfoo")
151 data = ""
152 for info in infos:
153 data += zipfp.read(info)
154 self.assertTrue(data == "foobar" or data == "barfoo")
Georg Brandl112aa502008-05-20 08:25:48 +0000155
Ezio Melottid5a23e32009-07-15 17:07:04 +0000156 def zip_random_open_test(self, f, compression):
157 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000158
159 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000160 with zipfile.ZipFile(f, "r", compression) as zipfp:
161 zipdata1 = []
162 zipopen1 = zipfp.open(TESTFN)
163 while True:
164 read_data = zipopen1.read(randint(1, 1024))
165 if not read_data:
166 break
167 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000168
Ezio Melotti569e61f2009-12-30 06:14:51 +0000169 self.assertEqual(''.join(zipdata1), self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000170
Ezio Melottid5a23e32009-07-15 17:07:04 +0000171 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000172 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000173 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +0000174
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000175 def test_univeral_readaheads(self):
176 f = StringIO()
177
178 data = 'a\r\n' * 16 * 1024
179 zipfp = zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED)
180 zipfp.writestr(TESTFN, data)
181 zipfp.close()
182
183 data2 = ''
184 zipfp = zipfile.ZipFile(f, 'r')
185 zipopen = zipfp.open(TESTFN, 'rU')
186 for line in zipopen:
187 data2 += line
188 zipfp.close()
189
190 self.assertEqual(data, data2.replace('\n', '\r\n'))
191
192 def zip_readline_read_test(self, f, compression):
193 self.make_test_archive(f, compression)
194
195 # Read the ZIP archive
196 zipfp = zipfile.ZipFile(f, "r")
197 zipopen = zipfp.open(TESTFN)
198
199 data = ''
200 while True:
201 read = zipopen.readline()
202 if not read:
203 break
204 data += read
205
206 read = zipopen.read(100)
207 if not read:
208 break
209 data += read
210
211 self.assertEqual(data, self.data)
212 zipfp.close()
213
Ezio Melottid5a23e32009-07-15 17:07:04 +0000214 def zip_readline_test(self, f, compression):
215 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000216
217 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000218 with zipfile.ZipFile(f, "r") as zipfp:
219 zipopen = zipfp.open(TESTFN)
220 for line in self.line_gen:
221 linedata = zipopen.readline()
222 self.assertEqual(linedata, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000223
Ezio Melottid5a23e32009-07-15 17:07:04 +0000224 def zip_readlines_test(self, f, compression):
225 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000226
227 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000228 with zipfile.ZipFile(f, "r") as zipfp:
229 ziplines = zipfp.open(TESTFN).readlines()
230 for line, zipline in zip(self.line_gen, ziplines):
231 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000232
Ezio Melottid5a23e32009-07-15 17:07:04 +0000233 def zip_iterlines_test(self, f, compression):
234 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000235
236 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000237 with zipfile.ZipFile(f, "r") as zipfp:
238 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
239 self.assertEqual(zipline, line + '\n')
Tim Petersea5962f2007-03-12 18:07:52 +0000240
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000241 def test_readline_read_stored(self):
242 # Issue #7610: calls to readline() interleaved with calls to read().
243 for f in (TESTFN2, TemporaryFile(), StringIO()):
244 self.zip_readline_read_test(f, zipfile.ZIP_STORED)
245
Ezio Melottid5a23e32009-07-15 17:07:04 +0000246 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000247 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000248 self.zip_readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000249
Ezio Melottid5a23e32009-07-15 17:07:04 +0000250 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000251 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000252 self.zip_readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000253
Ezio Melottid5a23e32009-07-15 17:07:04 +0000254 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000255 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000256 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000257
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000258 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000259 def test_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000260 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000261 self.zip_test(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000262
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000263 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000264 def test_open_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000265 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000266 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000267
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000268 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000269 def test_random_open_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000270 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000271 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000272
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000273 @skipUnless(zlib, "requires zlib")
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000274 def test_readline_read_deflated(self):
275 # Issue #7610: calls to readline() interleaved with calls to read().
276 for f in (TESTFN2, TemporaryFile(), StringIO()):
277 self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED)
278
279 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000280 def test_readline_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000281 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000282 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000283
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000284 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000285 def test_readlines_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000286 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000287 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000288
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000289 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000290 def test_iterlines_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000291 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000292 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
Tim Petersea5962f2007-03-12 18:07:52 +0000293
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000294 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000295 def test_low_compression(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000296 """Check for cases where compressed data is larger than original."""
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000297 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000298 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
299 zipfp.writestr("strfile", '12')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000300
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000301 # Get an open object for strfile
Ezio Melotti569e61f2009-12-30 06:14:51 +0000302 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
303 openobj = zipfp.open("strfile")
304 self.assertEqual(openobj.read(1), '1')
305 self.assertEqual(openobj.read(1), '2')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000306
Ezio Melottid5a23e32009-07-15 17:07:04 +0000307 def test_absolute_arcnames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000308 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
309 zipfp.write(TESTFN, "/absolute")
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000310
Ezio Melotti569e61f2009-12-30 06:14:51 +0000311 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
312 self.assertEqual(zipfp.namelist(), ["absolute"])
Tim Peters32cbc962006-02-20 21:42:18 +0000313
Ezio Melottid5a23e32009-07-15 17:07:04 +0000314 def test_append_to_zip_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000315 """Test appending to an existing zipfile."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000316 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
317 zipfp.write(TESTFN, TESTFN)
318
319 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
320 zipfp.writestr("strfile", self.data)
321 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000322
Ezio Melottid5a23e32009-07-15 17:07:04 +0000323 def test_append_to_non_zip_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000324 """Test appending to an existing file that is not a zipfile."""
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000325 # NOTE: this test fails if len(d) < 22 because of the first
326 # line "fpin.seek(-22, 2)" in _EndRecData
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000327 data = 'I am not a ZipFile!'*10
328 with open(TESTFN2, 'wb') as f:
329 f.write(data)
330
Ezio Melotti569e61f2009-12-30 06:14:51 +0000331 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
332 zipfp.write(TESTFN, TESTFN)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000333
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000334 with open(TESTFN2, 'rb') as f:
335 f.seek(len(data))
336 with zipfile.ZipFile(f, "r") as zipfp:
337 self.assertEqual(zipfp.namelist(), [TESTFN])
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000338
Ezio Melottid5a23e32009-07-15 17:07:04 +0000339 def test_write_default_name(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000340 """Check that calling ZipFile.write without arcname specified
341 produces the expected result."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000342 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
343 zipfp.write(TESTFN)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000344 self.assertEqual(zipfp.read(TESTFN), open(TESTFN).read())
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000345
Ezio Melotti1036a7f2009-09-12 14:43:43 +0000346 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000347 def test_per_file_compression(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000348 """Check that files within a Zip archive can have different
349 compression options."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000350 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
351 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
352 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
353 sinfo = zipfp.getinfo('storeme')
354 dinfo = zipfp.getinfo('deflateme')
355 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
356 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000357
Ezio Melottid5a23e32009-07-15 17:07:04 +0000358 def test_write_to_readonly(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000359 """Check that trying to call write() on a readonly ZipFile object
360 raises a RuntimeError."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000361 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
362 zipfp.writestr("somefile.txt", "bogus")
363
364 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
365 self.assertRaises(RuntimeError, zipfp.write, TESTFN)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000366
Ezio Melottid5a23e32009-07-15 17:07:04 +0000367 def test_extract(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000368 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
369 for fpath, fdata in SMALL_TEST_DATA:
370 zipfp.writestr(fpath, fdata)
Georg Brandl62416bc2008-01-07 18:47:44 +0000371
Ezio Melotti569e61f2009-12-30 06:14:51 +0000372 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
373 for fpath, fdata in SMALL_TEST_DATA:
374 writtenfile = zipfp.extract(fpath)
Georg Brandl62416bc2008-01-07 18:47:44 +0000375
Ezio Melotti569e61f2009-12-30 06:14:51 +0000376 # make sure it was written to the right place
377 if os.path.isabs(fpath):
378 correctfile = os.path.join(os.getcwd(), fpath[1:])
379 else:
380 correctfile = os.path.join(os.getcwd(), fpath)
381 correctfile = os.path.normpath(correctfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000382
Ezio Melotti569e61f2009-12-30 06:14:51 +0000383 self.assertEqual(writtenfile, correctfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000384
Ezio Melotti569e61f2009-12-30 06:14:51 +0000385 # make sure correct data is in correct file
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000386 self.assertEqual(fdata, open(writtenfile, "rb").read())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000387 os.remove(writtenfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000388
389 # remove the test file subdirectories
390 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
391
Ezio Melottid5a23e32009-07-15 17:07:04 +0000392 def test_extract_all(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000393 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
394 for fpath, fdata in SMALL_TEST_DATA:
395 zipfp.writestr(fpath, fdata)
Georg Brandl62416bc2008-01-07 18:47:44 +0000396
Ezio Melotti569e61f2009-12-30 06:14:51 +0000397 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
398 zipfp.extractall()
399 for fpath, fdata in SMALL_TEST_DATA:
400 if os.path.isabs(fpath):
401 outfile = os.path.join(os.getcwd(), fpath[1:])
402 else:
403 outfile = os.path.join(os.getcwd(), fpath)
Georg Brandl62416bc2008-01-07 18:47:44 +0000404
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000405 self.assertEqual(fdata, open(outfile, "rb").read())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000406 os.remove(outfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000407
408 # remove the test file subdirectories
409 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
410
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000411 def zip_test_writestr_permissions(self, f, compression):
412 # Make sure that writestr creates files with mode 0600,
413 # when it is passed a name rather than a ZipInfo instance.
414
Ezio Melottid5a23e32009-07-15 17:07:04 +0000415 self.make_test_archive(f, compression)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000416 with zipfile.ZipFile(f, "r") as zipfp:
417 zinfo = zipfp.getinfo('strfile')
418 self.assertEqual(zinfo.external_attr, 0600 << 16)
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000419
Ezio Melottid5a23e32009-07-15 17:07:04 +0000420 def test_writestr_permissions(self):
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000421 for f in (TESTFN2, TemporaryFile(), StringIO()):
422 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
423
Ezio Melotti569e61f2009-12-30 06:14:51 +0000424 def test_close(self):
425 """Check that the zipfile is closed after the 'with' block."""
426 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
427 for fpath, fdata in SMALL_TEST_DATA:
428 zipfp.writestr(fpath, fdata)
429 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
430 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
431
432 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
433 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
434 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
435
436 def test_close_on_exception(self):
437 """Check that the zipfile is closed if an exception is raised in the
438 'with' block."""
439 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
440 for fpath, fdata in SMALL_TEST_DATA:
441 zipfp.writestr(fpath, fdata)
442
443 try:
444 with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
445 raise zipfile.BadZipfile()
446 except zipfile.BadZipfile:
447 self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
448
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000449 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000450 unlink(TESTFN)
451 unlink(TESTFN2)
452
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000453
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000454class TestZip64InSmallFiles(unittest.TestCase):
455 # These tests test the ZIP64 functionality without using large files,
456 # see test_zipfile64 for proper tests.
457
458 def setUp(self):
459 self._limit = zipfile.ZIP64_LIMIT
460 zipfile.ZIP64_LIMIT = 5
461
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000462 line_gen = ("Test of zipfile line %d." % i
463 for i in range(0, FIXEDTEST_SIZE))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000464 self.data = '\n'.join(line_gen)
465
466 # Make a source file with some lines
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000467 with open(TESTFN, "wb") as fp:
468 fp.write(self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000469
Ezio Melottid5a23e32009-07-15 17:07:04 +0000470 def large_file_exception_test(self, f, compression):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000471 with zipfile.ZipFile(f, "w", compression) as zipfp:
472 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000473 zipfp.write, TESTFN, "another.name")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000474
Ezio Melottid5a23e32009-07-15 17:07:04 +0000475 def large_file_exception_test2(self, f, compression):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000476 with zipfile.ZipFile(f, "w", compression) as zipfp:
477 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000478 zipfp.writestr, "another.name", self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000479
Ezio Melottid5a23e32009-07-15 17:07:04 +0000480 def test_large_file_exception(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000481 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000482 self.large_file_exception_test(f, zipfile.ZIP_STORED)
483 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000484
Ezio Melottid5a23e32009-07-15 17:07:04 +0000485 def zip_test(self, f, compression):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000486 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000487 with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000488 zipfp.write(TESTFN, "another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +0000489 zipfp.write(TESTFN, TESTFN)
490 zipfp.writestr("strfile", self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000491
492 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000493 with zipfile.ZipFile(f, "r", compression) as zipfp:
494 self.assertEqual(zipfp.read(TESTFN), self.data)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000495 self.assertEqual(zipfp.read("another.name"), self.data)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000496 self.assertEqual(zipfp.read("strfile"), self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000497
Ezio Melotti569e61f2009-12-30 06:14:51 +0000498 # Print the ZIP directory
499 fp = StringIO()
500 stdout = sys.stdout
501 try:
502 sys.stdout = fp
503 zipfp.printdir()
504 finally:
505 sys.stdout = stdout
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000506
Ezio Melotti569e61f2009-12-30 06:14:51 +0000507 directory = fp.getvalue()
508 lines = directory.splitlines()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000509 self.assertEqual(len(lines), 4) # Number of files + header
Tim Petersa608bb22006-06-15 18:06:29 +0000510
Ezio Melottiaa980582010-01-23 23:04:36 +0000511 self.assertIn('File Name', lines[0])
512 self.assertIn('Modified', lines[0])
513 self.assertIn('Size', lines[0])
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000514
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000515 fn, date, time_, size = lines[1].split()
516 self.assertEqual(fn, 'another.name')
517 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
518 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
519 self.assertEqual(size, str(len(self.data)))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000520
Ezio Melotti569e61f2009-12-30 06:14:51 +0000521 # Check the namelist
522 names = zipfp.namelist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000523 self.assertEqual(len(names), 3)
Ezio Melottiaa980582010-01-23 23:04:36 +0000524 self.assertIn(TESTFN, names)
525 self.assertIn("another.name", names)
526 self.assertIn("strfile", names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000527
Ezio Melotti569e61f2009-12-30 06:14:51 +0000528 # Check infolist
529 infos = zipfp.infolist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000530 names = [i.filename for i in infos]
531 self.assertEqual(len(names), 3)
Ezio Melottiaa980582010-01-23 23:04:36 +0000532 self.assertIn(TESTFN, names)
533 self.assertIn("another.name", names)
534 self.assertIn("strfile", names)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000535 for i in infos:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000536 self.assertEqual(i.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000537
Ezio Melotti569e61f2009-12-30 06:14:51 +0000538 # check getinfo
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000539 for nm in (TESTFN, "another.name", "strfile"):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000540 info = zipfp.getinfo(nm)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000541 self.assertEqual(info.filename, nm)
542 self.assertEqual(info.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000543
Ezio Melotti569e61f2009-12-30 06:14:51 +0000544 # Check that testzip doesn't raise an exception
545 zipfp.testzip()
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000546
Ezio Melottid5a23e32009-07-15 17:07:04 +0000547 def test_stored(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000548 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000549 self.zip_test(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000550
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000551 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000552 def test_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000553 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000554 self.zip_test(f, zipfile.ZIP_DEFLATED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000555
Ezio Melottid5a23e32009-07-15 17:07:04 +0000556 def test_absolute_arcnames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000557 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
558 allowZip64=True) as zipfp:
559 zipfp.write(TESTFN, "/absolute")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000560
Ezio Melotti569e61f2009-12-30 06:14:51 +0000561 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
562 self.assertEqual(zipfp.namelist(), ["absolute"])
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000563
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000564 def tearDown(self):
565 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000566 unlink(TESTFN)
567 unlink(TESTFN2)
568
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000569
570class PyZipFileTests(unittest.TestCase):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000571 def test_write_pyfile(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000572 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
573 fn = __file__
574 if fn.endswith('.pyc') or fn.endswith('.pyo'):
575 fn = fn[:-1]
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000576
Ezio Melotti569e61f2009-12-30 06:14:51 +0000577 zipfp.writepy(fn)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000578
Ezio Melotti569e61f2009-12-30 06:14:51 +0000579 bn = os.path.basename(fn)
Ezio Melottiaa980582010-01-23 23:04:36 +0000580 self.assertNotIn(bn, zipfp.namelist())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000581 self.assertTrue(bn + 'o' in zipfp.namelist() or
582 bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000583
Ezio Melotti569e61f2009-12-30 06:14:51 +0000584 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
585 fn = __file__
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000586 if fn.endswith(('.pyc', '.pyo')):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000587 fn = fn[:-1]
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000588
Ezio Melotti569e61f2009-12-30 06:14:51 +0000589 zipfp.writepy(fn, "testpackage")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000590
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000591 bn = "%s/%s" % ("testpackage", os.path.basename(fn))
Ezio Melottiaa980582010-01-23 23:04:36 +0000592 self.assertNotIn(bn, zipfp.namelist())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000593 self.assertTrue(bn + 'o' in zipfp.namelist() or
594 bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000595
Ezio Melottid5a23e32009-07-15 17:07:04 +0000596 def test_write_python_package(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000597 import email
598 packagedir = os.path.dirname(email.__file__)
599
Ezio Melotti569e61f2009-12-30 06:14:51 +0000600 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
601 zipfp.writepy(packagedir)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000602
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000603 # Check for a couple of modules at different levels of the
604 # hierarchy
Ezio Melotti569e61f2009-12-30 06:14:51 +0000605 names = zipfp.namelist()
606 self.assertTrue('email/__init__.pyo' in names or
607 'email/__init__.pyc' in names)
608 self.assertTrue('email/mime/text.pyo' in names or
609 'email/mime/text.pyc' in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000610
Ezio Melottid5a23e32009-07-15 17:07:04 +0000611 def test_write_python_directory(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000612 os.mkdir(TESTFN2)
613 try:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000614 with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
Ezio Melotti763f1e82009-12-31 13:27:41 +0000615 fp.write("print(42)\n")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000616
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000617 with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
Ezio Melotti763f1e82009-12-31 13:27:41 +0000618 fp.write("print(42 * 42)\n")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000619
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000620 with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp:
621 fp.write("bla bla bla\n")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000622
623 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
624 zipfp.writepy(TESTFN2)
625
626 names = zipfp.namelist()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000627 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
628 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
Ezio Melottiaa980582010-01-23 23:04:36 +0000629 self.assertNotIn('mod2.txt', names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000630
631 finally:
632 shutil.rmtree(TESTFN2)
633
Ezio Melottid5a23e32009-07-15 17:07:04 +0000634 def test_write_non_pyfile(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000635 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000636 open(TESTFN, 'w').write('most definitely not a python file')
Ezio Melotti569e61f2009-12-30 06:14:51 +0000637 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
638 os.remove(TESTFN)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000639
640
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000641class OtherTests(unittest.TestCase):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000642 def test_unicode_filenames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000643 with zipfile.ZipFile(TESTFN, "w") as zf:
644 zf.writestr(u"foo.txt", "Test for unicode filename")
645 zf.writestr(u"\xf6.txt", "Test for unicode filename")
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000646 self.assertIsInstance(zf.infolist()[0].filename, unicode)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000647
648 with zipfile.ZipFile(TESTFN, "r") as zf:
649 self.assertEqual(zf.filelist[0].filename, "foo.txt")
650 self.assertEqual(zf.filelist[1].filename, u"\xf6.txt")
Martin v. Löwis471617d2008-05-05 17:16:58 +0000651
Ezio Melottid5a23e32009-07-15 17:07:04 +0000652 def test_create_non_existent_file_for_append(self):
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000653 if os.path.exists(TESTFN):
654 os.unlink(TESTFN)
Tim Petersea5962f2007-03-12 18:07:52 +0000655
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000656 filename = 'testfile.txt'
657 content = 'hello, world. this is some content.'
Tim Petersea5962f2007-03-12 18:07:52 +0000658
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000659 try:
Ezio Melotti569e61f2009-12-30 06:14:51 +0000660 with zipfile.ZipFile(TESTFN, 'a') as zf:
661 zf.writestr(filename, content)
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000662 except IOError:
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000663 self.fail('Could not append data to a non-existent zip file.')
664
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000665 self.assertTrue(os.path.exists(TESTFN))
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000666
Ezio Melotti569e61f2009-12-30 06:14:51 +0000667 with zipfile.ZipFile(TESTFN, 'r') as zf:
668 self.assertEqual(zf.read(filename), content)
Tim Petersea5962f2007-03-12 18:07:52 +0000669
Ezio Melottid5a23e32009-07-15 17:07:04 +0000670 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000671 # This test checks that the ZipFile constructor closes the file object
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000672 # it opens if there's an error in the file. If it doesn't, the
673 # traceback holds a reference to the ZipFile object and, indirectly,
674 # the file object.
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000675 # On Windows, this causes the os.unlink() call to fail because the
676 # underlying file is still open. This is SF bug #412214.
677 #
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000678 with open(TESTFN, "w") as fp:
679 fp.write("this is not a legal zip file\n")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000680 try:
681 zf = zipfile.ZipFile(TESTFN)
682 except zipfile.BadZipfile:
Collin Winter04a51ec2007-03-29 02:28:16 +0000683 pass
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000684
Ezio Melottid5a23e32009-07-15 17:07:04 +0000685 def test_is_zip_erroneous_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000686 """Check that is_zipfile() correctly identifies non-zip files."""
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000687 # - passing a filename
688 with open(TESTFN, "w") as fp:
689 fp.write("this is not a legal zip file\n")
Tim Petersea5962f2007-03-12 18:07:52 +0000690 chk = zipfile.is_zipfile(TESTFN)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000691 self.assertFalse(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000692 # - passing a file object
693 with open(TESTFN, "rb") as fp:
694 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000695 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000696 # - passing a file-like object
697 fp = StringIO()
698 fp.write("this is not a legal zip file\n")
699 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000700 self.assertTrue(not chk)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000701 fp.seek(0, 0)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000702 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000703 self.assertTrue(not chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000704
Ezio Melottid5a23e32009-07-15 17:07:04 +0000705 def test_is_zip_valid_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000706 """Check that is_zipfile() correctly identifies zip files."""
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000707 # - passing a filename
Ezio Melotti569e61f2009-12-30 06:14:51 +0000708 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
709 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Tim Petersea5962f2007-03-12 18:07:52 +0000710 chk = zipfile.is_zipfile(TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000711 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000712 # - passing a file object
713 with open(TESTFN, "rb") as fp:
714 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000715 self.assertTrue(chk)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000716 fp.seek(0, 0)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000717 zip_contents = fp.read()
718 # - passing a file-like object
719 fp = StringIO()
720 fp.write(zip_contents)
721 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000722 self.assertTrue(chk)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000723 fp.seek(0, 0)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000724 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000725 self.assertTrue(chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000726
Ezio Melottid5a23e32009-07-15 17:07:04 +0000727 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000728 # make sure we don't raise an AttributeError when a partially-constructed
729 # ZipFile instance is finalized; this tests for regression on SF tracker
730 # bug #403871.
731
732 # The bug we're testing for caused an AttributeError to be raised
733 # when a ZipFile instance was created for a file that did not
734 # exist; the .fp member was not initialized but was needed by the
735 # __del__() method. Since the AttributeError is in the __del__(),
736 # it is ignored, but the user should be sufficiently annoyed by
737 # the message on the output that regression will be noticed
738 # quickly.
739 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
740
Amaury Forgeot d'Arc3e5b0272009-07-28 22:15:30 +0000741 def test_empty_file_raises_BadZipFile(self):
742 f = open(TESTFN, 'w')
743 f.close()
744 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
745
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000746 with open(TESTFN, 'w') as fp:
747 fp.write("short file")
Amaury Forgeot d'Arc3e5b0272009-07-28 22:15:30 +0000748 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
749
Ezio Melottid5a23e32009-07-15 17:07:04 +0000750 def test_closed_zip_raises_RuntimeError(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000751 """Verify that testzip() doesn't swallow inappropriate exceptions."""
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000752 data = StringIO()
Ezio Melotti569e61f2009-12-30 06:14:51 +0000753 with zipfile.ZipFile(data, mode="w") as zipf:
754 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000755
756 # This is correct; calling .read on a closed ZipFile should throw
757 # a RuntimeError, and so should calling .testzip. An earlier
758 # version of .testzip would swallow this exception (and any other)
759 # and report that the first file in the archive was corrupt.
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000760 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
761 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000762 self.assertRaises(RuntimeError, zipf.testzip)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000763 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000764 open(TESTFN, 'w').write('zipfile test data')
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000765 self.assertRaises(RuntimeError, zipf.write, TESTFN)
766
Ezio Melottid5a23e32009-07-15 17:07:04 +0000767 def test_bad_constructor_mode(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000768 """Check that bad modes passed to ZipFile constructor are caught."""
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000769 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
770
Ezio Melottid5a23e32009-07-15 17:07:04 +0000771 def test_bad_open_mode(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000772 """Check that bad modes passed to ZipFile.open are caught."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000773 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
774 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
775
776 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000777 # read the data to make sure the file is there
Ezio Melotti569e61f2009-12-30 06:14:51 +0000778 zipf.read("foo.txt")
779 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000780
Ezio Melottid5a23e32009-07-15 17:07:04 +0000781 def test_read0(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000782 """Check that calling read(0) on a ZipExtFile object returns an empty
783 string and doesn't advance file pointer."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000784 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
785 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
786 # read the data to make sure the file is there
787 f = zipf.open("foo.txt")
788 for i in xrange(FIXEDTEST_SIZE):
789 self.assertEqual(f.read(0), '')
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000790
Ezio Melotti569e61f2009-12-30 06:14:51 +0000791 self.assertEqual(f.read(), "O, for a Muse of Fire!")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000792
Ezio Melottid5a23e32009-07-15 17:07:04 +0000793 def test_open_non_existent_item(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000794 """Check that attempting to call open() for an item that doesn't
795 exist in the archive raises a RuntimeError."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000796 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
797 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000798
Ezio Melottid5a23e32009-07-15 17:07:04 +0000799 def test_bad_compression_mode(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000800 """Check that bad compression methods passed to ZipFile.open are
801 caught."""
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000802 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
803
Ezio Melottid5a23e32009-07-15 17:07:04 +0000804 def test_null_byte_in_filename(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000805 """Check that a filename containing a null byte is properly
806 terminated."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000807 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
808 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
809 self.assertEqual(zipf.namelist(), ['foo.txt'])
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000810
Ezio Melottid5a23e32009-07-15 17:07:04 +0000811 def test_struct_sizes(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000812 """Check that ZIP internal structure sizes are calculated correctly."""
Martin v. Löwis8c436412008-07-03 12:51:14 +0000813 self.assertEqual(zipfile.sizeEndCentDir, 22)
814 self.assertEqual(zipfile.sizeCentralDir, 46)
815 self.assertEqual(zipfile.sizeEndCentDir64, 56)
816 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
817
Ezio Melottid5a23e32009-07-15 17:07:04 +0000818 def test_comments(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000819 """Check that comments on the archive are handled properly."""
Martin v. Löwis8c436412008-07-03 12:51:14 +0000820
821 # check default comment is empty
Ezio Melotti569e61f2009-12-30 06:14:51 +0000822 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
823 self.assertEqual(zipf.comment, '')
824 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
825
826 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
827 self.assertEqual(zipf.comment, '')
Martin v. Löwis8c436412008-07-03 12:51:14 +0000828
829 # check a simple short comment
830 comment = 'Bravely taking to his feet, he beat a very brave retreat.'
Ezio Melotti569e61f2009-12-30 06:14:51 +0000831 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
832 zipf.comment = comment
833 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
834 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
835 self.assertEqual(zipf.comment, comment)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000836
837 # check a comment of max length
838 comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)])
Ezio Melotti569e61f2009-12-30 06:14:51 +0000839 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
840 zipf.comment = comment2
841 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
842
843 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
844 self.assertEqual(zipf.comment, comment2)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000845
846 # check a comment that is too long is truncated
Ezio Melotti569e61f2009-12-30 06:14:51 +0000847 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
848 zipf.comment = comment2 + 'oops'
849 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
850 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
851 self.assertEqual(zipf.comment, comment2)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000852
Collin Winter04a51ec2007-03-29 02:28:16 +0000853 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000854 unlink(TESTFN)
855 unlink(TESTFN2)
856
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000857
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000858class DecryptionTests(unittest.TestCase):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000859 """Check that ZIP decryption works. Since the library does not
860 support encryption at the moment, we use a pre-generated encrypted
861 ZIP file."""
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000862
863 data = (
864 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
865 '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
866 '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
867 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
868 '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
869 '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
870 '\x00\x00L\x00\x00\x00\x00\x00' )
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000871 data2 = (
872 'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
873 '\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
874 '\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
875 'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
876 '\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
877 '\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
878 'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
879 '\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000880
881 plain = 'zipfile.py encryption test'
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000882 plain2 = '\x00'*512
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000883
884 def setUp(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000885 with open(TESTFN, "wb") as fp:
886 fp.write(self.data)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000887 self.zip = zipfile.ZipFile(TESTFN, "r")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000888 with open(TESTFN2, "wb") as fp:
889 fp.write(self.data2)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000890 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000891
892 def tearDown(self):
893 self.zip.close()
894 os.unlink(TESTFN)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000895 self.zip2.close()
896 os.unlink(TESTFN2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000897
Ezio Melottid5a23e32009-07-15 17:07:04 +0000898 def test_no_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000899 # Reading the encrypted file without password
900 # must generate a RunTime exception
901 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000902 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000903
Ezio Melottid5a23e32009-07-15 17:07:04 +0000904 def test_bad_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000905 self.zip.setpassword("perl")
906 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000907 self.zip2.setpassword("perl")
908 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Tim Petersea5962f2007-03-12 18:07:52 +0000909
Ezio Melotti1036a7f2009-09-12 14:43:43 +0000910 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000911 def test_good_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000912 self.zip.setpassword("python")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000913 self.assertEqual(self.zip.read("test.txt"), self.plain)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000914 self.zip2.setpassword("12345")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000915 self.assertEqual(self.zip2.read("zero"), self.plain2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000916
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000917
918class TestsWithRandomBinaryFiles(unittest.TestCase):
919 def setUp(self):
920 datacount = randint(16, 64)*1024 + randint(1, 1024)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000921 self.data = ''.join(struct.pack('<f', random()*randint(-1000, 1000))
Ezio Melotti763f1e82009-12-31 13:27:41 +0000922 for i in xrange(datacount))
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000923
924 # Make a source file with some lines
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000925 with open(TESTFN, "wb") as fp:
926 fp.write(self.data)
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000927
Collin Winter04a51ec2007-03-29 02:28:16 +0000928 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000929 unlink(TESTFN)
930 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000931
Ezio Melottid5a23e32009-07-15 17:07:04 +0000932 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000933 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000934 with zipfile.ZipFile(f, "w", compression) as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000935 zipfp.write(TESTFN, "another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +0000936 zipfp.write(TESTFN, TESTFN)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000937
Ezio Melottid5a23e32009-07-15 17:07:04 +0000938 def zip_test(self, f, compression):
939 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000940
941 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000942 with zipfile.ZipFile(f, "r", compression) as zipfp:
943 testdata = zipfp.read(TESTFN)
944 self.assertEqual(len(testdata), len(self.data))
945 self.assertEqual(testdata, self.data)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000946 self.assertEqual(zipfp.read("another.name"), self.data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000947
Ezio Melottid5a23e32009-07-15 17:07:04 +0000948 def test_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000949 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000950 self.zip_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +0000951
Ezio Melottid5a23e32009-07-15 17:07:04 +0000952 def zip_open_test(self, f, compression):
953 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000954
955 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000956 with zipfile.ZipFile(f, "r", compression) as zipfp:
957 zipdata1 = []
958 zipopen1 = zipfp.open(TESTFN)
959 while True:
960 read_data = zipopen1.read(256)
961 if not read_data:
962 break
963 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000964
Ezio Melotti569e61f2009-12-30 06:14:51 +0000965 zipdata2 = []
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000966 zipopen2 = zipfp.open("another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +0000967 while True:
968 read_data = zipopen2.read(256)
969 if not read_data:
970 break
971 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +0000972
Ezio Melotti569e61f2009-12-30 06:14:51 +0000973 testdata1 = ''.join(zipdata1)
974 self.assertEqual(len(testdata1), len(self.data))
975 self.assertEqual(testdata1, self.data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000976
Ezio Melotti569e61f2009-12-30 06:14:51 +0000977 testdata2 = ''.join(zipdata2)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000978 self.assertEqual(len(testdata2), len(self.data))
979 self.assertEqual(testdata2, self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000980
Ezio Melottid5a23e32009-07-15 17:07:04 +0000981 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000982 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000983 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000984
Ezio Melottid5a23e32009-07-15 17:07:04 +0000985 def zip_random_open_test(self, f, compression):
986 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000987
988 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000989 with zipfile.ZipFile(f, "r", compression) as zipfp:
990 zipdata1 = []
991 zipopen1 = zipfp.open(TESTFN)
992 while True:
993 read_data = zipopen1.read(randint(1, 1024))
994 if not read_data:
995 break
996 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000997
Ezio Melotti569e61f2009-12-30 06:14:51 +0000998 testdata = ''.join(zipdata1)
999 self.assertEqual(len(testdata), len(self.data))
1000 self.assertEqual(testdata, self.data)
Tim Petersea5962f2007-03-12 18:07:52 +00001001
Ezio Melottid5a23e32009-07-15 17:07:04 +00001002 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001003 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001004 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001005
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001006
Ezio Melotti1036a7f2009-09-12 14:43:43 +00001007@skipUnless(zlib, "requires zlib")
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001008class TestsWithMultipleOpens(unittest.TestCase):
1009 def setUp(self):
1010 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001011 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
1012 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
1013 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
Tim Petersea5962f2007-03-12 18:07:52 +00001014
Ezio Melottid5a23e32009-07-15 17:07:04 +00001015 def test_same_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001016 # Verify that (when the ZipFile is in control of creating file objects)
1017 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +00001018 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1019 zopen1 = zipf.open('ones')
1020 zopen2 = zipf.open('ones')
1021 data1 = zopen1.read(500)
1022 data2 = zopen2.read(500)
1023 data1 += zopen1.read(500)
1024 data2 += zopen2.read(500)
1025 self.assertEqual(data1, data2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001026
Ezio Melottid5a23e32009-07-15 17:07:04 +00001027 def test_different_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001028 # Verify that (when the ZipFile is in control of creating file objects)
1029 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +00001030 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1031 zopen1 = zipf.open('ones')
1032 zopen2 = zipf.open('twos')
1033 data1 = zopen1.read(500)
1034 data2 = zopen2.read(500)
1035 data1 += zopen1.read(500)
1036 data2 += zopen2.read(500)
1037 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1038 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001039
Ezio Melottid5a23e32009-07-15 17:07:04 +00001040 def test_interleaved(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001041 # Verify that (when the ZipFile is in control of creating file objects)
1042 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +00001043 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1044 zopen1 = zipf.open('ones')
1045 data1 = zopen1.read(500)
1046 zopen2 = zipf.open('twos')
1047 data2 = zopen2.read(500)
1048 data1 += zopen1.read(500)
1049 data2 += zopen2.read(500)
1050 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1051 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
Tim Petersea5962f2007-03-12 18:07:52 +00001052
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001053 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001054 unlink(TESTFN2)
1055
Tim Petersea5962f2007-03-12 18:07:52 +00001056
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001057class TestWithDirectory(unittest.TestCase):
1058 def setUp(self):
1059 os.mkdir(TESTFN2)
1060
Ezio Melottid5a23e32009-07-15 17:07:04 +00001061 def test_extract_dir(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +00001062 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
1063 zipf.extractall(TESTFN2)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001064 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1065 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1066 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1067
Ezio Melottid5a23e32009-07-15 17:07:04 +00001068 def test_bug_6050(self):
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001069 # Extraction should succeed if directories already exist
1070 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottid5a23e32009-07-15 17:07:04 +00001071 self.test_extract_dir()
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001072
Ezio Melottid5a23e32009-07-15 17:07:04 +00001073 def test_store_dir(self):
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001074 os.mkdir(os.path.join(TESTFN2, "x"))
1075 zipf = zipfile.ZipFile(TESTFN, "w")
1076 zipf.write(os.path.join(TESTFN2, "x"), "x")
1077 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1078
1079 def tearDown(self):
1080 shutil.rmtree(TESTFN2)
1081 if os.path.exists(TESTFN):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001082 unlink(TESTFN)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001083
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001084
1085class UniversalNewlineTests(unittest.TestCase):
1086 def setUp(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001087 self.line_gen = ["Test of zipfile line %d." % i
1088 for i in xrange(FIXEDTEST_SIZE)]
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001089 self.seps = ('\r', '\r\n', '\n')
1090 self.arcdata, self.arcfiles = {}, {}
1091 for n, s in enumerate(self.seps):
1092 self.arcdata[s] = s.join(self.line_gen) + s
1093 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Brett Cannon6cef0762007-05-25 20:17:15 +00001094 open(self.arcfiles[s], "wb").write(self.arcdata[s])
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001095
Ezio Melottid5a23e32009-07-15 17:07:04 +00001096 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001097 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001098 with zipfile.ZipFile(f, "w", compression) as zipfp:
1099 for fn in self.arcfiles.values():
1100 zipfp.write(fn, fn)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001101
Ezio Melottid5a23e32009-07-15 17:07:04 +00001102 def read_test(self, f, compression):
1103 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001104
1105 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001106 with zipfile.ZipFile(f, "r") as zipfp:
1107 for sep, fn in self.arcfiles.items():
1108 zipdata = zipfp.open(fn, "rU").read()
1109 self.assertEqual(self.arcdata[sep], zipdata)
Tim Petersea5962f2007-03-12 18:07:52 +00001110
Antoine Pitrou94c33eb2010-01-27 20:59:50 +00001111 def readline_read_test(self, f, compression):
1112 self.make_test_archive(f, compression)
1113
1114 # Read the ZIP archive
1115 zipfp = zipfile.ZipFile(f, "r")
1116 for sep, fn in self.arcfiles.items():
1117 zipopen = zipfp.open(fn, "rU")
1118 data = ''
1119 while True:
1120 read = zipopen.readline()
1121 if not read:
1122 break
1123 data += read
1124
1125 read = zipopen.read(5)
1126 if not read:
1127 break
1128 data += read
1129
1130 self.assertEqual(data, self.arcdata['\n'])
1131
1132 zipfp.close()
1133
Ezio Melottid5a23e32009-07-15 17:07:04 +00001134 def readline_test(self, f, compression):
1135 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001136
1137 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001138 with zipfile.ZipFile(f, "r") as zipfp:
1139 for sep, fn in self.arcfiles.items():
1140 zipopen = zipfp.open(fn, "rU")
1141 for line in self.line_gen:
1142 linedata = zipopen.readline()
1143 self.assertEqual(linedata, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001144
Ezio Melottid5a23e32009-07-15 17:07:04 +00001145 def readlines_test(self, f, compression):
1146 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001147
1148 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001149 with zipfile.ZipFile(f, "r") as zipfp:
1150 for sep, fn in self.arcfiles.items():
1151 ziplines = zipfp.open(fn, "rU").readlines()
1152 for line, zipline in zip(self.line_gen, ziplines):
1153 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001154
Ezio Melottid5a23e32009-07-15 17:07:04 +00001155 def iterlines_test(self, f, compression):
1156 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001157
1158 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001159 with zipfile.ZipFile(f, "r") as zipfp:
1160 for sep, fn in self.arcfiles.items():
1161 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
1162 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001163
Ezio Melottid5a23e32009-07-15 17:07:04 +00001164 def test_read_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001165 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001166 self.read_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001167
Antoine Pitrou94c33eb2010-01-27 20:59:50 +00001168 def test_readline_read_stored(self):
1169 # Issue #7610: calls to readline() interleaved with calls to read().
1170 for f in (TESTFN2, TemporaryFile(), StringIO()):
1171 self.readline_read_test(f, zipfile.ZIP_STORED)
1172
Ezio Melottid5a23e32009-07-15 17:07:04 +00001173 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001174 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001175 self.readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001176
Ezio Melottid5a23e32009-07-15 17:07:04 +00001177 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001178 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001179 self.readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001180
Ezio Melottid5a23e32009-07-15 17:07:04 +00001181 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001182 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001183 self.iterlines_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001184
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001185 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001186 def test_read_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001187 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001188 self.read_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001189
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001190 @skipUnless(zlib, "requires zlib")
Antoine Pitrou94c33eb2010-01-27 20:59:50 +00001191 def test_readline_read_deflated(self):
1192 # Issue #7610: calls to readline() interleaved with calls to read().
1193 for f in (TESTFN2, TemporaryFile(), StringIO()):
1194 self.readline_read_test(f, zipfile.ZIP_DEFLATED)
1195
1196 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001197 def test_readline_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001198 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001199 self.readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001200
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001201 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001202 def test_readlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001203 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001204 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001205
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001206 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001207 def test_iterlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001208 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001209 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001210
1211 def tearDown(self):
1212 for sep, fn in self.arcfiles.items():
1213 os.remove(fn)
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001214 unlink(TESTFN)
1215 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001216
1217
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001218def test_main():
Tim Petersea5962f2007-03-12 18:07:52 +00001219 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1220 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001221 TestWithDirectory, UniversalNewlineTests,
1222 TestsWithRandomBinaryFiles)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001223
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001224if __name__ == "__main__":
1225 test_main()