blob: e9a90e55fbc5340a63c7bfc58a1e0aedaaddc405 [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
Ezio Melotti74c96ec2009-07-08 22:24:06 +00006
7import io
8import os
Barry Warsaw28a691b2010-04-17 00:19:56 +00009import imp
Ezio Melotti35386712009-12-31 13:22:41 +000010import time
Ezio Melotti74c96ec2009-07-08 22:24:06 +000011import shutil
12import struct
13import zipfile
14import unittest
15
Tim Petersa45cacf2004-08-20 03:47:14 +000016
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000017from tempfile import TemporaryFile
Guido van Rossumd8faa362007-04-27 19:54:29 +000018from random import randint, random
Ezio Melotti74c96ec2009-07-08 22:24:06 +000019from unittest import skipUnless
Tim Petersa19a1682001-03-29 04:36:09 +000020
Ezio Melotti76430242009-07-11 18:28:48 +000021from 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öwis59e47792009-01-24 14:10:07 +000024TESTFNDIR = TESTFN + "d"
Guido van Rossumb5a755e2007-07-18 18:15:48 +000025FIXEDTEST_SIZE = 1000
Guido van Rossum368f04a2000-04-10 13:23:04 +000026
Christian Heimes790c8232008-01-07 21:14:23 +000027SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
28 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
29 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
30 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
31
Ezio Melotti76430242009-07-11 18:28:48 +000032
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000033class TestsWithSourceFile(unittest.TestCase):
34 def setUp(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +000035 self.line_gen = (bytes("Zipfile test line %d. random float: %f" %
Guido van Rossum9c627722007-08-27 18:31:48 +000036 (i, random()), "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +000037 for i in range(FIXEDTEST_SIZE))
38 self.data = b'\n'.join(self.line_gen) + b'\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000039
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000040 # Make a source file with some lines
Ezio Melotti35386712009-12-31 13:22:41 +000041 with open(TESTFN, "wb") as fp:
42 fp.write(self.data)
Tim Peters7d3bad62001-04-04 18:56:49 +000043
Ezio Melottiafd0d112009-07-15 17:17:17 +000044 def make_test_archive(self, f, compression):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000045 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000046 with zipfile.ZipFile(f, "w", compression) as zipfp:
47 zipfp.write(TESTFN, "another.name")
48 zipfp.write(TESTFN, TESTFN)
49 zipfp.writestr("strfile", self.data)
Tim Peters7d3bad62001-04-04 18:56:49 +000050
Ezio Melottiafd0d112009-07-15 17:17:17 +000051 def zip_test(self, f, compression):
52 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +000053
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000054 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000055 with zipfile.ZipFile(f, "r", compression) as zipfp:
56 self.assertEqual(zipfp.read(TESTFN), self.data)
57 self.assertEqual(zipfp.read("another.name"), self.data)
58 self.assertEqual(zipfp.read("strfile"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000060 # Print the ZIP directory
61 fp = io.StringIO()
62 zipfp.printdir(file=fp)
63 directory = fp.getvalue()
64 lines = directory.splitlines()
Ezio Melotti35386712009-12-31 13:22:41 +000065 self.assertEqual(len(lines), 4) # Number of files + header
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066
Benjamin Peterson577473f2010-01-19 00:09:57 +000067 self.assertIn('File Name', lines[0])
68 self.assertIn('Modified', lines[0])
69 self.assertIn('Size', lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070
Ezio Melotti35386712009-12-31 13:22:41 +000071 fn, date, time_, size = lines[1].split()
72 self.assertEqual(fn, 'another.name')
73 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
74 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
75 self.assertEqual(size, str(len(self.data)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000077 # Check the namelist
78 names = zipfp.namelist()
Ezio Melotti35386712009-12-31 13:22:41 +000079 self.assertEqual(len(names), 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +000080 self.assertIn(TESTFN, names)
81 self.assertIn("another.name", names)
82 self.assertIn("strfile", names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000084 # Check infolist
85 infos = zipfp.infolist()
Ezio Melotti35386712009-12-31 13:22:41 +000086 names = [i.filename for i in infos]
87 self.assertEqual(len(names), 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +000088 self.assertIn(TESTFN, names)
89 self.assertIn("another.name", names)
90 self.assertIn("strfile", names)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000091 for i in infos:
Ezio Melotti35386712009-12-31 13:22:41 +000092 self.assertEqual(i.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093
Ezio Melottifaa6b7f2009-12-30 12:34:59 +000094 # check getinfo
95 for nm in (TESTFN, "another.name", "strfile"):
96 info = zipfp.getinfo(nm)
Ezio Melotti35386712009-12-31 13:22:41 +000097 self.assertEqual(info.filename, nm)
98 self.assertEqual(info.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000099
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000100 # Check that testzip doesn't raise an exception
101 zipfp.testzip()
Tim Peters7d3bad62001-04-04 18:56:49 +0000102
Ezio Melottiafd0d112009-07-15 17:17:17 +0000103 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000104 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000105 self.zip_test(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000106
Ezio Melottiafd0d112009-07-15 17:17:17 +0000107 def zip_open_test(self, f, compression):
108 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000109
110 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000111 with zipfile.ZipFile(f, "r", compression) as zipfp:
112 zipdata1 = []
113 zipopen1 = zipfp.open(TESTFN)
114 while True:
115 read_data = zipopen1.read(256)
116 if not read_data:
117 break
118 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000119
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000120 zipdata2 = []
121 zipopen2 = zipfp.open("another.name")
122 while True:
123 read_data = zipopen2.read(256)
124 if not read_data:
125 break
126 zipdata2.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000127
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000128 self.assertEqual(b''.join(zipdata1), self.data)
129 self.assertEqual(b''.join(zipdata2), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000130
Ezio Melottiafd0d112009-07-15 17:17:17 +0000131 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000132 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000133 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000134
Ezio Melottiafd0d112009-07-15 17:17:17 +0000135 def test_open_via_zip_info(self):
Georg Brandlb533e262008-05-25 18:19:30 +0000136 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000137 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
138 zipfp.writestr("name", "foo")
139 zipfp.writestr("name", "bar")
Georg Brandlb533e262008-05-25 18:19:30 +0000140
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000141 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
142 infos = zipfp.infolist()
143 data = b""
144 for info in infos:
145 data += zipfp.open(info).read()
146 self.assertTrue(data == b"foobar" or data == b"barfoo")
147 data = b""
148 for info in infos:
149 data += zipfp.read(info)
150 self.assertTrue(data == b"foobar" or data == b"barfoo")
Georg Brandlb533e262008-05-25 18:19:30 +0000151
Ezio Melottiafd0d112009-07-15 17:17:17 +0000152 def zip_random_open_test(self, f, compression):
153 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000154
155 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000156 with zipfile.ZipFile(f, "r", compression) as zipfp:
157 zipdata1 = []
158 zipopen1 = zipfp.open(TESTFN)
159 while True:
160 read_data = zipopen1.read(randint(1, 1024))
161 if not read_data:
162 break
163 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000164
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000165 self.assertEqual(b''.join(zipdata1), self.data)
166
Guido van Rossumd8faa362007-04-27 19:54:29 +0000167
Ezio Melottiafd0d112009-07-15 17:17:17 +0000168 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000169 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000170 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000171
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000172 def test_univeral_readaheads(self):
173 f = io.BytesIO()
174
175 data = b'a\r\n' * 16 * 1024
176 zipfp = zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED)
177 zipfp.writestr(TESTFN, data)
178 zipfp.close()
179
180 data2 = b''
181 zipfp = zipfile.ZipFile(f, 'r')
182 zipopen = zipfp.open(TESTFN, 'rU')
183 for line in zipopen:
184 data2 += line
185 zipfp.close()
186
187 self.assertEqual(data, data2.replace(b'\n', b'\r\n'))
188
189 def zip_readline_read_test(self, f, compression):
190 self.make_test_archive(f, compression)
191
192 # Read the ZIP archive
193 zipfp = zipfile.ZipFile(f, "r")
194 zipopen = zipfp.open(TESTFN)
195
196 data = b''
197 while True:
198 read = zipopen.readline()
199 if not read:
200 break
201 data += read
202
203 read = zipopen.read(100)
204 if not read:
205 break
206 data += read
207
208 self.assertEqual(data, self.data)
209 zipfp.close()
210
Ezio Melottiafd0d112009-07-15 17:17:17 +0000211 def zip_readline_test(self, f, compression):
212 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000213
214 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000215 with zipfile.ZipFile(f, "r") as zipfp:
216 zipopen = zipfp.open(TESTFN)
217 for line in self.line_gen:
218 linedata = zipopen.readline()
219 self.assertEqual(linedata, line + '\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000220
Ezio Melottiafd0d112009-07-15 17:17:17 +0000221 def zip_readlines_test(self, f, compression):
222 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000223
224 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000225 with zipfile.ZipFile(f, "r") as zipfp:
226 ziplines = zipfp.open(TESTFN).readlines()
227 for line, zipline in zip(self.line_gen, ziplines):
228 self.assertEqual(zipline, line + '\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000229
Ezio Melottiafd0d112009-07-15 17:17:17 +0000230 def zip_iterlines_test(self, f, compression):
231 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000232
233 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000234 with zipfile.ZipFile(f, "r") as zipfp:
235 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
236 self.assertEqual(zipline, line + '\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000237
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000238 def test_readline_read_stored(self):
239 # Issue #7610: calls to readline() interleaved with calls to read().
240 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
241 self.zip_readline_read_test(f, zipfile.ZIP_STORED)
242
Ezio Melottiafd0d112009-07-15 17:17:17 +0000243 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000244 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000245 self.zip_readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000246
Ezio Melottiafd0d112009-07-15 17:17:17 +0000247 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000248 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000249 self.zip_readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000250
Ezio Melottiafd0d112009-07-15 17:17:17 +0000251 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000252 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000253 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000254
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000255 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000256 def test_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000257 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000258 self.zip_test(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000259
Guido van Rossumd8faa362007-04-27 19:54:29 +0000260
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000261 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000262 def test_open_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000263 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000264 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000265
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000266 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000267 def test_random_open_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000268 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000269 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000270
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000271 @skipUnless(zlib, "requires zlib")
Antoine Pitroua32f9a22010-01-27 21:18:57 +0000272 def test_readline_read_deflated(self):
273 # Issue #7610: calls to readline() interleaved with calls to read().
274 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
275 self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED)
276
277 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000278 def test_readline_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000279 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000280 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000281
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000282 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000283 def test_readlines_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000284 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000285 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000286
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000287 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000288 def test_iterlines_deflated(self):
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000289 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000290 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000291
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000292 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000293 def test_low_compression(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000294 """Check for cases where compressed data is larger than original."""
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000295 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000296 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
297 zipfp.writestr("strfile", '12')
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000298
299 # Get an open object for strfile
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000300 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
301 openobj = zipfp.open("strfile")
302 self.assertEqual(openobj.read(1), b'1')
303 self.assertEqual(openobj.read(1), b'2')
Ezio Melotti74c96ec2009-07-08 22:24:06 +0000304
Ezio Melottiafd0d112009-07-15 17:17:17 +0000305 def test_absolute_arcnames(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000306 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
307 zipfp.write(TESTFN, "/absolute")
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000308
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000309 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
310 self.assertEqual(zipfp.namelist(), ["absolute"])
Tim Peters32cbc962006-02-20 21:42:18 +0000311
Ezio Melottiafd0d112009-07-15 17:17:17 +0000312 def test_append_to_zip_file(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000313 """Test appending to an existing zipfile."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000314 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
315 zipfp.write(TESTFN, TESTFN)
316
317 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
318 zipfp.writestr("strfile", self.data)
319 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000320
Ezio Melottiafd0d112009-07-15 17:17:17 +0000321 def test_append_to_non_zip_file(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000322 """Test appending to an existing file that is not a zipfile."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000323 # NOTE: this test fails if len(d) < 22 because of the first
324 # line "fpin.seek(-22, 2)" in _EndRecData
Ezio Melotti35386712009-12-31 13:22:41 +0000325 data = b'I am not a ZipFile!'*10
326 with open(TESTFN2, 'wb') as f:
327 f.write(data)
328
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000329 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
330 zipfp.write(TESTFN, TESTFN)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000331
Ezio Melotti35386712009-12-31 13:22:41 +0000332 with open(TESTFN2, 'rb') as f:
333 f.seek(len(data))
334 with zipfile.ZipFile(f, "r") as zipfp:
335 self.assertEqual(zipfp.namelist(), [TESTFN])
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000336
Ezio Melottiafd0d112009-07-15 17:17:17 +0000337 def test_write_default_name(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000338 """Check that calling ZipFile.write without arcname specified
339 produces the expected result."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000340 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
341 zipfp.write(TESTFN)
342 self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read())
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000343
Ezio Melotti78ea2022009-09-12 18:41:20 +0000344 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000345 def test_per_file_compression(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000346 """Check that files within a Zip archive can have different
347 compression options."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000348 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
349 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
350 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
351 sinfo = zipfp.getinfo('storeme')
352 dinfo = zipfp.getinfo('deflateme')
353 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
354 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000355
Ezio Melottiafd0d112009-07-15 17:17:17 +0000356 def test_write_to_readonly(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000357 """Check that trying to call write() on a readonly ZipFile object
358 raises a RuntimeError."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000359 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
360 zipfp.writestr("somefile.txt", "bogus")
361
362 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
363 self.assertRaises(RuntimeError, zipfp.write, TESTFN)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000364
Ezio Melottiafd0d112009-07-15 17:17:17 +0000365 def test_extract(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000366 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
367 for fpath, fdata in SMALL_TEST_DATA:
368 zipfp.writestr(fpath, fdata)
Christian Heimes790c8232008-01-07 21:14:23 +0000369
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000370 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
371 for fpath, fdata in SMALL_TEST_DATA:
372 writtenfile = zipfp.extract(fpath)
Christian Heimes790c8232008-01-07 21:14:23 +0000373
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000374 # make sure it was written to the right place
375 if os.path.isabs(fpath):
376 correctfile = os.path.join(os.getcwd(), fpath[1:])
377 else:
378 correctfile = os.path.join(os.getcwd(), fpath)
379 correctfile = os.path.normpath(correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000380
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000381 self.assertEqual(writtenfile, correctfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000382
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000383 # make sure correct data is in correct file
384 self.assertEqual(fdata.encode(), open(writtenfile, "rb").read())
Christian Heimes790c8232008-01-07 21:14:23 +0000385
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000386 os.remove(writtenfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000387
388 # remove the test file subdirectories
389 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
390
Ezio Melottiafd0d112009-07-15 17:17:17 +0000391 def test_extract_all(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000392 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
393 for fpath, fdata in SMALL_TEST_DATA:
394 zipfp.writestr(fpath, fdata)
Christian Heimes790c8232008-01-07 21:14:23 +0000395
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000396 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
397 zipfp.extractall()
398 for fpath, fdata in SMALL_TEST_DATA:
399 if os.path.isabs(fpath):
400 outfile = os.path.join(os.getcwd(), fpath[1:])
401 else:
402 outfile = os.path.join(os.getcwd(), fpath)
Christian Heimes790c8232008-01-07 21:14:23 +0000403
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000404 self.assertEqual(fdata.encode(), open(outfile, "rb").read())
Christian Heimes790c8232008-01-07 21:14:23 +0000405
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000406 os.remove(outfile)
Christian Heimes790c8232008-01-07 21:14:23 +0000407
408 # remove the test file subdirectories
409 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
410
Ronald Oussorenee5c8852010-02-07 20:24:02 +0000411 def test_writestr_compression(self):
412 zipfp = zipfile.ZipFile(TESTFN2, "w")
413 zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED)
414 if zlib:
415 zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED)
416
417 info = zipfp.getinfo('a.txt')
418 self.assertEqual(info.compress_type, zipfile.ZIP_STORED)
419
420 if zlib:
421 info = zipfp.getinfo('b.txt')
422 self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED)
423
424
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000425 def zip_test_writestr_permissions(self, f, compression):
426 # Make sure that writestr creates files with mode 0600,
427 # when it is passed a name rather than a ZipInfo instance.
428
Ezio Melottiafd0d112009-07-15 17:17:17 +0000429 self.make_test_archive(f, compression)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000430 with zipfile.ZipFile(f, "r") as zipfp:
431 zinfo = zipfp.getinfo('strfile')
432 self.assertEqual(zinfo.external_attr, 0o600 << 16)
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000433
Ezio Melottiafd0d112009-07-15 17:17:17 +0000434 def test_writestr_permissions(self):
Antoine Pitrou6e1df8d2008-07-25 19:58:18 +0000435 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
436 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
437
Gregory P. Smithb0d9ca92009-07-07 05:06:04 +0000438 def test_writestr_extended_local_header_issue1202(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000439 with zipfile.ZipFile(TESTFN2, 'w') as orig_zip:
440 for data in 'abcdefghijklmnop':
441 zinfo = zipfile.ZipInfo(data)
442 zinfo.flag_bits |= 0x08 # Include an extended local header.
443 orig_zip.writestr(zinfo, data)
444
445 def test_close(self):
446 """Check that the zipfile is closed after the 'with' block."""
447 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
448 for fpath, fdata in SMALL_TEST_DATA:
449 zipfp.writestr(fpath, fdata)
450 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
451 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
452
453 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
454 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
455 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
456
457 def test_close_on_exception(self):
458 """Check that the zipfile is closed if an exception is raised in the
459 'with' block."""
460 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
461 for fpath, fdata in SMALL_TEST_DATA:
462 zipfp.writestr(fpath, fdata)
463
464 try:
465 with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
466 raise zipfile.BadZipfile()
467 except zipfile.BadZipfile:
468 self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
Gregory P. Smithb0d9ca92009-07-07 05:06:04 +0000469
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000470 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000471 unlink(TESTFN)
472 unlink(TESTFN2)
473
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000474
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000475class TestZip64InSmallFiles(unittest.TestCase):
476 # These tests test the ZIP64 functionality without using large files,
477 # see test_zipfile64 for proper tests.
478
479 def setUp(self):
480 self._limit = zipfile.ZIP64_LIMIT
481 zipfile.ZIP64_LIMIT = 5
482
Guido van Rossum9c627722007-08-27 18:31:48 +0000483 line_gen = (bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000484 for i in range(0, FIXEDTEST_SIZE))
485 self.data = b'\n'.join(line_gen)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000486
487 # Make a source file with some lines
Ezio Melotti35386712009-12-31 13:22:41 +0000488 with open(TESTFN, "wb") as fp:
489 fp.write(self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000490
Ezio Melottiafd0d112009-07-15 17:17:17 +0000491 def large_file_exception_test(self, f, compression):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000492 with zipfile.ZipFile(f, "w", compression) as zipfp:
493 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti35386712009-12-31 13:22:41 +0000494 zipfp.write, TESTFN, "another.name")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000495
Ezio Melottiafd0d112009-07-15 17:17:17 +0000496 def large_file_exception_test2(self, f, compression):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000497 with zipfile.ZipFile(f, "w", compression) as zipfp:
498 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti35386712009-12-31 13:22:41 +0000499 zipfp.writestr, "another.name", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000500
Ezio Melottiafd0d112009-07-15 17:17:17 +0000501 def test_large_file_exception(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000502 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000503 self.large_file_exception_test(f, zipfile.ZIP_STORED)
504 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000505
Ezio Melottiafd0d112009-07-15 17:17:17 +0000506 def zip_test(self, f, compression):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000507 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000508 with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
509 zipfp.write(TESTFN, "another.name")
510 zipfp.write(TESTFN, TESTFN)
511 zipfp.writestr("strfile", self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000512
513 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000514 with zipfile.ZipFile(f, "r", compression) as zipfp:
515 self.assertEqual(zipfp.read(TESTFN), self.data)
516 self.assertEqual(zipfp.read("another.name"), self.data)
517 self.assertEqual(zipfp.read("strfile"), self.data)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000518
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000519 # Print the ZIP directory
520 fp = io.StringIO()
521 zipfp.printdir(fp)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000522
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000523 directory = fp.getvalue()
524 lines = directory.splitlines()
Ezio Melotti35386712009-12-31 13:22:41 +0000525 self.assertEqual(len(lines), 4) # Number of files + header
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000526
Benjamin Peterson577473f2010-01-19 00:09:57 +0000527 self.assertIn('File Name', lines[0])
528 self.assertIn('Modified', lines[0])
529 self.assertIn('Size', lines[0])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000530
Ezio Melotti35386712009-12-31 13:22:41 +0000531 fn, date, time_, size = lines[1].split()
532 self.assertEqual(fn, 'another.name')
533 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
534 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
535 self.assertEqual(size, str(len(self.data)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000536
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000537 # Check the namelist
538 names = zipfp.namelist()
Ezio Melotti35386712009-12-31 13:22:41 +0000539 self.assertEqual(len(names), 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000540 self.assertIn(TESTFN, names)
541 self.assertIn("another.name", names)
542 self.assertIn("strfile", names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000543
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000544 # Check infolist
545 infos = zipfp.infolist()
Ezio Melotti35386712009-12-31 13:22:41 +0000546 names = [i.filename for i in infos]
547 self.assertEqual(len(names), 3)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000548 self.assertIn(TESTFN, names)
549 self.assertIn("another.name", names)
550 self.assertIn("strfile", names)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000551 for i in infos:
Ezio Melotti35386712009-12-31 13:22:41 +0000552 self.assertEqual(i.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000553
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000554 # check getinfo
555 for nm in (TESTFN, "another.name", "strfile"):
556 info = zipfp.getinfo(nm)
Ezio Melotti35386712009-12-31 13:22:41 +0000557 self.assertEqual(info.filename, nm)
558 self.assertEqual(info.file_size, len(self.data))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000559
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000560 # Check that testzip doesn't raise an exception
561 zipfp.testzip()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000562
Ezio Melottiafd0d112009-07-15 17:17:17 +0000563 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000564 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000565 self.zip_test(f, zipfile.ZIP_STORED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000566
Ezio Melotti76430242009-07-11 18:28:48 +0000567 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000568 def test_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000569 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000570 self.zip_test(f, zipfile.ZIP_DEFLATED)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000571
Ezio Melottiafd0d112009-07-15 17:17:17 +0000572 def test_absolute_arcnames(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000573 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
574 allowZip64=True) as zipfp:
575 zipfp.write(TESTFN, "/absolute")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000576
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000577 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
578 self.assertEqual(zipfp.namelist(), ["absolute"])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000579
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000580 def tearDown(self):
581 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti76430242009-07-11 18:28:48 +0000582 unlink(TESTFN)
583 unlink(TESTFN2)
584
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000585
586class PyZipFileTests(unittest.TestCase):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000587 def test_write_pyfile(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000588 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
589 fn = __file__
590 if fn.endswith('.pyc') or fn.endswith('.pyo'):
Barry Warsaw28a691b2010-04-17 00:19:56 +0000591 path_split = fn.split(os.sep)
592 if os.altsep is not None:
593 path_split.extend(fn.split(os.altsep))
594 if '__pycache__' in path_split:
595 fn = imp.source_from_cache(fn)
596 else:
597 fn = fn[:-1]
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000598
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000599 zipfp.writepy(fn)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000600
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000601 bn = os.path.basename(fn)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000602 self.assertNotIn(bn, zipfp.namelist())
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000603 self.assertTrue(bn + 'o' in zipfp.namelist() or
604 bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000605
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000606 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
607 fn = __file__
Ezio Melotti35386712009-12-31 13:22:41 +0000608 if fn.endswith(('.pyc', '.pyo')):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000609 fn = fn[:-1]
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000610
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000611 zipfp.writepy(fn, "testpackage")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000612
Ezio Melotti35386712009-12-31 13:22:41 +0000613 bn = "%s/%s" % ("testpackage", os.path.basename(fn))
Benjamin Peterson577473f2010-01-19 00:09:57 +0000614 self.assertNotIn(bn, zipfp.namelist())
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000615 self.assertTrue(bn + 'o' in zipfp.namelist() or
616 bn + 'c' in zipfp.namelist())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000617
Ezio Melottiafd0d112009-07-15 17:17:17 +0000618 def test_write_python_package(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000619 import email
620 packagedir = os.path.dirname(email.__file__)
621
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000622 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
623 zipfp.writepy(packagedir)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000624
Ezio Melotti35386712009-12-31 13:22:41 +0000625 # Check for a couple of modules at different levels of the
626 # hierarchy
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000627 names = zipfp.namelist()
628 self.assertTrue('email/__init__.pyo' in names or
629 'email/__init__.pyc' in names)
630 self.assertTrue('email/mime/text.pyo' in names or
631 'email/mime/text.pyc' in names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000632
Ezio Melottiafd0d112009-07-15 17:17:17 +0000633 def test_write_python_directory(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000634 os.mkdir(TESTFN2)
635 try:
Ezio Melotti35386712009-12-31 13:22:41 +0000636 with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
637 fp.write("print(42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000638
Ezio Melotti35386712009-12-31 13:22:41 +0000639 with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
640 fp.write("print(42 * 42)\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000641
Ezio Melotti35386712009-12-31 13:22:41 +0000642 with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp:
643 fp.write("bla bla bla\n")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000644
645 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
646 zipfp.writepy(TESTFN2)
647
648 names = zipfp.namelist()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000649 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
650 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000651 self.assertNotIn('mod2.txt', names)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000652
653 finally:
654 shutil.rmtree(TESTFN2)
655
Ezio Melottiafd0d112009-07-15 17:17:17 +0000656 def test_write_non_pyfile(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000657 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
658 open(TESTFN, 'w').write('most definitely not a python file')
659 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
660 os.remove(TESTFN)
661
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000662
663
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000664class OtherTests(unittest.TestCase):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000665 def test_unicode_filenames(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000666 with zipfile.ZipFile(TESTFN, "w") as zf:
667 zf.writestr("foo.txt", "Test for unicode filename")
668 zf.writestr("\xf6.txt", "Test for unicode filename")
Ezio Melottie9615932010-01-24 19:26:24 +0000669 self.assertIsInstance(zf.infolist()[0].filename, str)
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000670
671 with zipfile.ZipFile(TESTFN, "r") as zf:
672 self.assertEqual(zf.filelist[0].filename, "foo.txt")
673 self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
Martin v. Löwis8570f6a2008-05-05 17:44:38 +0000674
Ezio Melottiafd0d112009-07-15 17:17:17 +0000675 def test_create_non_existent_file_for_append(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000676 if os.path.exists(TESTFN):
677 os.unlink(TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000678
Thomas Wouterscf297e42007-02-23 15:07:44 +0000679 filename = 'testfile.txt'
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000680 content = b'hello, world. this is some content.'
Guido van Rossumd8faa362007-04-27 19:54:29 +0000681
Thomas Wouterscf297e42007-02-23 15:07:44 +0000682 try:
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000683 with zipfile.ZipFile(TESTFN, 'a') as zf:
684 zf.writestr(filename, content)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000685 except IOError:
686 self.fail('Could not append data to a non-existent zip file.')
687
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000688 self.assertTrue(os.path.exists(TESTFN))
Thomas Wouterscf297e42007-02-23 15:07:44 +0000689
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000690 with zipfile.ZipFile(TESTFN, 'r') as zf:
691 self.assertEqual(zf.read(filename), content)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000692
Ezio Melottiafd0d112009-07-15 17:17:17 +0000693 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000694 # This test checks that the ZipFile constructor closes the file object
Ezio Melotti35386712009-12-31 13:22:41 +0000695 # it opens if there's an error in the file. If it doesn't, the
696 # traceback holds a reference to the ZipFile object and, indirectly,
697 # the file object.
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000698 # On Windows, this causes the os.unlink() call to fail because the
699 # underlying file is still open. This is SF bug #412214.
700 #
Ezio Melotti35386712009-12-31 13:22:41 +0000701 with open(TESTFN, "w") as fp:
702 fp.write("this is not a legal zip file\n")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000703 try:
704 zf = zipfile.ZipFile(TESTFN)
705 except zipfile.BadZipfile:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000706 pass
707
Ezio Melottiafd0d112009-07-15 17:17:17 +0000708 def test_is_zip_erroneous_file(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000709 """Check that is_zipfile() correctly identifies non-zip files."""
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000710 # - passing a filename
711 with open(TESTFN, "w") as fp:
712 fp.write("this is not a legal zip file\n")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000713 chk = zipfile.is_zipfile(TESTFN)
Ezio Melotti35386712009-12-31 13:22:41 +0000714 self.assertFalse(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000715 # - passing a file object
716 with open(TESTFN, "rb") as fp:
717 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000718 self.assertTrue(not chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000719 # - passing a file-like object
720 fp = io.BytesIO()
721 fp.write(b"this is not a legal zip file\n")
722 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000723 self.assertTrue(not chk)
Ezio Melotti35386712009-12-31 13:22:41 +0000724 fp.seek(0, 0)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000725 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000726 self.assertTrue(not chk)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000727
Ezio Melottiafd0d112009-07-15 17:17:17 +0000728 def test_is_zip_valid_file(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000729 """Check that is_zipfile() correctly identifies zip files."""
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000730 # - passing a filename
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000731 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
732 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
733
Guido van Rossumd8faa362007-04-27 19:54:29 +0000734 chk = zipfile.is_zipfile(TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000735 self.assertTrue(chk)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000736 # - passing a file object
737 with open(TESTFN, "rb") as fp:
738 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000739 self.assertTrue(chk)
Ezio Melotti35386712009-12-31 13:22:41 +0000740 fp.seek(0, 0)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000741 zip_contents = fp.read()
742 # - passing a file-like object
743 fp = io.BytesIO()
744 fp.write(zip_contents)
745 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000746 self.assertTrue(chk)
Ezio Melotti35386712009-12-31 13:22:41 +0000747 fp.seek(0, 0)
Antoine Pitroudb5fe662008-12-27 15:50:40 +0000748 chk = zipfile.is_zipfile(fp)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000749 self.assertTrue(chk)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000750
Ezio Melottiafd0d112009-07-15 17:17:17 +0000751 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000752 # make sure we don't raise an AttributeError when a partially-constructed
753 # ZipFile instance is finalized; this tests for regression on SF tracker
754 # bug #403871.
755
756 # The bug we're testing for caused an AttributeError to be raised
757 # when a ZipFile instance was created for a file that did not
758 # exist; the .fp member was not initialized but was needed by the
759 # __del__() method. Since the AttributeError is in the __del__(),
760 # it is ignored, but the user should be sufficiently annoyed by
761 # the message on the output that regression will be noticed
762 # quickly.
763 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
764
Amaury Forgeot d'Arcbc347802009-07-28 22:18:57 +0000765 def test_empty_file_raises_BadZipFile(self):
766 f = open(TESTFN, 'w')
767 f.close()
768 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
769
Ezio Melotti35386712009-12-31 13:22:41 +0000770 with open(TESTFN, 'w') as fp:
771 fp.write("short file")
Amaury Forgeot d'Arcbc347802009-07-28 22:18:57 +0000772 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
773
Ezio Melottiafd0d112009-07-15 17:17:17 +0000774 def test_closed_zip_raises_RuntimeError(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000775 """Verify that testzip() doesn't swallow inappropriate exceptions."""
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000776 data = io.BytesIO()
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000777 with zipfile.ZipFile(data, mode="w") as zipf:
778 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000779
780 # This is correct; calling .read on a closed ZipFile should throw
781 # a RuntimeError, and so should calling .testzip. An earlier
782 # version of .testzip would swallow this exception (and any other)
783 # and report that the first file in the archive was corrupt.
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000784 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
785 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000786 self.assertRaises(RuntimeError, zipf.testzip)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000787 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Guido van Rossum814661e2007-07-18 22:07:29 +0000788 open(TESTFN, 'w').write('zipfile test data')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000789 self.assertRaises(RuntimeError, zipf.write, TESTFN)
790
Ezio Melottiafd0d112009-07-15 17:17:17 +0000791 def test_bad_constructor_mode(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000792 """Check that bad modes passed to ZipFile constructor are caught."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000793 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
794
Ezio Melottiafd0d112009-07-15 17:17:17 +0000795 def test_bad_open_mode(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000796 """Check that bad modes passed to ZipFile.open are caught."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000797 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
798 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
799
800 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000801 # read the data to make sure the file is there
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000802 zipf.read("foo.txt")
803 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000804
Ezio Melottiafd0d112009-07-15 17:17:17 +0000805 def test_read0(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000806 """Check that calling read(0) on a ZipExtFile object returns an empty
807 string and doesn't advance file pointer."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000808 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
809 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
810 # read the data to make sure the file is there
811 f = zipf.open("foo.txt")
812 for i in range(FIXEDTEST_SIZE):
813 self.assertEqual(f.read(0), b'')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000814
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000815 self.assertEqual(f.read(), b"O, for a Muse of Fire!")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000816
Ezio Melottiafd0d112009-07-15 17:17:17 +0000817 def test_open_non_existent_item(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000818 """Check that attempting to call open() for an item that doesn't
819 exist in the archive raises a RuntimeError."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000820 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
821 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000822
Ezio Melottiafd0d112009-07-15 17:17:17 +0000823 def test_bad_compression_mode(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000824 """Check that bad compression methods passed to ZipFile.open are
825 caught."""
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000826 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
827
Ezio Melottiafd0d112009-07-15 17:17:17 +0000828 def test_null_byte_in_filename(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000829 """Check that a filename containing a null byte is properly
830 terminated."""
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000831 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
832 zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
833 self.assertEqual(zipf.namelist(), ['foo.txt'])
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000834
Ezio Melottiafd0d112009-07-15 17:17:17 +0000835 def test_struct_sizes(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000836 """Check that ZIP internal structure sizes are calculated correctly."""
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000837 self.assertEqual(zipfile.sizeEndCentDir, 22)
838 self.assertEqual(zipfile.sizeCentralDir, 46)
839 self.assertEqual(zipfile.sizeEndCentDir64, 56)
840 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
841
Ezio Melottiafd0d112009-07-15 17:17:17 +0000842 def test_comments(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000843 """Check that comments on the archive are handled properly."""
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000844
845 # check default comment is empty
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000846 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
847 self.assertEqual(zipf.comment, b'')
848 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
849
850 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
851 self.assertEqual(zipfr.comment, b'')
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000852
853 # check a simple short comment
854 comment = b'Bravely taking to his feet, he beat a very brave retreat.'
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000855 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
856 zipf.comment = comment
857 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
858 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
859 self.assertEqual(zipf.comment, comment)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000860
861 # check a comment of max length
862 comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
863 comment2 = comment2.encode("ascii")
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000864 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
865 zipf.comment = comment2
866 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
867
868 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
869 self.assertEqual(zipfr.comment, comment2)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000870
871 # check a comment that is too long is truncated
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000872 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
873 zipf.comment = comment2 + b'oops'
874 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
875 with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
876 self.assertEqual(zipfr.comment, comment2)
Martin v. Löwisb09b8442008-07-03 14:13:42 +0000877
Guido van Rossumd8faa362007-04-27 19:54:29 +0000878 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000879 unlink(TESTFN)
880 unlink(TESTFN2)
881
Thomas Wouterscf297e42007-02-23 15:07:44 +0000882
883class DecryptionTests(unittest.TestCase):
Ezio Melotti35386712009-12-31 13:22:41 +0000884 """Check that ZIP decryption works. Since the library does not
885 support encryption at the moment, we use a pre-generated encrypted
886 ZIP file."""
Thomas Wouterscf297e42007-02-23 15:07:44 +0000887
888 data = (
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000889 b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
890 b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
891 b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
892 b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
893 b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
894 b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
895 b'\x00\x00L\x00\x00\x00\x00\x00' )
Christian Heimesfdab48e2008-01-20 09:06:41 +0000896 data2 = (
897 b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
898 b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
899 b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
900 b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
901 b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
902 b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
903 b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
904 b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Thomas Wouterscf297e42007-02-23 15:07:44 +0000905
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000906 plain = b'zipfile.py encryption test'
Christian Heimesfdab48e2008-01-20 09:06:41 +0000907 plain2 = b'\x00'*512
Thomas Wouterscf297e42007-02-23 15:07:44 +0000908
909 def setUp(self):
Ezio Melotti35386712009-12-31 13:22:41 +0000910 with open(TESTFN, "wb") as fp:
911 fp.write(self.data)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000912 self.zip = zipfile.ZipFile(TESTFN, "r")
Ezio Melotti35386712009-12-31 13:22:41 +0000913 with open(TESTFN2, "wb") as fp:
914 fp.write(self.data2)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000915 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000916
917 def tearDown(self):
918 self.zip.close()
919 os.unlink(TESTFN)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000920 self.zip2.close()
921 os.unlink(TESTFN2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000922
Ezio Melottiafd0d112009-07-15 17:17:17 +0000923 def test_no_password(self):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000924 # Reading the encrypted file without password
925 # must generate a RunTime exception
926 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000927 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000928
Ezio Melottiafd0d112009-07-15 17:17:17 +0000929 def test_bad_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000930 self.zip.setpassword(b"perl")
Thomas Wouterscf297e42007-02-23 15:07:44 +0000931 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Christian Heimesfdab48e2008-01-20 09:06:41 +0000932 self.zip2.setpassword(b"perl")
933 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000934
Ezio Melotti78ea2022009-09-12 18:41:20 +0000935 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +0000936 def test_good_password(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000937 self.zip.setpassword(b"python")
Ezio Melotti35386712009-12-31 13:22:41 +0000938 self.assertEqual(self.zip.read("test.txt"), self.plain)
Christian Heimesfdab48e2008-01-20 09:06:41 +0000939 self.zip2.setpassword(b"12345")
Ezio Melotti35386712009-12-31 13:22:41 +0000940 self.assertEqual(self.zip2.read("zero"), self.plain2)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000941
Guido van Rossumd8faa362007-04-27 19:54:29 +0000942
943class TestsWithRandomBinaryFiles(unittest.TestCase):
944 def setUp(self):
945 datacount = randint(16, 64)*1024 + randint(1, 1024)
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000946 self.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
947 for i in range(datacount))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000948
949 # Make a source file with some lines
Ezio Melotti35386712009-12-31 13:22:41 +0000950 with open(TESTFN, "wb") as fp:
951 fp.write(self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000952
953 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +0000954 unlink(TESTFN)
955 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000956
Ezio Melottiafd0d112009-07-15 17:17:17 +0000957 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000958 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000959 with zipfile.ZipFile(f, "w", compression) as zipfp:
960 zipfp.write(TESTFN, "another.name")
961 zipfp.write(TESTFN, TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000962
Ezio Melottiafd0d112009-07-15 17:17:17 +0000963 def zip_test(self, f, compression):
964 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000965
966 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000967 with zipfile.ZipFile(f, "r", compression) as zipfp:
968 testdata = zipfp.read(TESTFN)
969 self.assertEqual(len(testdata), len(self.data))
970 self.assertEqual(testdata, self.data)
971 self.assertEqual(zipfp.read("another.name"), self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000972
Ezio Melottiafd0d112009-07-15 17:17:17 +0000973 def test_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +0000974 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +0000975 self.zip_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000976
Ezio Melottiafd0d112009-07-15 17:17:17 +0000977 def zip_open_test(self, f, compression):
978 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000979
980 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000981 with zipfile.ZipFile(f, "r", compression) as zipfp:
982 zipdata1 = []
983 zipopen1 = zipfp.open(TESTFN)
984 while True:
985 read_data = zipopen1.read(256)
986 if not read_data:
987 break
988 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000989
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000990 zipdata2 = []
991 zipopen2 = zipfp.open("another.name")
992 while True:
993 read_data = zipopen2.read(256)
994 if not read_data:
995 break
996 zipdata2.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000997
Ezio Melottifaa6b7f2009-12-30 12:34:59 +0000998 testdata1 = b''.join(zipdata1)
999 self.assertEqual(len(testdata1), len(self.data))
1000 self.assertEqual(testdata1, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001001
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001002 testdata2 = b''.join(zipdata2)
Ezio Melotti35386712009-12-31 13:22:41 +00001003 self.assertEqual(len(testdata2), len(self.data))
1004 self.assertEqual(testdata2, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001005
Ezio Melottiafd0d112009-07-15 17:17:17 +00001006 def test_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001007 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001008 self.zip_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001009
Ezio Melottiafd0d112009-07-15 17:17:17 +00001010 def zip_random_open_test(self, f, compression):
1011 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001012
1013 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001014 with zipfile.ZipFile(f, "r", compression) as zipfp:
1015 zipdata1 = []
1016 zipopen1 = zipfp.open(TESTFN)
1017 while True:
1018 read_data = zipopen1.read(randint(1, 1024))
1019 if not read_data:
1020 break
1021 zipdata1.append(read_data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001022
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001023 testdata = b''.join(zipdata1)
1024 self.assertEqual(len(testdata), len(self.data))
1025 self.assertEqual(testdata, self.data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001026
Ezio Melottiafd0d112009-07-15 17:17:17 +00001027 def test_random_open_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001028 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001029 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001030
Ezio Melotti76430242009-07-11 18:28:48 +00001031
Ezio Melotti78ea2022009-09-12 18:41:20 +00001032@skipUnless(zlib, "requires zlib")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001033class TestsWithMultipleOpens(unittest.TestCase):
1034 def setUp(self):
1035 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001036 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
1037 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
1038 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001039
Ezio Melottiafd0d112009-07-15 17:17:17 +00001040 def test_same_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +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 Melottifaa6b7f2009-12-30 12:34:59 +00001043 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1044 zopen1 = zipf.open('ones')
1045 zopen2 = zipf.open('ones')
1046 data1 = zopen1.read(500)
1047 data2 = zopen2.read(500)
1048 data1 += zopen1.read(500)
1049 data2 += zopen2.read(500)
1050 self.assertEqual(data1, data2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001051
Ezio Melottiafd0d112009-07-15 17:17:17 +00001052 def test_different_file(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001053 # Verify that (when the ZipFile is in control of creating file objects)
1054 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001055 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1056 zopen1 = zipf.open('ones')
1057 zopen2 = zipf.open('twos')
1058 data1 = zopen1.read(500)
1059 data2 = zopen2.read(500)
1060 data1 += zopen1.read(500)
1061 data2 += zopen2.read(500)
1062 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1063 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001064
Ezio Melottiafd0d112009-07-15 17:17:17 +00001065 def test_interleaved(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001066 # Verify that (when the ZipFile is in control of creating file objects)
1067 # multiple open() calls can be made without interfering with each other.
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001068 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1069 zopen1 = zipf.open('ones')
1070 data1 = zopen1.read(500)
1071 zopen2 = zipf.open('twos')
1072 data2 = zopen2.read(500)
1073 data1 += zopen1.read(500)
1074 data2 += zopen2.read(500)
1075 self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
1076 self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001077
1078 def tearDown(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001079 unlink(TESTFN2)
1080
Guido van Rossumd8faa362007-04-27 19:54:29 +00001081
Martin v. Löwis59e47792009-01-24 14:10:07 +00001082class TestWithDirectory(unittest.TestCase):
1083 def setUp(self):
1084 os.mkdir(TESTFN2)
1085
Ezio Melottiafd0d112009-07-15 17:17:17 +00001086 def test_extract_dir(self):
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001087 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
1088 zipf.extractall(TESTFN2)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001089 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1090 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1091 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1092
Ezio Melottiafd0d112009-07-15 17:17:17 +00001093 def test_bug_6050(self):
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001094 # Extraction should succeed if directories already exist
1095 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottiafd0d112009-07-15 17:17:17 +00001096 self.test_extract_dir()
Martin v. Löwis70ccd162009-05-24 19:47:22 +00001097
Ezio Melottiafd0d112009-07-15 17:17:17 +00001098 def test_store_dir(self):
Martin v. Löwis59e47792009-01-24 14:10:07 +00001099 os.mkdir(os.path.join(TESTFN2, "x"))
1100 zipf = zipfile.ZipFile(TESTFN, "w")
1101 zipf.write(os.path.join(TESTFN2, "x"), "x")
1102 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1103
1104 def tearDown(self):
1105 shutil.rmtree(TESTFN2)
1106 if os.path.exists(TESTFN):
Ezio Melotti76430242009-07-11 18:28:48 +00001107 unlink(TESTFN)
Martin v. Löwis59e47792009-01-24 14:10:07 +00001108
Guido van Rossumd8faa362007-04-27 19:54:29 +00001109
1110class UniversalNewlineTests(unittest.TestCase):
1111 def setUp(self):
Guido van Rossum9c627722007-08-27 18:31:48 +00001112 self.line_gen = [bytes("Test of zipfile line %d." % i, "ascii")
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001113 for i in range(FIXEDTEST_SIZE)]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001114 self.seps = ('\r', '\r\n', '\n')
1115 self.arcdata, self.arcfiles = {}, {}
1116 for n, s in enumerate(self.seps):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001117 b = s.encode("ascii")
1118 self.arcdata[s] = b.join(self.line_gen) + b
Guido van Rossumd8faa362007-04-27 19:54:29 +00001119 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001120 f = open(self.arcfiles[s], "wb")
1121 try:
1122 f.write(self.arcdata[s])
1123 finally:
1124 f.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001125
Ezio Melottiafd0d112009-07-15 17:17:17 +00001126 def make_test_archive(self, f, compression):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001127 # Create the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001128 with zipfile.ZipFile(f, "w", compression) as zipfp:
1129 for fn in self.arcfiles.values():
1130 zipfp.write(fn, fn)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001131
Ezio Melottiafd0d112009-07-15 17:17:17 +00001132 def read_test(self, f, compression):
1133 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001134
1135 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001136 with zipfile.ZipFile(f, "r") as zipfp:
1137 for sep, fn in self.arcfiles.items():
1138 zipdata = zipfp.open(fn, "rU").read()
1139 self.assertEqual(self.arcdata[sep], zipdata)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001140
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001141 def readline_read_test(self, f, compression):
1142 self.make_test_archive(f, compression)
1143
1144 # Read the ZIP archive
1145 zipfp = zipfile.ZipFile(f, "r")
1146 for sep, fn in self.arcfiles.items():
1147 zipopen = zipfp.open(fn, "rU")
1148 data = b''
1149 while True:
1150 read = zipopen.readline()
1151 if not read:
1152 break
1153 data += read
1154
1155 read = zipopen.read(5)
1156 if not read:
1157 break
1158 data += read
1159
1160 self.assertEqual(data, self.arcdata['\n'])
1161
1162 zipfp.close()
1163
Ezio Melottiafd0d112009-07-15 17:17:17 +00001164 def readline_test(self, f, compression):
1165 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001166
1167 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001168 with zipfile.ZipFile(f, "r") as zipfp:
1169 for sep, fn in self.arcfiles.items():
1170 zipopen = zipfp.open(fn, "rU")
1171 for line in self.line_gen:
1172 linedata = zipopen.readline()
1173 self.assertEqual(linedata, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001174
Ezio Melottiafd0d112009-07-15 17:17:17 +00001175 def readlines_test(self, f, compression):
1176 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001177
1178 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001179 with zipfile.ZipFile(f, "r") as zipfp:
1180 for sep, fn in self.arcfiles.items():
1181 ziplines = zipfp.open(fn, "rU").readlines()
1182 for line, zipline in zip(self.line_gen, ziplines):
1183 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001184
Ezio Melottiafd0d112009-07-15 17:17:17 +00001185 def iterlines_test(self, f, compression):
1186 self.make_test_archive(f, compression)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001187
1188 # Read the ZIP archive
Ezio Melottifaa6b7f2009-12-30 12:34:59 +00001189 with zipfile.ZipFile(f, "r") as zipfp:
1190 for sep, fn in self.arcfiles.items():
1191 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
1192 self.assertEqual(zipline, line + b'\n')
Guido van Rossumd8faa362007-04-27 19:54:29 +00001193
Ezio Melottiafd0d112009-07-15 17:17:17 +00001194 def test_read_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001195 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001196 self.read_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001197
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001198 def test_readline_read_stored(self):
1199 # Issue #7610: calls to readline() interleaved with calls to read().
1200 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1201 self.readline_read_test(f, zipfile.ZIP_STORED)
1202
Ezio Melottiafd0d112009-07-15 17:17:17 +00001203 def test_readline_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001204 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001205 self.readline_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001206
Ezio Melottiafd0d112009-07-15 17:17:17 +00001207 def test_readlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001208 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001209 self.readlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001210
Ezio Melottiafd0d112009-07-15 17:17:17 +00001211 def test_iterlines_stored(self):
Guido van Rossumd6ca5462007-05-22 01:29:33 +00001212 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001213 self.iterlines_test(f, zipfile.ZIP_STORED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001214
Ezio Melotti76430242009-07-11 18:28:48 +00001215 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001216 def test_read_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001217 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001218 self.read_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001219
Ezio Melotti76430242009-07-11 18:28:48 +00001220 @skipUnless(zlib, "requires zlib")
Antoine Pitroua32f9a22010-01-27 21:18:57 +00001221 def test_readline_read_deflated(self):
1222 # Issue #7610: calls to readline() interleaved with calls to read().
1223 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1224 self.readline_read_test(f, zipfile.ZIP_DEFLATED)
1225
1226 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001227 def test_readline_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001228 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001229 self.readline_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001230
Ezio Melotti76430242009-07-11 18:28:48 +00001231 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001232 def test_readlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001233 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001234 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001235
Ezio Melotti76430242009-07-11 18:28:48 +00001236 @skipUnless(zlib, "requires zlib")
Ezio Melottiafd0d112009-07-15 17:17:17 +00001237 def test_iterlines_deflated(self):
Ezio Melotti76430242009-07-11 18:28:48 +00001238 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
Ezio Melottiafd0d112009-07-15 17:17:17 +00001239 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001240
1241 def tearDown(self):
1242 for sep, fn in self.arcfiles.items():
1243 os.remove(fn)
Ezio Melotti76430242009-07-11 18:28:48 +00001244 unlink(TESTFN)
1245 unlink(TESTFN2)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001246
1247
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001248def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +00001249 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1250 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti76430242009-07-11 18:28:48 +00001251 TestWithDirectory, UniversalNewlineTests,
1252 TestsWithRandomBinaryFiles)
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001253
1254if __name__ == "__main__":
1255 test_main()