blob: fbeda53096eb9a050be321756247522ea6edc0fe [file] [log] [blame]
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001# We can test part of the module without zlib.
Guido van Rossum368f04a2000-04-10 13:23:04 +00002try:
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00003 import zlib
4except ImportError:
5 zlib = None
Tim Petersa45cacf2004-08-20 03:47:14 +00006
Ezio Melottie7a0cc22009-07-04 14:58:27 +00007import os
Antoine Pitroue1436d12010-08-12 15:25:51 +00008import io
Ezio Melottie7a0cc22009-07-04 14:58:27 +00009import sys
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000010import time
Ezio Melottie7a0cc22009-07-04 14:58:27 +000011import 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
Serhiy Storchakadb03e6b2013-05-08 21:52:31 +030020from test.test_support import TESTFN, TESTFN_UNICODE, TESTFN_ENCODING, \
Benjamin Peterson352eb4f2014-04-03 10:31:25 -040021 run_unittest, findfile, unlink, rmtree, check_warnings
Serhiy Storchakadb03e6b2013-05-08 21:52:31 +030022try:
23 TESTFN_UNICODE.encode(TESTFN_ENCODING)
24except (UnicodeError, TypeError):
25 # Either the file system encoding is None, or the file name
26 # cannot be encoded in the file system encoding.
27 TESTFN_UNICODE = None
Guido van Rossum368f04a2000-04-10 13:23:04 +000028
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000029TESTFN2 = TESTFN + "2"
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +000030TESTFNDIR = TESTFN + "d"
Georg Brandl4b3ab6f2007-07-12 09:59:22 +000031FIXEDTEST_SIZE = 1000
Guido van Rossum368f04a2000-04-10 13:23:04 +000032
Georg Brandl62416bc2008-01-07 18:47:44 +000033SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
34 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
Gregory P. Smith608cc452013-02-01 11:40:18 -080035 ('ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
Georg Brandl62416bc2008-01-07 18:47:44 +000036 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
37
Ezio Melotti6cbfc122009-07-10 20:25:56 +000038
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000039class TestsWithSourceFile(unittest.TestCase):
40 def setUp(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +000041 self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random())
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000042 for i in xrange(FIXEDTEST_SIZE)]
Martin v. Löwis3eb76482007-03-06 10:41:24 +000043 self.data = '\n'.join(self.line_gen) + '\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000044
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000045 # Make a source file with some lines
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000046 with open(TESTFN, "wb") as fp:
47 fp.write(self.data)
Tim Peters7d3bad62001-04-04 18:56:49 +000048
Ezio Melottid5a23e32009-07-15 17:07:04 +000049 def make_test_archive(self, f, compression):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000050 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +000051 with zipfile.ZipFile(f, "w", compression) as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000052 zipfp.write(TESTFN, "another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +000053 zipfp.write(TESTFN, TESTFN)
54 zipfp.writestr("strfile", self.data)
Tim Peters7d3bad62001-04-04 18:56:49 +000055
Ezio Melottid5a23e32009-07-15 17:07:04 +000056 def zip_test(self, f, compression):
57 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +000058
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000059 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +000060 with zipfile.ZipFile(f, "r", compression) as zipfp:
61 self.assertEqual(zipfp.read(TESTFN), self.data)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000062 self.assertEqual(zipfp.read("another.name"), self.data)
Ezio Melotti569e61f2009-12-30 06:14:51 +000063 self.assertEqual(zipfp.read("strfile"), self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000064
Ezio Melotti569e61f2009-12-30 06:14:51 +000065 # Print the ZIP directory
66 fp = StringIO()
67 stdout = sys.stdout
68 try:
69 sys.stdout = fp
70 zipfp.printdir()
71 finally:
72 sys.stdout = stdout
Tim Petersa608bb22006-06-15 18:06:29 +000073
Ezio Melotti569e61f2009-12-30 06:14:51 +000074 directory = fp.getvalue()
75 lines = directory.splitlines()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000076 self.assertEqual(len(lines), 4) # Number of files + header
Ronald Oussoren143cefb2006-06-15 08:14:18 +000077
Ezio Melottiaa980582010-01-23 23:04:36 +000078 self.assertIn('File Name', lines[0])
79 self.assertIn('Modified', lines[0])
80 self.assertIn('Size', lines[0])
Ronald Oussoren143cefb2006-06-15 08:14:18 +000081
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000082 fn, date, time_, size = lines[1].split()
83 self.assertEqual(fn, 'another.name')
84 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
85 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
86 self.assertEqual(size, str(len(self.data)))
Ronald Oussoren143cefb2006-06-15 08:14:18 +000087
Ezio Melotti569e61f2009-12-30 06:14:51 +000088 # Check the namelist
89 names = zipfp.namelist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000090 self.assertEqual(len(names), 3)
Ezio Melottiaa980582010-01-23 23:04:36 +000091 self.assertIn(TESTFN, names)
92 self.assertIn("another.name", names)
93 self.assertIn("strfile", names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000094
Ezio Melotti569e61f2009-12-30 06:14:51 +000095 # Check infolist
96 infos = zipfp.infolist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000097 names = [i.filename for i in infos]
98 self.assertEqual(len(names), 3)
Ezio Melottiaa980582010-01-23 23:04:36 +000099 self.assertIn(TESTFN, names)
100 self.assertIn("another.name", names)
101 self.assertIn("strfile", names)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000102 for i in infos:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000103 self.assertEqual(i.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000104
Ezio Melotti569e61f2009-12-30 06:14:51 +0000105 # check getinfo
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000106 for nm in (TESTFN, "another.name", "strfile"):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000107 info = zipfp.getinfo(nm)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000108 self.assertEqual(info.filename, nm)
109 self.assertEqual(info.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000110
Ezio Melotti569e61f2009-12-30 06:14:51 +0000111 # Check that testzip doesn't raise an exception
112 zipfp.testzip()
Tim Peters7d3bad62001-04-04 18:56:49 +0000113
Ezio Melottid5a23e32009-07-15 17:07:04 +0000114 def test_stored(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000115 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000116 self.zip_test(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000117
Ezio Melottid5a23e32009-07-15 17:07:04 +0000118 def zip_open_test(self, f, compression):
119 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000120
121 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000122 with zipfile.ZipFile(f, "r", compression) as zipfp:
123 zipdata1 = []
Brian Curtin0d654332011-04-19 21:15:55 -0500124 with zipfp.open(TESTFN) as zipopen1:
125 while True:
126 read_data = zipopen1.read(256)
127 if not read_data:
128 break
129 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000130
Ezio Melotti569e61f2009-12-30 06:14:51 +0000131 zipdata2 = []
Brian Curtin0d654332011-04-19 21:15:55 -0500132 with zipfp.open("another.name") as zipopen2:
133 while True:
134 read_data = zipopen2.read(256)
135 if not read_data:
136 break
137 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +0000138
Ezio Melotti569e61f2009-12-30 06:14:51 +0000139 self.assertEqual(''.join(zipdata1), self.data)
140 self.assertEqual(''.join(zipdata2), self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000141
Ezio Melottid5a23e32009-07-15 17:07:04 +0000142 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000143 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000144 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000145
Ezio Melottid5a23e32009-07-15 17:07:04 +0000146 def test_open_via_zip_info(self):
Georg Brandl112aa502008-05-20 08:25:48 +0000147 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000148 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
149 zipfp.writestr("name", "foo")
Serhiy Storchaka49259352014-01-20 21:57:09 +0200150 with check_warnings(('', UserWarning)):
151 zipfp.writestr("name", "bar")
152 self.assertEqual(zipfp.namelist(), ["name"] * 2)
Georg Brandl112aa502008-05-20 08:25:48 +0000153
Ezio Melotti569e61f2009-12-30 06:14:51 +0000154 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
155 infos = zipfp.infolist()
156 data = ""
157 for info in infos:
Brian Curtin0d654332011-04-19 21:15:55 -0500158 with zipfp.open(info) as f:
159 data += f.read()
Ezio Melotti569e61f2009-12-30 06:14:51 +0000160 self.assertTrue(data == "foobar" or data == "barfoo")
161 data = ""
162 for info in infos:
163 data += zipfp.read(info)
164 self.assertTrue(data == "foobar" or data == "barfoo")
Georg Brandl112aa502008-05-20 08:25:48 +0000165
Ezio Melottid5a23e32009-07-15 17:07:04 +0000166 def zip_random_open_test(self, f, compression):
167 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000168
169 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000170 with zipfile.ZipFile(f, "r", compression) as zipfp:
171 zipdata1 = []
Brian Curtin0d654332011-04-19 21:15:55 -0500172 with zipfp.open(TESTFN) as zipopen1:
173 while True:
174 read_data = zipopen1.read(randint(1, 1024))
175 if not read_data:
176 break
177 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000178
Ezio Melotti569e61f2009-12-30 06:14:51 +0000179 self.assertEqual(''.join(zipdata1), self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000180
Ezio Melottid5a23e32009-07-15 17:07:04 +0000181 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000182 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000183 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +0000184
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000185 def test_univeral_readaheads(self):
186 f = StringIO()
187
188 data = 'a\r\n' * 16 * 1024
Brian Curtin0d654332011-04-19 21:15:55 -0500189 with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as zipfp:
190 zipfp.writestr(TESTFN, data)
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000191
192 data2 = ''
Brian Curtin0d654332011-04-19 21:15:55 -0500193 with zipfile.ZipFile(f, 'r') as zipfp:
194 with zipfp.open(TESTFN, 'rU') as zipopen:
195 for line in zipopen:
196 data2 += line
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000197
198 self.assertEqual(data, data2.replace('\n', '\r\n'))
199
200 def zip_readline_read_test(self, f, compression):
201 self.make_test_archive(f, compression)
202
203 # Read the ZIP archive
Brian Curtin0d654332011-04-19 21:15:55 -0500204 with zipfile.ZipFile(f, "r") as zipfp:
205 with zipfp.open(TESTFN) as zipopen:
206 data = ''
207 while True:
208 read = zipopen.readline()
209 if not read:
210 break
211 data += read
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000212
Brian Curtin0d654332011-04-19 21:15:55 -0500213 read = zipopen.read(100)
214 if not read:
215 break
216 data += read
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000217
218 self.assertEqual(data, self.data)
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000219
Ezio Melottid5a23e32009-07-15 17:07:04 +0000220 def zip_readline_test(self, f, compression):
221 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000222
223 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000224 with zipfile.ZipFile(f, "r") as zipfp:
Brian Curtin0d654332011-04-19 21:15:55 -0500225 with zipfp.open(TESTFN) as zipopen:
226 for line in self.line_gen:
227 linedata = zipopen.readline()
228 self.assertEqual(linedata, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000229
Ezio Melottid5a23e32009-07-15 17:07:04 +0000230 def zip_readlines_test(self, f, compression):
231 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000232
233 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000234 with zipfile.ZipFile(f, "r") as zipfp:
Brian Curtin0d654332011-04-19 21:15:55 -0500235 with zipfp.open(TESTFN) as zo:
236 ziplines = zo.readlines()
237 for line, zipline in zip(self.line_gen, ziplines):
238 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000239
Ezio Melottid5a23e32009-07-15 17:07:04 +0000240 def zip_iterlines_test(self, f, compression):
241 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000242
243 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000244 with zipfile.ZipFile(f, "r") as zipfp:
245 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
246 self.assertEqual(zipline, line + '\n')
Tim Petersea5962f2007-03-12 18:07:52 +0000247
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000248 def test_readline_read_stored(self):
249 # Issue #7610: calls to readline() interleaved with calls to read().
250 for f in (TESTFN2, TemporaryFile(), StringIO()):
251 self.zip_readline_read_test(f, zipfile.ZIP_STORED)
252
Ezio Melottid5a23e32009-07-15 17:07:04 +0000253 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000254 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000255 self.zip_readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000256
Ezio Melottid5a23e32009-07-15 17:07:04 +0000257 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000258 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000259 self.zip_readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000260
Ezio Melottid5a23e32009-07-15 17:07:04 +0000261 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000262 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000263 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000264
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000265 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000266 def test_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000267 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000268 self.zip_test(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000269
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000270 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000271 def test_open_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000272 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000273 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000274
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000275 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000276 def test_random_open_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000277 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000278 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000279
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000280 @skipUnless(zlib, "requires zlib")
Antoine Pitrou94c33eb2010-01-27 20:59:50 +0000281 def test_readline_read_deflated(self):
282 # Issue #7610: calls to readline() interleaved with calls to read().
283 for f in (TESTFN2, TemporaryFile(), StringIO()):
284 self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED)
285
286 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000287 def test_readline_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000288 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000289 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000290
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000291 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000292 def test_readlines_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000293 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000294 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000295
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000296 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000297 def test_iterlines_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000298 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000299 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
Tim Petersea5962f2007-03-12 18:07:52 +0000300
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000301 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000302 def test_low_compression(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000303 """Check for cases where compressed data is larger than original."""
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000304 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000305 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
306 zipfp.writestr("strfile", '12')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000307
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000308 # Get an open object for strfile
Ezio Melotti569e61f2009-12-30 06:14:51 +0000309 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
Brian Curtin0d654332011-04-19 21:15:55 -0500310 with zipfp.open("strfile") as openobj:
311 self.assertEqual(openobj.read(1), '1')
312 self.assertEqual(openobj.read(1), '2')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000313
Ezio Melottid5a23e32009-07-15 17:07:04 +0000314 def test_absolute_arcnames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000315 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
316 zipfp.write(TESTFN, "/absolute")
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000317
Ezio Melotti569e61f2009-12-30 06:14:51 +0000318 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
319 self.assertEqual(zipfp.namelist(), ["absolute"])
Tim Peters32cbc962006-02-20 21:42:18 +0000320
Ezio Melottid5a23e32009-07-15 17:07:04 +0000321 def test_append_to_zip_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000322 """Test appending to an existing zipfile."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000323 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
324 zipfp.write(TESTFN, TESTFN)
325
326 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
327 zipfp.writestr("strfile", self.data)
328 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000329
Ezio Melottid5a23e32009-07-15 17:07:04 +0000330 def test_append_to_non_zip_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000331 """Test appending to an existing file that is not a zipfile."""
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000332 # NOTE: this test fails if len(d) < 22 because of the first
333 # line "fpin.seek(-22, 2)" in _EndRecData
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000334 data = 'I am not a ZipFile!'*10
335 with open(TESTFN2, 'wb') as f:
336 f.write(data)
337
Ezio Melotti569e61f2009-12-30 06:14:51 +0000338 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
339 zipfp.write(TESTFN, TESTFN)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000340
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000341 with open(TESTFN2, 'rb') as f:
342 f.seek(len(data))
343 with zipfile.ZipFile(f, "r") as zipfp:
344 self.assertEqual(zipfp.namelist(), [TESTFN])
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000345
R David Murray873c5832011-06-09 16:01:09 -0400346 def test_ignores_newline_at_end(self):
347 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
348 zipfp.write(TESTFN, TESTFN)
349 with open(TESTFN2, 'a') as f:
350 f.write("\r\n\00\00\00")
351 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
352 self.assertIsInstance(zipfp, zipfile.ZipFile)
353
354 def test_ignores_stuff_appended_past_comments(self):
355 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
356 zipfp.comment = b"this is a comment"
357 zipfp.write(TESTFN, TESTFN)
358 with open(TESTFN2, 'a') as f:
359 f.write("abcdef\r\n")
360 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
361 self.assertIsInstance(zipfp, zipfile.ZipFile)
362 self.assertEqual(zipfp.comment, b"this is a comment")
363
Ezio Melottid5a23e32009-07-15 17:07:04 +0000364 def test_write_default_name(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000365 """Check that calling ZipFile.write without arcname specified
366 produces the expected result."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000367 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
368 zipfp.write(TESTFN)
Benjamin Peterson352eb4f2014-04-03 10:31:25 -0400369 with open(TESTFN,'r') as fid:
370 self.assertEqual(zipfp.read(TESTFN), fid.read())
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000371
Ezio Melotti1036a7f2009-09-12 14:43:43 +0000372 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000373 def test_per_file_compression(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000374 """Check that files within a Zip archive can have different
375 compression options."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000376 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
377 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
378 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
379 sinfo = zipfp.getinfo('storeme')
380 dinfo = zipfp.getinfo('deflateme')
381 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
382 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000383
Ezio Melottid5a23e32009-07-15 17:07:04 +0000384 def test_write_to_readonly(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000385 """Check that trying to call write() on a readonly ZipFile object
386 raises a RuntimeError."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000387 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
388 zipfp.writestr("somefile.txt", "bogus")
389
390 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
391 self.assertRaises(RuntimeError, zipfp.write, TESTFN)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000392
Ezio Melottid5a23e32009-07-15 17:07:04 +0000393 def test_extract(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000394 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
395 for fpath, fdata in SMALL_TEST_DATA:
396 zipfp.writestr(fpath, fdata)
Georg Brandl62416bc2008-01-07 18:47:44 +0000397
Ezio Melotti569e61f2009-12-30 06:14:51 +0000398 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
399 for fpath, fdata in SMALL_TEST_DATA:
400 writtenfile = zipfp.extract(fpath)
Georg Brandl62416bc2008-01-07 18:47:44 +0000401
Ezio Melotti569e61f2009-12-30 06:14:51 +0000402 # make sure it was written to the right place
Gregory P. Smith608cc452013-02-01 11:40:18 -0800403 correctfile = os.path.join(os.getcwd(), fpath)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000404 correctfile = os.path.normpath(correctfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000405
Ezio Melotti569e61f2009-12-30 06:14:51 +0000406 self.assertEqual(writtenfile, correctfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000407
Ezio Melotti569e61f2009-12-30 06:14:51 +0000408 # make sure correct data is in correct file
Benjamin Peterson352eb4f2014-04-03 10:31:25 -0400409 with open(writtenfile, "rb") as fid:
410 self.assertEqual(fdata, fid.read())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000411 os.remove(writtenfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000412
413 # remove the test file subdirectories
Benjamin Peterson352eb4f2014-04-03 10:31:25 -0400414 rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
Georg Brandl62416bc2008-01-07 18:47:44 +0000415
Ezio Melottid5a23e32009-07-15 17:07:04 +0000416 def test_extract_all(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000417 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
418 for fpath, fdata in SMALL_TEST_DATA:
419 zipfp.writestr(fpath, fdata)
Georg Brandl62416bc2008-01-07 18:47:44 +0000420
Ezio Melotti569e61f2009-12-30 06:14:51 +0000421 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
422 zipfp.extractall()
423 for fpath, fdata in SMALL_TEST_DATA:
Gregory P. Smith608cc452013-02-01 11:40:18 -0800424 outfile = os.path.join(os.getcwd(), fpath)
Georg Brandl62416bc2008-01-07 18:47:44 +0000425
Benjamin Peterson352eb4f2014-04-03 10:31:25 -0400426 with open(outfile, "rb") as fid:
427 self.assertEqual(fdata, fid.read())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000428 os.remove(outfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000429
430 # remove the test file subdirectories
Benjamin Peterson352eb4f2014-04-03 10:31:25 -0400431 rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
Georg Brandl62416bc2008-01-07 18:47:44 +0000432
Gregory P. Smith608cc452013-02-01 11:40:18 -0800433 def check_file(self, filename, content):
434 self.assertTrue(os.path.isfile(filename))
435 with open(filename, 'rb') as f:
436 self.assertEqual(f.read(), content)
437
Serhiy Storchakadb03e6b2013-05-08 21:52:31 +0300438 @skipUnless(TESTFN_UNICODE, "No Unicode filesystem semantics on this platform.")
Serhiy Storchaka6fa83f92013-04-13 12:28:17 +0300439 def test_extract_unicode_filenames(self):
440 fnames = [u'foo.txt', os.path.basename(TESTFN_UNICODE)]
441 content = 'Test for unicode filename'
442 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
443 for fname in fnames:
444 zipfp.writestr(fname, content)
445
446 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
447 for fname in fnames:
448 writtenfile = zipfp.extract(fname)
449
450 # make sure it was written to the right place
451 correctfile = os.path.join(os.getcwd(), fname)
452 correctfile = os.path.normpath(correctfile)
453 self.assertEqual(writtenfile, correctfile)
454
455 self.check_file(writtenfile, content)
456 os.remove(writtenfile)
457
Gregory P. Smith608cc452013-02-01 11:40:18 -0800458 def test_extract_hackers_arcnames(self):
459 hacknames = [
460 ('../foo/bar', 'foo/bar'),
461 ('foo/../bar', 'foo/bar'),
462 ('foo/../../bar', 'foo/bar'),
463 ('foo/bar/..', 'foo/bar'),
464 ('./../foo/bar', 'foo/bar'),
465 ('/foo/bar', 'foo/bar'),
466 ('/foo/../bar', 'foo/bar'),
467 ('/foo/../../bar', 'foo/bar'),
Gregory P. Smith608cc452013-02-01 11:40:18 -0800468 ]
469 if os.path.sep == '\\':
470 hacknames.extend([
471 (r'..\foo\bar', 'foo/bar'),
472 (r'..\/foo\/bar', 'foo/bar'),
473 (r'foo/\..\/bar', 'foo/bar'),
474 (r'foo\/../\bar', 'foo/bar'),
475 (r'C:foo/bar', 'foo/bar'),
476 (r'C:/foo/bar', 'foo/bar'),
477 (r'C://foo/bar', 'foo/bar'),
478 (r'C:\foo\bar', 'foo/bar'),
Benjamin Petersonc4597552014-06-22 20:26:07 -0700479 (r'//conky/mountpoint/foo/bar', 'foo/bar'),
480 (r'\\conky\mountpoint\foo\bar', 'foo/bar'),
Gregory P. Smith608cc452013-02-01 11:40:18 -0800481 (r'///conky/mountpoint/foo/bar', 'conky/mountpoint/foo/bar'),
482 (r'\\\conky\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'),
483 (r'//conky//mountpoint/foo/bar', 'conky/mountpoint/foo/bar'),
484 (r'\\conky\\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'),
Benjamin Petersonc4597552014-06-22 20:26:07 -0700485 (r'//?/C:/foo/bar', 'foo/bar'),
486 (r'\\?\C:\foo\bar', 'foo/bar'),
Gregory P. Smith608cc452013-02-01 11:40:18 -0800487 (r'C:/../C:/foo/bar', 'C_/foo/bar'),
488 (r'a:b\c<d>e|f"g?h*i', 'b/c_d_e_f_g_h_i'),
Serhiy Storchaka13e56c72013-02-02 17:46:33 +0200489 ('../../foo../../ba..r', 'foo/ba..r'),
490 ])
491 else: # Unix
492 hacknames.extend([
493 ('//foo/bar', 'foo/bar'),
494 ('../../foo../../ba..r', 'foo../ba..r'),
Serhiy Storchaka05fd7442013-02-02 18:34:57 +0200495 (r'foo/..\bar', r'foo/..\bar'),
Gregory P. Smith608cc452013-02-01 11:40:18 -0800496 ])
497
498 for arcname, fixedname in hacknames:
499 content = b'foobar' + arcname.encode()
500 with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipfp:
Serhiy Storchaka05fd7442013-02-02 18:34:57 +0200501 zinfo = zipfile.ZipInfo()
502 # preserve backslashes
503 zinfo.filename = arcname
504 zinfo.external_attr = 0o600 << 16
505 zipfp.writestr(zinfo, content)
Gregory P. Smith608cc452013-02-01 11:40:18 -0800506
Serhiy Storchaka2a051fa2013-02-02 19:25:57 +0200507 arcname = arcname.replace(os.sep, "/")
Gregory P. Smith608cc452013-02-01 11:40:18 -0800508 targetpath = os.path.join('target', 'subdir', 'subsub')
509 correctfile = os.path.join(targetpath, *fixedname.split('/'))
510
511 with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
512 writtenfile = zipfp.extract(arcname, targetpath)
Serhiy Storchaka13e56c72013-02-02 17:46:33 +0200513 self.assertEqual(writtenfile, correctfile,
514 msg="extract %r" % arcname)
Gregory P. Smith608cc452013-02-01 11:40:18 -0800515 self.check_file(correctfile, content)
Benjamin Peterson352eb4f2014-04-03 10:31:25 -0400516 rmtree('target')
Gregory P. Smith608cc452013-02-01 11:40:18 -0800517
518 with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
519 zipfp.extractall(targetpath)
520 self.check_file(correctfile, content)
Benjamin Peterson352eb4f2014-04-03 10:31:25 -0400521 rmtree('target')
Gregory P. Smith608cc452013-02-01 11:40:18 -0800522
523 correctfile = os.path.join(os.getcwd(), *fixedname.split('/'))
524
525 with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
526 writtenfile = zipfp.extract(arcname)
Serhiy Storchaka13e56c72013-02-02 17:46:33 +0200527 self.assertEqual(writtenfile, correctfile,
528 msg="extract %r" % arcname)
Gregory P. Smith608cc452013-02-01 11:40:18 -0800529 self.check_file(correctfile, content)
Benjamin Peterson352eb4f2014-04-03 10:31:25 -0400530 rmtree(fixedname.split('/')[0])
Gregory P. Smith608cc452013-02-01 11:40:18 -0800531
532 with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
533 zipfp.extractall()
534 self.check_file(correctfile, content)
Benjamin Peterson352eb4f2014-04-03 10:31:25 -0400535 rmtree(fixedname.split('/')[0])
Gregory P. Smith608cc452013-02-01 11:40:18 -0800536
537 os.remove(TESTFN2)
538
Ronald Oussorendd25e862010-02-07 20:18:02 +0000539 def test_writestr_compression(self):
540 zipfp = zipfile.ZipFile(TESTFN2, "w")
541 zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED)
542 if zlib:
543 zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED)
544
545 info = zipfp.getinfo('a.txt')
546 self.assertEqual(info.compress_type, zipfile.ZIP_STORED)
547
548 if zlib:
549 info = zipfp.getinfo('b.txt')
550 self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED)
551
552
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000553 def zip_test_writestr_permissions(self, f, compression):
554 # Make sure that writestr creates files with mode 0600,
555 # when it is passed a name rather than a ZipInfo instance.
556
Ezio Melottid5a23e32009-07-15 17:07:04 +0000557 self.make_test_archive(f, compression)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000558 with zipfile.ZipFile(f, "r") as zipfp:
559 zinfo = zipfp.getinfo('strfile')
560 self.assertEqual(zinfo.external_attr, 0600 << 16)
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000561
Ezio Melottid5a23e32009-07-15 17:07:04 +0000562 def test_writestr_permissions(self):
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000563 for f in (TESTFN2, TemporaryFile(), StringIO()):
564 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
565
Ezio Melotti569e61f2009-12-30 06:14:51 +0000566 def test_close(self):
567 """Check that the zipfile is closed after the 'with' block."""
568 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
569 for fpath, fdata in SMALL_TEST_DATA:
570 zipfp.writestr(fpath, fdata)
571 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
572 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
573
574 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
575 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
576 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
577
578 def test_close_on_exception(self):
579 """Check that the zipfile is closed if an exception is raised in the
580 'with' block."""
581 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
582 for fpath, fdata in SMALL_TEST_DATA:
583 zipfp.writestr(fpath, fdata)
584
585 try:
586 with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
587 raise zipfile.BadZipfile()
588 except zipfile.BadZipfile:
589 self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
590
Senthil Kumaranddd40312011-10-20 01:38:35 +0800591 def test_add_file_before_1980(self):
592 # Set atime and mtime to 1970-01-01
593 os.utime(TESTFN, (0, 0))
594 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
595 self.assertRaises(ValueError, zipfp.write, TESTFN)
596
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000597 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000598 unlink(TESTFN)
599 unlink(TESTFN2)
600
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000601
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000602class TestZip64InSmallFiles(unittest.TestCase):
603 # These tests test the ZIP64 functionality without using large files,
604 # see test_zipfile64 for proper tests.
605
606 def setUp(self):
607 self._limit = zipfile.ZIP64_LIMIT
Serhiy Storchaka1af262c2014-09-23 22:26:45 +0300608 self._filecount_limit = zipfile.ZIP_FILECOUNT_LIMIT
609 zipfile.ZIP64_LIMIT = 1000
610 zipfile.ZIP_FILECOUNT_LIMIT = 9
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000611
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000612 line_gen = ("Test of zipfile line %d." % i
613 for i in range(0, FIXEDTEST_SIZE))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000614 self.data = '\n'.join(line_gen)
615
616 # Make a source file with some lines
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000617 with open(TESTFN, "wb") as fp:
618 fp.write(self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000619
Ezio Melottid5a23e32009-07-15 17:07:04 +0000620 def large_file_exception_test(self, f, compression):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000621 with zipfile.ZipFile(f, "w", compression) as zipfp:
622 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000623 zipfp.write, TESTFN, "another.name")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000624
Ezio Melottid5a23e32009-07-15 17:07:04 +0000625 def large_file_exception_test2(self, f, compression):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000626 with zipfile.ZipFile(f, "w", compression) as zipfp:
627 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000628 zipfp.writestr, "another.name", self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000629
Ezio Melottid5a23e32009-07-15 17:07:04 +0000630 def test_large_file_exception(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000631 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000632 self.large_file_exception_test(f, zipfile.ZIP_STORED)
633 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000634
Ezio Melottid5a23e32009-07-15 17:07:04 +0000635 def zip_test(self, f, compression):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000636 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000637 with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000638 zipfp.write(TESTFN, "another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +0000639 zipfp.write(TESTFN, TESTFN)
640 zipfp.writestr("strfile", self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000641
642 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000643 with zipfile.ZipFile(f, "r", compression) as zipfp:
644 self.assertEqual(zipfp.read(TESTFN), self.data)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000645 self.assertEqual(zipfp.read("another.name"), self.data)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000646 self.assertEqual(zipfp.read("strfile"), self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000647
Ezio Melotti569e61f2009-12-30 06:14:51 +0000648 # Print the ZIP directory
649 fp = StringIO()
650 stdout = sys.stdout
651 try:
652 sys.stdout = fp
653 zipfp.printdir()
654 finally:
655 sys.stdout = stdout
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000656
Ezio Melotti569e61f2009-12-30 06:14:51 +0000657 directory = fp.getvalue()
658 lines = directory.splitlines()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000659 self.assertEqual(len(lines), 4) # Number of files + header
Tim Petersa608bb22006-06-15 18:06:29 +0000660
Ezio Melottiaa980582010-01-23 23:04:36 +0000661 self.assertIn('File Name', lines[0])
662 self.assertIn('Modified', lines[0])
663 self.assertIn('Size', lines[0])
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000664
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000665 fn, date, time_, size = lines[1].split()
666 self.assertEqual(fn, 'another.name')
667 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
668 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
669 self.assertEqual(size, str(len(self.data)))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000670
Ezio Melotti569e61f2009-12-30 06:14:51 +0000671 # Check the namelist
672 names = zipfp.namelist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000673 self.assertEqual(len(names), 3)
Ezio Melottiaa980582010-01-23 23:04:36 +0000674 self.assertIn(TESTFN, names)
675 self.assertIn("another.name", names)
676 self.assertIn("strfile", names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000677
Ezio Melotti569e61f2009-12-30 06:14:51 +0000678 # Check infolist
679 infos = zipfp.infolist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000680 names = [i.filename for i in infos]
681 self.assertEqual(len(names), 3)
Ezio Melottiaa980582010-01-23 23:04:36 +0000682 self.assertIn(TESTFN, names)
683 self.assertIn("another.name", names)
684 self.assertIn("strfile", names)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000685 for i in infos:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000686 self.assertEqual(i.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000687
Ezio Melotti569e61f2009-12-30 06:14:51 +0000688 # check getinfo
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000689 for nm in (TESTFN, "another.name", "strfile"):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000690 info = zipfp.getinfo(nm)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000691 self.assertEqual(info.filename, nm)
692 self.assertEqual(info.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000693
Ezio Melotti569e61f2009-12-30 06:14:51 +0000694 # Check that testzip doesn't raise an exception
695 zipfp.testzip()
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000696
Ezio Melottid5a23e32009-07-15 17:07:04 +0000697 def test_stored(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000698 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000699 self.zip_test(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000700
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000701 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000702 def test_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000703 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000704 self.zip_test(f, zipfile.ZIP_DEFLATED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000705
Ezio Melottid5a23e32009-07-15 17:07:04 +0000706 def test_absolute_arcnames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000707 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
708 allowZip64=True) as zipfp:
709 zipfp.write(TESTFN, "/absolute")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000710
Ezio Melotti569e61f2009-12-30 06:14:51 +0000711 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
712 self.assertEqual(zipfp.namelist(), ["absolute"])
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000713
Serhiy Storchaka1af262c2014-09-23 22:26:45 +0300714 def test_too_many_files(self):
715 # This test checks that more than 64k files can be added to an archive,
716 # and that the resulting archive can be read properly by ZipFile
717 zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=True)
718 zipf.debug = 100
719 numfiles = 15
720 for i in range(numfiles):
721 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
722 self.assertEqual(len(zipf.namelist()), numfiles)
723 zipf.close()
724
725 zipf2 = zipfile.ZipFile(TESTFN, mode="r")
726 self.assertEqual(len(zipf2.namelist()), numfiles)
727 for i in range(numfiles):
728 content = zipf2.read("foo%08d" % i)
729 self.assertEqual(content, "%d" % (i**3 % 57))
730 zipf2.close()
731
732 def test_too_many_files_append(self):
733 zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=False)
734 zipf.debug = 100
735 numfiles = 9
736 for i in range(numfiles):
737 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
738 self.assertEqual(len(zipf.namelist()), numfiles)
739 with self.assertRaises(zipfile.LargeZipFile):
740 zipf.writestr("foo%08d" % numfiles, b'')
741 self.assertEqual(len(zipf.namelist()), numfiles)
742 zipf.close()
743
744 zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=False)
745 zipf.debug = 100
746 self.assertEqual(len(zipf.namelist()), numfiles)
747 with self.assertRaises(zipfile.LargeZipFile):
748 zipf.writestr("foo%08d" % numfiles, b'')
749 self.assertEqual(len(zipf.namelist()), numfiles)
750 zipf.close()
751
752 zipf = zipfile.ZipFile(TESTFN, mode="a", allowZip64=True)
753 zipf.debug = 100
754 self.assertEqual(len(zipf.namelist()), numfiles)
755 numfiles2 = 15
756 for i in range(numfiles, numfiles2):
757 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
758 self.assertEqual(len(zipf.namelist()), numfiles2)
759 zipf.close()
760
761 zipf2 = zipfile.ZipFile(TESTFN, mode="r")
762 self.assertEqual(len(zipf2.namelist()), numfiles2)
763 for i in range(numfiles2):
764 content = zipf2.read("foo%08d" % i)
765 self.assertEqual(content, "%d" % (i**3 % 57))
766 zipf2.close()
767
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000768 def tearDown(self):
769 zipfile.ZIP64_LIMIT = self._limit
Serhiy Storchaka1af262c2014-09-23 22:26:45 +0300770 zipfile.ZIP_FILECOUNT_LIMIT = self._filecount_limit
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000771 unlink(TESTFN)
772 unlink(TESTFN2)
773
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000774
775class PyZipFileTests(unittest.TestCase):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000776 def test_write_pyfile(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000777 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
778 fn = __file__
779 if fn.endswith('.pyc') or fn.endswith('.pyo'):
780 fn = fn[:-1]
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000781
Ezio Melotti569e61f2009-12-30 06:14:51 +0000782 zipfp.writepy(fn)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000783
Ezio Melotti569e61f2009-12-30 06:14:51 +0000784 bn = os.path.basename(fn)
Ezio Melottiaa980582010-01-23 23:04:36 +0000785 self.assertNotIn(bn, zipfp.namelist())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000786 self.assertTrue(bn + 'o' in zipfp.namelist() or
787 bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000788
Ezio Melotti569e61f2009-12-30 06:14:51 +0000789 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
790 fn = __file__
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000791 if fn.endswith(('.pyc', '.pyo')):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000792 fn = fn[:-1]
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000793
Ezio Melotti569e61f2009-12-30 06:14:51 +0000794 zipfp.writepy(fn, "testpackage")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000795
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000796 bn = "%s/%s" % ("testpackage", os.path.basename(fn))
Ezio Melottiaa980582010-01-23 23:04:36 +0000797 self.assertNotIn(bn, zipfp.namelist())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000798 self.assertTrue(bn + 'o' in zipfp.namelist() or
799 bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000800
Ezio Melottid5a23e32009-07-15 17:07:04 +0000801 def test_write_python_package(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000802 import email
803 packagedir = os.path.dirname(email.__file__)
804
Ezio Melotti569e61f2009-12-30 06:14:51 +0000805 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
806 zipfp.writepy(packagedir)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000807
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000808 # Check for a couple of modules at different levels of the
809 # hierarchy
Ezio Melotti569e61f2009-12-30 06:14:51 +0000810 names = zipfp.namelist()
811 self.assertTrue('email/__init__.pyo' in names or
812 'email/__init__.pyc' in names)
813 self.assertTrue('email/mime/text.pyo' in names or
814 'email/mime/text.pyc' in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000815
Ezio Melottid5a23e32009-07-15 17:07:04 +0000816 def test_write_python_directory(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000817 os.mkdir(TESTFN2)
818 try:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000819 with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
Ezio Melotti763f1e82009-12-31 13:27:41 +0000820 fp.write("print(42)\n")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000821
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000822 with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
Ezio Melotti763f1e82009-12-31 13:27:41 +0000823 fp.write("print(42 * 42)\n")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000824
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000825 with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp:
826 fp.write("bla bla bla\n")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000827
828 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
829 zipfp.writepy(TESTFN2)
830
831 names = zipfp.namelist()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000832 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
833 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
Ezio Melottiaa980582010-01-23 23:04:36 +0000834 self.assertNotIn('mod2.txt', names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000835
836 finally:
Benjamin Peterson352eb4f2014-04-03 10:31:25 -0400837 rmtree(TESTFN2)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000838
Ezio Melottid5a23e32009-07-15 17:07:04 +0000839 def test_write_non_pyfile(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000840 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
Benjamin Peterson352eb4f2014-04-03 10:31:25 -0400841 with open(TESTFN, 'w') as fid:
842 fid.write('most definitely not a python file')
Ezio Melotti569e61f2009-12-30 06:14:51 +0000843 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
844 os.remove(TESTFN)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000845
846
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000847class OtherTests(unittest.TestCase):
Antoine Pitroue1436d12010-08-12 15:25:51 +0000848 zips_with_bad_crc = {
849 zipfile.ZIP_STORED: (
850 b'PK\003\004\024\0\0\0\0\0 \213\212;:r'
851 b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af'
852 b'ilehello,AworldP'
853 b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:'
854 b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0'
855 b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi'
856 b'lePK\005\006\0\0\0\0\001\0\001\0003\000'
857 b'\0\0/\0\0\0\0\0'),
858 zipfile.ZIP_DEFLATED: (
859 b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA'
860 b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
861 b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0'
862 b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n'
863 b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05'
864 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00'
865 b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00'
866 b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'),
867 }
868
Ezio Melottid5a23e32009-07-15 17:07:04 +0000869 def test_unicode_filenames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000870 with zipfile.ZipFile(TESTFN, "w") as zf:
871 zf.writestr(u"foo.txt", "Test for unicode filename")
872 zf.writestr(u"\xf6.txt", "Test for unicode filename")
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000873 self.assertIsInstance(zf.infolist()[0].filename, unicode)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000874
875 with zipfile.ZipFile(TESTFN, "r") as zf:
876 self.assertEqual(zf.filelist[0].filename, "foo.txt")
877 self.assertEqual(zf.filelist[1].filename, u"\xf6.txt")
Martin v. Löwis471617d2008-05-05 17:16:58 +0000878
Ezio Melottid5a23e32009-07-15 17:07:04 +0000879 def test_create_non_existent_file_for_append(self):
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000880 if os.path.exists(TESTFN):
881 os.unlink(TESTFN)
Tim Petersea5962f2007-03-12 18:07:52 +0000882
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000883 filename = 'testfile.txt'
884 content = 'hello, world. this is some content.'
Tim Petersea5962f2007-03-12 18:07:52 +0000885
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000886 try:
Ezio Melotti569e61f2009-12-30 06:14:51 +0000887 with zipfile.ZipFile(TESTFN, 'a') as zf:
888 zf.writestr(filename, content)
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000889 except IOError:
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000890 self.fail('Could not append data to a non-existent zip file.')
891
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000892 self.assertTrue(os.path.exists(TESTFN))
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000893
Ezio Melotti569e61f2009-12-30 06:14:51 +0000894 with zipfile.ZipFile(TESTFN, 'r') as zf:
895 self.assertEqual(zf.read(filename), content)
Tim Petersea5962f2007-03-12 18:07:52 +0000896
Ezio Melottid5a23e32009-07-15 17:07:04 +0000897 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000898 # This test checks that the ZipFile constructor closes the file object
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000899 # it opens if there's an error in the file. If it doesn't, the
900 # traceback holds a reference to the ZipFile object and, indirectly,
901 # the file object.
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000902 # On Windows, this causes the os.unlink() call to fail because the
903 # underlying file is still open. This is SF bug #412214.
904 #
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000905 with open(TESTFN, "w") as fp:
906 fp.write("this is not a legal zip file\n")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000907 try:
908 zf = zipfile.ZipFile(TESTFN)
909 except zipfile.BadZipfile:
Collin Winter04a51ec2007-03-29 02:28:16 +0000910 pass
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000911
Ezio Melottid5a23e32009-07-15 17:07:04 +0000912 def test_is_zip_erroneous_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000913 """Check that is_zipfile() correctly identifies non-zip files."""
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000914 # - passing a filename
915 with open(TESTFN, "w") as fp:
916 fp.write("this is not a legal zip file\n")
Tim Petersea5962f2007-03-12 18:07:52 +0000917 chk = zipfile.is_zipfile(TESTFN)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000918 self.assertFalse(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000919 # - passing a file object
920 with open(TESTFN, "rb") as fp:
921 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000922 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000923 # - passing a file-like object
924 fp = StringIO()
925 fp.write("this is not a legal zip file\n")
926 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000927 self.assertTrue(not chk)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000928 fp.seek(0, 0)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000929 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000930 self.assertTrue(not chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000931
Serhiy Storchaka0be506a2013-01-31 15:26:55 +0200932 def test_damaged_zipfile(self):
933 """Check that zipfiles with missing bytes at the end raise BadZipFile."""
934 # - Create a valid zip file
935 fp = io.BytesIO()
936 with zipfile.ZipFile(fp, mode="w") as zipf:
937 zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
938 zipfiledata = fp.getvalue()
939
940 # - Now create copies of it missing the last N bytes and make sure
941 # a BadZipFile exception is raised when we try to open it
942 for N in range(len(zipfiledata)):
943 fp = io.BytesIO(zipfiledata[:N])
944 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, fp)
945
Ezio Melottid5a23e32009-07-15 17:07:04 +0000946 def test_is_zip_valid_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000947 """Check that is_zipfile() correctly identifies zip files."""
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000948 # - passing a filename
Ezio Melotti569e61f2009-12-30 06:14:51 +0000949 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
950 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Tim Petersea5962f2007-03-12 18:07:52 +0000951 chk = zipfile.is_zipfile(TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000952 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000953 # - passing a file object
954 with open(TESTFN, "rb") as fp:
955 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000956 self.assertTrue(chk)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000957 fp.seek(0, 0)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000958 zip_contents = fp.read()
959 # - passing a file-like object
960 fp = StringIO()
961 fp.write(zip_contents)
962 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000963 self.assertTrue(chk)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000964 fp.seek(0, 0)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000965 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000966 self.assertTrue(chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000967
Ezio Melottid5a23e32009-07-15 17:07:04 +0000968 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000969 # make sure we don't raise an AttributeError when a partially-constructed
970 # ZipFile instance is finalized; this tests for regression on SF tracker
971 # bug #403871.
972
973 # The bug we're testing for caused an AttributeError to be raised
974 # when a ZipFile instance was created for a file that did not
975 # exist; the .fp member was not initialized but was needed by the
976 # __del__() method. Since the AttributeError is in the __del__(),
977 # it is ignored, but the user should be sufficiently annoyed by
978 # the message on the output that regression will be noticed
979 # quickly.
980 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
981
Amaury Forgeot d'Arc3e5b0272009-07-28 22:15:30 +0000982 def test_empty_file_raises_BadZipFile(self):
Brian Curtin0d654332011-04-19 21:15:55 -0500983 with open(TESTFN, 'w') as f:
984 pass
Amaury Forgeot d'Arc3e5b0272009-07-28 22:15:30 +0000985 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
986
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000987 with open(TESTFN, 'w') as fp:
988 fp.write("short file")
Amaury Forgeot d'Arc3e5b0272009-07-28 22:15:30 +0000989 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
990
Ezio Melottid5a23e32009-07-15 17:07:04 +0000991 def test_closed_zip_raises_RuntimeError(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000992 """Verify that testzip() doesn't swallow inappropriate exceptions."""
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000993 data = StringIO()
Ezio Melotti569e61f2009-12-30 06:14:51 +0000994 with zipfile.ZipFile(data, mode="w") as zipf:
995 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000996
Andrew Svetlov4bb142b2012-12-18 21:27:37 +0200997 # This is correct; calling .read on a closed ZipFile should raise
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000998 # a RuntimeError, and so should calling .testzip. An earlier
999 # version of .testzip would swallow this exception (and any other)
1000 # and report that the first file in the archive was corrupt.
Georg Brandl4b3ab6f2007-07-12 09:59:22 +00001001 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
1002 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001003 self.assertRaises(RuntimeError, zipf.testzip)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +00001004 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Benjamin Peterson352eb4f2014-04-03 10:31:25 -04001005 with open(TESTFN, 'w') as fid:
1006 fid.write('zipfile test data')
1007 self.assertRaises(RuntimeError, zipf.write, TESTFN)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +00001008
Ezio Melottid5a23e32009-07-15 17:07:04 +00001009 def test_bad_constructor_mode(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001010 """Check that bad modes passed to ZipFile constructor are caught."""
Georg Brandl4b3ab6f2007-07-12 09:59:22 +00001011 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
1012
Ezio Melottid5a23e32009-07-15 17:07:04 +00001013 def test_bad_open_mode(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001014 """Check that bad modes passed to ZipFile.open are caught."""
Ezio Melotti569e61f2009-12-30 06:14:51 +00001015 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1016 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1017
1018 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
Georg Brandl4b3ab6f2007-07-12 09:59:22 +00001019 # read the data to make sure the file is there
Ezio Melotti569e61f2009-12-30 06:14:51 +00001020 zipf.read("foo.txt")
1021 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +00001022
Ezio Melottid5a23e32009-07-15 17:07:04 +00001023 def test_read0(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001024 """Check that calling read(0) on a ZipExtFile object returns an empty
1025 string and doesn't advance file pointer."""
Ezio Melotti569e61f2009-12-30 06:14:51 +00001026 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1027 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1028 # read the data to make sure the file is there
Brian Curtin0d654332011-04-19 21:15:55 -05001029 with zipf.open("foo.txt") as f:
1030 for i in xrange(FIXEDTEST_SIZE):
1031 self.assertEqual(f.read(0), '')
Georg Brandl4b3ab6f2007-07-12 09:59:22 +00001032
Brian Curtin0d654332011-04-19 21:15:55 -05001033 self.assertEqual(f.read(), "O, for a Muse of Fire!")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +00001034
Ezio Melottid5a23e32009-07-15 17:07:04 +00001035 def test_open_non_existent_item(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001036 """Check that attempting to call open() for an item that doesn't
1037 exist in the archive raises a RuntimeError."""
Ezio Melotti569e61f2009-12-30 06:14:51 +00001038 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1039 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +00001040
Ezio Melottid5a23e32009-07-15 17:07:04 +00001041 def test_bad_compression_mode(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001042 """Check that bad compression methods passed to ZipFile.open are
1043 caught."""
Georg Brandl4b3ab6f2007-07-12 09:59:22 +00001044 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
1045
Ezio Melotti9e949722012-11-18 13:18:06 +02001046 def test_unsupported_compression(self):
1047 # data is declared as shrunk, but actually deflated
1048 data = (b'PK\x03\x04.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00'
1049 b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01'
1050 b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00'
1051 b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
1052 b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00'
1053 b'/\x00\x00\x00!\x00\x00\x00\x00\x00')
1054 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
1055 self.assertRaises(NotImplementedError, zipf.open, 'x')
1056
Ezio Melottid5a23e32009-07-15 17:07:04 +00001057 def test_null_byte_in_filename(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001058 """Check that a filename containing a null byte is properly
1059 terminated."""
Ezio Melotti569e61f2009-12-30 06:14:51 +00001060 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1061 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
1062 self.assertEqual(zipf.namelist(), ['foo.txt'])
Neal Norwitz0d4c06e2007-04-25 06:30:05 +00001063
Ezio Melottid5a23e32009-07-15 17:07:04 +00001064 def test_struct_sizes(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001065 """Check that ZIP internal structure sizes are calculated correctly."""
Martin v. Löwis8c436412008-07-03 12:51:14 +00001066 self.assertEqual(zipfile.sizeEndCentDir, 22)
1067 self.assertEqual(zipfile.sizeCentralDir, 46)
1068 self.assertEqual(zipfile.sizeEndCentDir64, 56)
1069 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
1070
Ezio Melottid5a23e32009-07-15 17:07:04 +00001071 def test_comments(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001072 """Check that comments on the archive are handled properly."""
Martin v. Löwis8c436412008-07-03 12:51:14 +00001073
1074 # check default comment is empty
Ezio Melotti569e61f2009-12-30 06:14:51 +00001075 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1076 self.assertEqual(zipf.comment, '')
1077 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1078
1079 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
1080 self.assertEqual(zipf.comment, '')
Martin v. Löwis8c436412008-07-03 12:51:14 +00001081
1082 # check a simple short comment
1083 comment = 'Bravely taking to his feet, he beat a very brave retreat.'
Ezio Melotti569e61f2009-12-30 06:14:51 +00001084 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1085 zipf.comment = comment
1086 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1087 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
1088 self.assertEqual(zipf.comment, comment)
Martin v. Löwis8c436412008-07-03 12:51:14 +00001089
1090 # check a comment of max length
1091 comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)])
Ezio Melotti569e61f2009-12-30 06:14:51 +00001092 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1093 zipf.comment = comment2
1094 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1095
1096 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
1097 self.assertEqual(zipf.comment, comment2)
Martin v. Löwis8c436412008-07-03 12:51:14 +00001098
1099 # check a comment that is too long is truncated
Ezio Melotti569e61f2009-12-30 06:14:51 +00001100 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
Serhiy Storchaka49259352014-01-20 21:57:09 +02001101 with check_warnings(('', UserWarning)):
1102 zipf.comment = comment2 + 'oops'
Ezio Melotti569e61f2009-12-30 06:14:51 +00001103 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1104 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
1105 self.assertEqual(zipf.comment, comment2)
Martin v. Löwis8c436412008-07-03 12:51:14 +00001106
R David Murray3f4ccba2012-04-12 18:42:47 -04001107 def test_change_comment_in_empty_archive(self):
1108 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
1109 self.assertFalse(zipf.filelist)
1110 zipf.comment = b"this is a comment"
1111 with zipfile.ZipFile(TESTFN, "r") as zipf:
1112 self.assertEqual(zipf.comment, b"this is a comment")
1113
1114 def test_change_comment_in_nonempty_archive(self):
1115 with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
1116 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
1117 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
1118 self.assertTrue(zipf.filelist)
1119 zipf.comment = b"this is a comment"
1120 with zipfile.ZipFile(TESTFN, "r") as zipf:
1121 self.assertEqual(zipf.comment, b"this is a comment")
1122
Antoine Pitroue1436d12010-08-12 15:25:51 +00001123 def check_testzip_with_bad_crc(self, compression):
1124 """Tests that files with bad CRCs return their name from testzip."""
1125 zipdata = self.zips_with_bad_crc[compression]
1126
1127 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
1128 # testzip returns the name of the first corrupt file, or None
1129 self.assertEqual('afile', zipf.testzip())
1130
1131 def test_testzip_with_bad_crc_stored(self):
1132 self.check_testzip_with_bad_crc(zipfile.ZIP_STORED)
1133
1134 @skipUnless(zlib, "requires zlib")
1135 def test_testzip_with_bad_crc_deflated(self):
1136 self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED)
1137
1138 def check_read_with_bad_crc(self, compression):
1139 """Tests that files with bad CRCs raise a BadZipfile exception when read."""
1140 zipdata = self.zips_with_bad_crc[compression]
1141
1142 # Using ZipFile.read()
1143 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
1144 self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile')
1145
1146 # Using ZipExtFile.read()
1147 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
1148 with zipf.open('afile', 'r') as corrupt_file:
1149 self.assertRaises(zipfile.BadZipfile, corrupt_file.read)
1150
1151 # Same with small reads (in order to exercise the buffering logic)
1152 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
1153 with zipf.open('afile', 'r') as corrupt_file:
1154 corrupt_file.MIN_READ_SIZE = 2
1155 with self.assertRaises(zipfile.BadZipfile):
1156 while corrupt_file.read(2):
1157 pass
1158
1159 def test_read_with_bad_crc_stored(self):
1160 self.check_read_with_bad_crc(zipfile.ZIP_STORED)
1161
1162 @skipUnless(zlib, "requires zlib")
1163 def test_read_with_bad_crc_deflated(self):
1164 self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED)
1165
Antoine Pitroue4195e82010-09-12 14:56:27 +00001166 def check_read_return_size(self, compression):
1167 # Issue #9837: ZipExtFile.read() shouldn't return more bytes
1168 # than requested.
1169 for test_size in (1, 4095, 4096, 4097, 16384):
1170 file_size = test_size + 1
1171 junk = b''.join(struct.pack('B', randint(0, 255))
1172 for x in range(file_size))
1173 with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf:
1174 zipf.writestr('foo', junk)
1175 with zipf.open('foo', 'r') as fp:
1176 buf = fp.read(test_size)
1177 self.assertEqual(len(buf), test_size)
1178
1179 def test_read_return_size_stored(self):
1180 self.check_read_return_size(zipfile.ZIP_STORED)
1181
1182 @skipUnless(zlib, "requires zlib")
1183 def test_read_return_size_deflated(self):
1184 self.check_read_return_size(zipfile.ZIP_DEFLATED)
1185
Georg Brandl86e0c892010-11-26 07:22:28 +00001186 def test_empty_zipfile(self):
1187 # Check that creating a file in 'w' or 'a' mode and closing without
1188 # adding any files to the archives creates a valid empty ZIP file
Brian Curtin0d654332011-04-19 21:15:55 -05001189 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
1190 pass
Georg Brandl86e0c892010-11-26 07:22:28 +00001191 try:
1192 zipf = zipfile.ZipFile(TESTFN, mode="r")
Benjamin Peterson352eb4f2014-04-03 10:31:25 -04001193 zipf.close()
Éric Araujo67843b32011-02-02 17:03:38 +00001194 except zipfile.BadZipfile:
Georg Brandl86e0c892010-11-26 07:22:28 +00001195 self.fail("Unable to create empty ZIP file in 'w' mode")
1196
Brian Curtin0d654332011-04-19 21:15:55 -05001197 with zipfile.ZipFile(TESTFN, mode="a") as zipf:
1198 pass
Georg Brandl86e0c892010-11-26 07:22:28 +00001199 try:
1200 zipf = zipfile.ZipFile(TESTFN, mode="r")
Benjamin Peterson352eb4f2014-04-03 10:31:25 -04001201 zipf.close()
Georg Brandl86e0c892010-11-26 07:22:28 +00001202 except:
1203 self.fail("Unable to create empty ZIP file in 'a' mode")
1204
1205 def test_open_empty_file(self):
1206 # Issue 1710703: Check that opening a file with less than 22 bytes
1207 # raises a BadZipfile exception (rather than the previously unhelpful
1208 # IOError)
Brian Curtin0d654332011-04-19 21:15:55 -05001209 with open(TESTFN, 'w') as f:
1210 pass
Georg Brandl86e0c892010-11-26 07:22:28 +00001211 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r')
1212
Senthil Kumaranddd40312011-10-20 01:38:35 +08001213 def test_create_zipinfo_before_1980(self):
1214 self.assertRaises(ValueError,
1215 zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0))
1216
Gregory P. Smith0344a062014-05-29 23:41:52 -07001217 def test_zipfile_with_short_extra_field(self):
1218 """If an extra field in the header is less than 4 bytes, skip it."""
1219 zipdata = (
1220 b'PK\x03\x04\x14\x00\x00\x00\x00\x00\x93\x9b\xad@\x8b\x9e'
1221 b'\xd9\xd3\x01\x00\x00\x00\x01\x00\x00\x00\x03\x00\x03\x00ab'
1222 b'c\x00\x00\x00APK\x01\x02\x14\x03\x14\x00\x00\x00\x00'
1223 b'\x00\x93\x9b\xad@\x8b\x9e\xd9\xd3\x01\x00\x00\x00\x01\x00\x00'
1224 b'\x00\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00'
1225 b'\x00\x00\x00abc\x00\x00PK\x05\x06\x00\x00\x00\x00'
1226 b'\x01\x00\x01\x003\x00\x00\x00%\x00\x00\x00\x00\x00'
1227 )
1228 with zipfile.ZipFile(io.BytesIO(zipdata), 'r') as zipf:
1229 # testzip returns the name of the first corrupt file, or None
1230 self.assertIsNone(zipf.testzip())
1231
Collin Winter04a51ec2007-03-29 02:28:16 +00001232 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001233 unlink(TESTFN)
1234 unlink(TESTFN2)
1235
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001236
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001237class DecryptionTests(unittest.TestCase):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001238 """Check that ZIP decryption works. Since the library does not
1239 support encryption at the moment, we use a pre-generated encrypted
1240 ZIP file."""
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001241
1242 data = (
1243 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
1244 '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
1245 '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
1246 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
1247 '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
1248 '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
1249 '\x00\x00L\x00\x00\x00\x00\x00' )
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001250 data2 = (
1251 'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
1252 '\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
1253 '\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
1254 'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
1255 '\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
1256 '\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
1257 'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
1258 '\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001259
1260 plain = 'zipfile.py encryption test'
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001261 plain2 = '\x00'*512
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001262
1263 def setUp(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001264 with open(TESTFN, "wb") as fp:
1265 fp.write(self.data)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001266 self.zip = zipfile.ZipFile(TESTFN, "r")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001267 with open(TESTFN2, "wb") as fp:
1268 fp.write(self.data2)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001269 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001270
1271 def tearDown(self):
1272 self.zip.close()
1273 os.unlink(TESTFN)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001274 self.zip2.close()
1275 os.unlink(TESTFN2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001276
Ezio Melottid5a23e32009-07-15 17:07:04 +00001277 def test_no_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001278 # Reading the encrypted file without password
1279 # must generate a RunTime exception
1280 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001281 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001282
Ezio Melottid5a23e32009-07-15 17:07:04 +00001283 def test_bad_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001284 self.zip.setpassword("perl")
1285 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001286 self.zip2.setpassword("perl")
1287 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Tim Petersea5962f2007-03-12 18:07:52 +00001288
Ezio Melotti1036a7f2009-09-12 14:43:43 +00001289 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001290 def test_good_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001291 self.zip.setpassword("python")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001292 self.assertEqual(self.zip.read("test.txt"), self.plain)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +00001293 self.zip2.setpassword("12345")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001294 self.assertEqual(self.zip2.read("zero"), self.plain2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +00001295
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001296
1297class TestsWithRandomBinaryFiles(unittest.TestCase):
1298 def setUp(self):
1299 datacount = randint(16, 64)*1024 + randint(1, 1024)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001300 self.data = ''.join(struct.pack('<f', random()*randint(-1000, 1000))
Ezio Melotti763f1e82009-12-31 13:27:41 +00001301 for i in xrange(datacount))
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001302
1303 # Make a source file with some lines
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001304 with open(TESTFN, "wb") as fp:
1305 fp.write(self.data)
Neal Norwitz0d4c06e2007-04-25 06:30:05 +00001306
Collin Winter04a51ec2007-03-29 02:28:16 +00001307 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001308 unlink(TESTFN)
1309 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001310
Ezio Melottid5a23e32009-07-15 17:07:04 +00001311 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001312 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001313 with zipfile.ZipFile(f, "w", compression) as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001314 zipfp.write(TESTFN, "another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +00001315 zipfp.write(TESTFN, TESTFN)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001316
Ezio Melottid5a23e32009-07-15 17:07:04 +00001317 def zip_test(self, f, compression):
1318 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001319
1320 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001321 with zipfile.ZipFile(f, "r", compression) as zipfp:
1322 testdata = zipfp.read(TESTFN)
1323 self.assertEqual(len(testdata), len(self.data))
1324 self.assertEqual(testdata, self.data)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001325 self.assertEqual(zipfp.read("another.name"), self.data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001326
Ezio Melottid5a23e32009-07-15 17:07:04 +00001327 def test_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001328 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001329 self.zip_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001330
Antoine Pitroue1436d12010-08-12 15:25:51 +00001331 @skipUnless(zlib, "requires zlib")
1332 def test_deflated(self):
1333 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1334 self.zip_test(f, zipfile.ZIP_DEFLATED)
1335
Ezio Melottid5a23e32009-07-15 17:07:04 +00001336 def zip_open_test(self, f, compression):
1337 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001338
1339 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001340 with zipfile.ZipFile(f, "r", compression) as zipfp:
1341 zipdata1 = []
Brian Curtin0d654332011-04-19 21:15:55 -05001342 with zipfp.open(TESTFN) as zipopen1:
1343 while True:
1344 read_data = zipopen1.read(256)
1345 if not read_data:
1346 break
1347 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001348
Ezio Melotti569e61f2009-12-30 06:14:51 +00001349 zipdata2 = []
Brian Curtin0d654332011-04-19 21:15:55 -05001350 with zipfp.open("another.name") as zipopen2:
1351 while True:
1352 read_data = zipopen2.read(256)
1353 if not read_data:
1354 break
1355 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +00001356
Ezio Melotti569e61f2009-12-30 06:14:51 +00001357 testdata1 = ''.join(zipdata1)
1358 self.assertEqual(len(testdata1), len(self.data))
1359 self.assertEqual(testdata1, self.data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001360
Ezio Melotti569e61f2009-12-30 06:14:51 +00001361 testdata2 = ''.join(zipdata2)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001362 self.assertEqual(len(testdata2), len(self.data))
1363 self.assertEqual(testdata2, self.data)
Tim Petersea5962f2007-03-12 18:07:52 +00001364
Ezio Melottid5a23e32009-07-15 17:07:04 +00001365 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001366 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001367 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001368
Antoine Pitroue1436d12010-08-12 15:25:51 +00001369 @skipUnless(zlib, "requires zlib")
1370 def test_open_deflated(self):
1371 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1372 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
1373
Ezio Melottid5a23e32009-07-15 17:07:04 +00001374 def zip_random_open_test(self, f, compression):
1375 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001376
1377 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001378 with zipfile.ZipFile(f, "r", compression) as zipfp:
1379 zipdata1 = []
Brian Curtin0d654332011-04-19 21:15:55 -05001380 with zipfp.open(TESTFN) as zipopen1:
1381 while True:
1382 read_data = zipopen1.read(randint(1, 1024))
1383 if not read_data:
1384 break
1385 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001386
Ezio Melotti569e61f2009-12-30 06:14:51 +00001387 testdata = ''.join(zipdata1)
1388 self.assertEqual(len(testdata), len(self.data))
1389 self.assertEqual(testdata, self.data)
Tim Petersea5962f2007-03-12 18:07:52 +00001390
Ezio Melottid5a23e32009-07-15 17:07:04 +00001391 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001392 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001393 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001394
Antoine Pitroue1436d12010-08-12 15:25:51 +00001395 @skipUnless(zlib, "requires zlib")
1396 def test_random_open_deflated(self):
1397 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
1398 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
1399
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001400
Ezio Melotti1036a7f2009-09-12 14:43:43 +00001401@skipUnless(zlib, "requires zlib")
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001402class TestsWithMultipleOpens(unittest.TestCase):
1403 def setUp(self):
1404 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001405 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
1406 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
1407 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
Tim Petersea5962f2007-03-12 18:07:52 +00001408
Ezio Melottid5a23e32009-07-15 17:07:04 +00001409 def test_same_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001410 # Verify that (when the ZipFile is in control of creating file objects)
1411 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +00001412 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
Benjamin Peterson352eb4f2014-04-03 10:31:25 -04001413 with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
1414 data1 = zopen1.read(500)
1415 data2 = zopen2.read(500)
1416 data1 += zopen1.read(500)
1417 data2 += zopen2.read(500)
Ezio Melotti569e61f2009-12-30 06:14:51 +00001418 self.assertEqual(data1, data2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001419
Ezio Melottid5a23e32009-07-15 17:07:04 +00001420 def test_different_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001421 # Verify that (when the ZipFile is in control of creating file objects)
1422 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +00001423 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
Brian Curtin0d654332011-04-19 21:15:55 -05001424 with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
1425 data1 = zopen1.read(500)
1426 data2 = zopen2.read(500)
1427 data1 += zopen1.read(500)
1428 data2 += zopen2.read(500)
Ezio Melotti569e61f2009-12-30 06:14:51 +00001429 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1430 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001431
Ezio Melottid5a23e32009-07-15 17:07:04 +00001432 def test_interleaved(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001433 # Verify that (when the ZipFile is in control of creating file objects)
1434 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +00001435 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
Brian Curtin0d654332011-04-19 21:15:55 -05001436 with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
1437 data1 = zopen1.read(500)
1438 data2 = zopen2.read(500)
1439 data1 += zopen1.read(500)
1440 data2 += zopen2.read(500)
Ezio Melotti569e61f2009-12-30 06:14:51 +00001441 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1442 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
Tim Petersea5962f2007-03-12 18:07:52 +00001443
Benjamin Peterson352eb4f2014-04-03 10:31:25 -04001444 def test_many_opens(self):
1445 # Verify that read() and open() promptly close the file descriptor,
1446 # and don't rely on the garbage collector to free resources.
1447 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1448 for x in range(100):
1449 zipf.read('ones')
1450 with zipf.open('ones') as zopen1:
1451 pass
1452 with open(os.devnull) as f:
1453 self.assertLess(f.fileno(), 100)
1454
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001455 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001456 unlink(TESTFN2)
1457
Tim Petersea5962f2007-03-12 18:07:52 +00001458
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001459class TestWithDirectory(unittest.TestCase):
1460 def setUp(self):
1461 os.mkdir(TESTFN2)
1462
Ezio Melottid5a23e32009-07-15 17:07:04 +00001463 def test_extract_dir(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +00001464 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
1465 zipf.extractall(TESTFN2)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001466 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1467 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1468 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1469
Ezio Melottid5a23e32009-07-15 17:07:04 +00001470 def test_bug_6050(self):
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001471 # Extraction should succeed if directories already exist
1472 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottid5a23e32009-07-15 17:07:04 +00001473 self.test_extract_dir()
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001474
Serhiy Storchaka6d343e72014-09-23 22:39:59 +03001475 def test_write_dir(self):
1476 dirpath = os.path.join(TESTFN2, "x")
1477 os.mkdir(dirpath)
1478 mode = os.stat(dirpath).st_mode & 0xFFFF
1479 with zipfile.ZipFile(TESTFN, "w") as zipf:
1480 zipf.write(dirpath)
1481 zinfo = zipf.filelist[0]
1482 self.assertTrue(zinfo.filename.endswith("/x/"))
1483 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
1484 zipf.write(dirpath, "y")
1485 zinfo = zipf.filelist[1]
1486 self.assertTrue(zinfo.filename, "y/")
1487 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
1488 with zipfile.ZipFile(TESTFN, "r") as zipf:
1489 zinfo = zipf.filelist[0]
1490 self.assertTrue(zinfo.filename.endswith("/x/"))
1491 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
1492 zinfo = zipf.filelist[1]
1493 self.assertTrue(zinfo.filename, "y/")
1494 self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
1495 target = os.path.join(TESTFN2, "target")
1496 os.mkdir(target)
1497 zipf.extractall(target)
1498 self.assertTrue(os.path.isdir(os.path.join(target, "y")))
1499 self.assertEqual(len(os.listdir(target)), 2)
1500
1501 def test_writestr_dir(self):
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001502 os.mkdir(os.path.join(TESTFN2, "x"))
Benjamin Peterson352eb4f2014-04-03 10:31:25 -04001503 with zipfile.ZipFile(TESTFN, "w") as zipf:
Serhiy Storchaka6d343e72014-09-23 22:39:59 +03001504 zipf.writestr("x/", b'')
1505 zinfo = zipf.filelist[0]
1506 self.assertEqual(zinfo.filename, "x/")
1507 self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10)
1508 with zipfile.ZipFile(TESTFN, "r") as zipf:
1509 zinfo = zipf.filelist[0]
1510 self.assertTrue(zinfo.filename.endswith("x/"))
1511 self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10)
1512 target = os.path.join(TESTFN2, "target")
1513 os.mkdir(target)
1514 zipf.extractall(target)
1515 self.assertTrue(os.path.isdir(os.path.join(target, "x")))
1516 self.assertEqual(os.listdir(target), ["x"])
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001517
1518 def tearDown(self):
Benjamin Peterson352eb4f2014-04-03 10:31:25 -04001519 rmtree(TESTFN2)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001520 if os.path.exists(TESTFN):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001521 unlink(TESTFN)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001522
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001523
1524class UniversalNewlineTests(unittest.TestCase):
1525 def setUp(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001526 self.line_gen = ["Test of zipfile line %d." % i
1527 for i in xrange(FIXEDTEST_SIZE)]
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001528 self.seps = ('\r', '\r\n', '\n')
1529 self.arcdata, self.arcfiles = {}, {}
1530 for n, s in enumerate(self.seps):
1531 self.arcdata[s] = s.join(self.line_gen) + s
1532 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Benjamin Peterson352eb4f2014-04-03 10:31:25 -04001533 with open(self.arcfiles[s], "wb") as fid:
1534 fid.write(self.arcdata[s])
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001535
Ezio Melottid5a23e32009-07-15 17:07:04 +00001536 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001537 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001538 with zipfile.ZipFile(f, "w", compression) as zipfp:
1539 for fn in self.arcfiles.values():
1540 zipfp.write(fn, fn)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001541
Ezio Melottid5a23e32009-07-15 17:07:04 +00001542 def read_test(self, f, compression):
1543 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001544
1545 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001546 with zipfile.ZipFile(f, "r") as zipfp:
1547 for sep, fn in self.arcfiles.items():
Brian Curtin0d654332011-04-19 21:15:55 -05001548 with zipfp.open(fn, "rU") as fp:
1549 zipdata = fp.read()
Ezio Melotti569e61f2009-12-30 06:14:51 +00001550 self.assertEqual(self.arcdata[sep], zipdata)
Tim Petersea5962f2007-03-12 18:07:52 +00001551
Antoine Pitrou94c33eb2010-01-27 20:59:50 +00001552 def readline_read_test(self, f, compression):
1553 self.make_test_archive(f, compression)
1554
1555 # Read the ZIP archive
1556 zipfp = zipfile.ZipFile(f, "r")
1557 for sep, fn in self.arcfiles.items():
Brian Curtin0d654332011-04-19 21:15:55 -05001558 with zipfp.open(fn, "rU") as zipopen:
1559 data = ''
1560 while True:
1561 read = zipopen.readline()
1562 if not read:
1563 break
1564 data += read
Antoine Pitrou94c33eb2010-01-27 20:59:50 +00001565
Brian Curtin0d654332011-04-19 21:15:55 -05001566 read = zipopen.read(5)
1567 if not read:
1568 break
1569 data += read
Antoine Pitrou94c33eb2010-01-27 20:59:50 +00001570
1571 self.assertEqual(data, self.arcdata['\n'])
1572
1573 zipfp.close()
1574
Ezio Melottid5a23e32009-07-15 17:07:04 +00001575 def readline_test(self, f, compression):
1576 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001577
1578 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001579 with zipfile.ZipFile(f, "r") as zipfp:
1580 for sep, fn in self.arcfiles.items():
Brian Curtin0d654332011-04-19 21:15:55 -05001581 with zipfp.open(fn, "rU") as zipopen:
1582 for line in self.line_gen:
1583 linedata = zipopen.readline()
1584 self.assertEqual(linedata, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001585
Ezio Melottid5a23e32009-07-15 17:07:04 +00001586 def readlines_test(self, f, compression):
1587 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001588
1589 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001590 with zipfile.ZipFile(f, "r") as zipfp:
1591 for sep, fn in self.arcfiles.items():
Brian Curtin0d654332011-04-19 21:15:55 -05001592 with zipfp.open(fn, "rU") as fp:
1593 ziplines = fp.readlines()
Ezio Melotti569e61f2009-12-30 06:14:51 +00001594 for line, zipline in zip(self.line_gen, ziplines):
1595 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001596
Ezio Melottid5a23e32009-07-15 17:07:04 +00001597 def iterlines_test(self, f, compression):
1598 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001599
1600 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001601 with zipfile.ZipFile(f, "r") as zipfp:
1602 for sep, fn in self.arcfiles.items():
Benjamin Peterson352eb4f2014-04-03 10:31:25 -04001603 with zipfp.open(fn, "rU") as fid:
1604 for line, zipline in zip(self.line_gen, fid):
1605 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001606
Ezio Melottid5a23e32009-07-15 17:07:04 +00001607 def test_read_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001608 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001609 self.read_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001610
Antoine Pitrou94c33eb2010-01-27 20:59:50 +00001611 def test_readline_read_stored(self):
1612 # Issue #7610: calls to readline() interleaved with calls to read().
1613 for f in (TESTFN2, TemporaryFile(), StringIO()):
1614 self.readline_read_test(f, zipfile.ZIP_STORED)
1615
Ezio Melottid5a23e32009-07-15 17:07:04 +00001616 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001617 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001618 self.readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001619
Ezio Melottid5a23e32009-07-15 17:07:04 +00001620 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001621 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001622 self.readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001623
Ezio Melottid5a23e32009-07-15 17:07:04 +00001624 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001625 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001626 self.iterlines_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001627
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001628 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001629 def test_read_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001630 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001631 self.read_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001632
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001633 @skipUnless(zlib, "requires zlib")
Antoine Pitrou94c33eb2010-01-27 20:59:50 +00001634 def test_readline_read_deflated(self):
1635 # Issue #7610: calls to readline() interleaved with calls to read().
1636 for f in (TESTFN2, TemporaryFile(), StringIO()):
1637 self.readline_read_test(f, zipfile.ZIP_DEFLATED)
1638
1639 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001640 def test_readline_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001641 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001642 self.readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001643
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001644 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001645 def test_readlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001646 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001647 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001648
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001649 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001650 def test_iterlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001651 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001652 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001653
1654 def tearDown(self):
1655 for sep, fn in self.arcfiles.items():
1656 os.remove(fn)
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001657 unlink(TESTFN)
1658 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001659
1660
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001661def test_main():
Tim Petersea5962f2007-03-12 18:07:52 +00001662 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1663 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001664 TestWithDirectory, UniversalNewlineTests,
1665 TestsWithRandomBinaryFiles)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001666
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001667if __name__ == "__main__":
1668 test_main()