blob: 27591b78b5343dbbf37cafb456a35dbe94a2b7e7 [file] [log] [blame]
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001# We can test part of the module without zlib.
Guido van Rossum368f04a2000-04-10 13:23:04 +00002try:
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00003 import zlib
4except ImportError:
5 zlib = None
Tim Petersa45cacf2004-08-20 03:47:14 +00006
Ezio Melottie7a0cc22009-07-04 14:58:27 +00007import os
8import sys
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00009import time
Ezio Melottie7a0cc22009-07-04 14:58:27 +000010import shutil
11import struct
12import zipfile
13import unittest
Tim Petersa19a1682001-03-29 04:36:09 +000014
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000015from StringIO import StringIO
16from tempfile import TemporaryFile
Martin v. Löwis3eb76482007-03-06 10:41:24 +000017from random import randint, random
Ezio Melottie7a0cc22009-07-04 14:58:27 +000018from unittest import skipUnless
Tim Petersa19a1682001-03-29 04:36:09 +000019
Ezio Melotti6cbfc122009-07-10 20:25:56 +000020from test.test_support import TESTFN, run_unittest, findfile, unlink
Guido van Rossum368f04a2000-04-10 13:23:04 +000021
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000022TESTFN2 = TESTFN + "2"
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +000023TESTFNDIR = TESTFN + "d"
Georg Brandl4b3ab6f2007-07-12 09:59:22 +000024FIXEDTEST_SIZE = 1000
Guido van Rossum368f04a2000-04-10 13:23:04 +000025
Georg Brandl62416bc2008-01-07 18:47:44 +000026SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
27 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
28 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
29 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
30
Ezio Melotti6cbfc122009-07-10 20:25:56 +000031
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000032class TestsWithSourceFile(unittest.TestCase):
33 def setUp(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +000034 self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random())
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000035 for i in xrange(FIXEDTEST_SIZE)]
Martin v. Löwis3eb76482007-03-06 10:41:24 +000036 self.data = '\n'.join(self.line_gen) + '\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000037
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000038 # Make a source file with some lines
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000039 with open(TESTFN, "wb") as fp:
40 fp.write(self.data)
Tim Peters7d3bad62001-04-04 18:56:49 +000041
Ezio Melottid5a23e32009-07-15 17:07:04 +000042 def make_test_archive(self, f, compression):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000043 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +000044 with zipfile.ZipFile(f, "w", compression) as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000045 zipfp.write(TESTFN, "another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +000046 zipfp.write(TESTFN, TESTFN)
47 zipfp.writestr("strfile", self.data)
Tim Peters7d3bad62001-04-04 18:56:49 +000048
Ezio Melottid5a23e32009-07-15 17:07:04 +000049 def zip_test(self, f, compression):
50 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +000051
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000052 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +000053 with zipfile.ZipFile(f, "r", compression) as zipfp:
54 self.assertEqual(zipfp.read(TESTFN), self.data)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000055 self.assertEqual(zipfp.read("another.name"), self.data)
Ezio Melotti569e61f2009-12-30 06:14:51 +000056 self.assertEqual(zipfp.read("strfile"), self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000057
Ezio Melotti569e61f2009-12-30 06:14:51 +000058 # Print the ZIP directory
59 fp = StringIO()
60 stdout = sys.stdout
61 try:
62 sys.stdout = fp
63 zipfp.printdir()
64 finally:
65 sys.stdout = stdout
Tim Petersa608bb22006-06-15 18:06:29 +000066
Ezio Melotti569e61f2009-12-30 06:14:51 +000067 directory = fp.getvalue()
68 lines = directory.splitlines()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000069 self.assertEqual(len(lines), 4) # Number of files + header
Ronald Oussoren143cefb2006-06-15 08:14:18 +000070
Ezio Melotti569e61f2009-12-30 06:14:51 +000071 self.assertTrue('File Name' in lines[0])
72 self.assertTrue('Modified' in lines[0])
73 self.assertTrue('Size' in lines[0])
Ronald Oussoren143cefb2006-06-15 08:14:18 +000074
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000075 fn, date, time_, size = lines[1].split()
76 self.assertEqual(fn, 'another.name')
77 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
78 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
79 self.assertEqual(size, str(len(self.data)))
Ronald Oussoren143cefb2006-06-15 08:14:18 +000080
Ezio Melotti569e61f2009-12-30 06:14:51 +000081 # Check the namelist
82 names = zipfp.namelist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000083 self.assertEqual(len(names), 3)
Ezio Melotti569e61f2009-12-30 06:14:51 +000084 self.assertTrue(TESTFN in names)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000085 self.assertTrue("another.name" in names)
Ezio Melotti569e61f2009-12-30 06:14:51 +000086 self.assertTrue("strfile" in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000087
Ezio Melotti569e61f2009-12-30 06:14:51 +000088 # Check infolist
89 infos = zipfp.infolist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000090 names = [i.filename for i in infos]
91 self.assertEqual(len(names), 3)
Ezio Melotti569e61f2009-12-30 06:14:51 +000092 self.assertTrue(TESTFN in names)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000093 self.assertTrue("another.name" in names)
Ezio Melotti569e61f2009-12-30 06:14:51 +000094 self.assertTrue("strfile" in names)
95 for i in infos:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000096 self.assertEqual(i.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +000097
Ezio Melotti569e61f2009-12-30 06:14:51 +000098 # check getinfo
Ezio Melotti6d6b53c2009-12-31 13:00:43 +000099 for nm in (TESTFN, "another.name", "strfile"):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000100 info = zipfp.getinfo(nm)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000101 self.assertEqual(info.filename, nm)
102 self.assertEqual(info.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000103
Ezio Melotti569e61f2009-12-30 06:14:51 +0000104 # Check that testzip doesn't raise an exception
105 zipfp.testzip()
Tim Peters7d3bad62001-04-04 18:56:49 +0000106
Ezio Melottid5a23e32009-07-15 17:07:04 +0000107 def test_stored(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000108 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000109 self.zip_test(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000110
Ezio Melottid5a23e32009-07-15 17:07:04 +0000111 def zip_open_test(self, f, compression):
112 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000113
114 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000115 with zipfile.ZipFile(f, "r", compression) as zipfp:
116 zipdata1 = []
117 zipopen1 = zipfp.open(TESTFN)
118 while True:
119 read_data = zipopen1.read(256)
120 if not read_data:
121 break
122 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000123
Ezio Melotti569e61f2009-12-30 06:14:51 +0000124 zipdata2 = []
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000125 zipopen2 = zipfp.open("another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +0000126 while True:
127 read_data = zipopen2.read(256)
128 if not read_data:
129 break
130 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +0000131
Ezio Melotti569e61f2009-12-30 06:14:51 +0000132 self.assertEqual(''.join(zipdata1), self.data)
133 self.assertEqual(''.join(zipdata2), self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000134
Ezio Melottid5a23e32009-07-15 17:07:04 +0000135 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000136 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000137 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000138
Ezio Melottid5a23e32009-07-15 17:07:04 +0000139 def test_open_via_zip_info(self):
Georg Brandl112aa502008-05-20 08:25:48 +0000140 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000141 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
142 zipfp.writestr("name", "foo")
143 zipfp.writestr("name", "bar")
Georg Brandl112aa502008-05-20 08:25:48 +0000144
Ezio Melotti569e61f2009-12-30 06:14:51 +0000145 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
146 infos = zipfp.infolist()
147 data = ""
148 for info in infos:
149 data += zipfp.open(info).read()
150 self.assertTrue(data == "foobar" or data == "barfoo")
151 data = ""
152 for info in infos:
153 data += zipfp.read(info)
154 self.assertTrue(data == "foobar" or data == "barfoo")
Georg Brandl112aa502008-05-20 08:25:48 +0000155
Ezio Melottid5a23e32009-07-15 17:07:04 +0000156 def zip_random_open_test(self, f, compression):
157 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000158
159 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000160 with zipfile.ZipFile(f, "r", compression) as zipfp:
161 zipdata1 = []
162 zipopen1 = zipfp.open(TESTFN)
163 while True:
164 read_data = zipopen1.read(randint(1, 1024))
165 if not read_data:
166 break
167 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000168
Ezio Melotti569e61f2009-12-30 06:14:51 +0000169 self.assertEqual(''.join(zipdata1), self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000170
Ezio Melottid5a23e32009-07-15 17:07:04 +0000171 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000172 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000173 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +0000174
Ezio Melottid5a23e32009-07-15 17:07:04 +0000175 def zip_readline_test(self, f, compression):
176 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000177
178 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000179 with zipfile.ZipFile(f, "r") as zipfp:
180 zipopen = zipfp.open(TESTFN)
181 for line in self.line_gen:
182 linedata = zipopen.readline()
183 self.assertEqual(linedata, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000184
Ezio Melottid5a23e32009-07-15 17:07:04 +0000185 def zip_readlines_test(self, f, compression):
186 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000187
188 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000189 with zipfile.ZipFile(f, "r") as zipfp:
190 ziplines = zipfp.open(TESTFN).readlines()
191 for line, zipline in zip(self.line_gen, ziplines):
192 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000193
Ezio Melottid5a23e32009-07-15 17:07:04 +0000194 def zip_iterlines_test(self, f, compression):
195 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000196
197 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000198 with zipfile.ZipFile(f, "r") as zipfp:
199 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
200 self.assertEqual(zipline, line + '\n')
Tim Petersea5962f2007-03-12 18:07:52 +0000201
Ezio Melottid5a23e32009-07-15 17:07:04 +0000202 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000203 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000204 self.zip_readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000205
Ezio Melottid5a23e32009-07-15 17:07:04 +0000206 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000207 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000208 self.zip_readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000209
Ezio Melottid5a23e32009-07-15 17:07:04 +0000210 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000211 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000212 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000213
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000214 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000215 def test_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000216 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000217 self.zip_test(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000218
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000219 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000220 def test_open_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000221 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000222 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000223
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000224 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000225 def test_random_open_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000226 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000227 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000228
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000229 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000230 def test_readline_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000231 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000232 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000233
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000234 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000235 def test_readlines_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000236 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000237 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000238
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000239 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000240 def test_iterlines_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000241 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000242 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
Tim Petersea5962f2007-03-12 18:07:52 +0000243
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000244 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000245 def test_low_compression(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000246 """Check for cases where compressed data is larger than original."""
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000247 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000248 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
249 zipfp.writestr("strfile", '12')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000250
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000251 # Get an open object for strfile
Ezio Melotti569e61f2009-12-30 06:14:51 +0000252 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
253 openobj = zipfp.open("strfile")
254 self.assertEqual(openobj.read(1), '1')
255 self.assertEqual(openobj.read(1), '2')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000256
Ezio Melottid5a23e32009-07-15 17:07:04 +0000257 def test_absolute_arcnames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000258 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
259 zipfp.write(TESTFN, "/absolute")
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000260
Ezio Melotti569e61f2009-12-30 06:14:51 +0000261 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
262 self.assertEqual(zipfp.namelist(), ["absolute"])
Tim Peters32cbc962006-02-20 21:42:18 +0000263
Ezio Melottid5a23e32009-07-15 17:07:04 +0000264 def test_append_to_zip_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000265 """Test appending to an existing zipfile."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000266 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
267 zipfp.write(TESTFN, TESTFN)
268
269 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
270 zipfp.writestr("strfile", self.data)
271 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000272
Ezio Melottid5a23e32009-07-15 17:07:04 +0000273 def test_append_to_non_zip_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000274 """Test appending to an existing file that is not a zipfile."""
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000275 # NOTE: this test fails if len(d) < 22 because of the first
276 # line "fpin.seek(-22, 2)" in _EndRecData
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000277 data = 'I am not a ZipFile!'*10
278 with open(TESTFN2, 'wb') as f:
279 f.write(data)
280
Ezio Melotti569e61f2009-12-30 06:14:51 +0000281 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
282 zipfp.write(TESTFN, TESTFN)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000283
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000284 with open(TESTFN2, 'rb') as f:
285 f.seek(len(data))
286 with zipfile.ZipFile(f, "r") as zipfp:
287 self.assertEqual(zipfp.namelist(), [TESTFN])
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000288
Ezio Melottid5a23e32009-07-15 17:07:04 +0000289 def test_write_default_name(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000290 """Check that calling ZipFile.write without arcname specified
291 produces the expected result."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000292 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
293 zipfp.write(TESTFN)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000294 self.assertEqual(zipfp.read(TESTFN), open(TESTFN).read())
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000295
Ezio Melotti1036a7f2009-09-12 14:43:43 +0000296 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000297 def test_per_file_compression(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000298 """Check that files within a Zip archive can have different
299 compression options."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000300 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
301 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
302 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
303 sinfo = zipfp.getinfo('storeme')
304 dinfo = zipfp.getinfo('deflateme')
305 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
306 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000307
Ezio Melottid5a23e32009-07-15 17:07:04 +0000308 def test_write_to_readonly(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000309 """Check that trying to call write() on a readonly ZipFile object
310 raises a RuntimeError."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000311 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
312 zipfp.writestr("somefile.txt", "bogus")
313
314 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
315 self.assertRaises(RuntimeError, zipfp.write, TESTFN)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000316
Ezio Melottid5a23e32009-07-15 17:07:04 +0000317 def test_extract(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000318 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
319 for fpath, fdata in SMALL_TEST_DATA:
320 zipfp.writestr(fpath, fdata)
Georg Brandl62416bc2008-01-07 18:47:44 +0000321
Ezio Melotti569e61f2009-12-30 06:14:51 +0000322 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
323 for fpath, fdata in SMALL_TEST_DATA:
324 writtenfile = zipfp.extract(fpath)
Georg Brandl62416bc2008-01-07 18:47:44 +0000325
Ezio Melotti569e61f2009-12-30 06:14:51 +0000326 # make sure it was written to the right place
327 if os.path.isabs(fpath):
328 correctfile = os.path.join(os.getcwd(), fpath[1:])
329 else:
330 correctfile = os.path.join(os.getcwd(), fpath)
331 correctfile = os.path.normpath(correctfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000332
Ezio Melotti569e61f2009-12-30 06:14:51 +0000333 self.assertEqual(writtenfile, correctfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000334
Ezio Melotti569e61f2009-12-30 06:14:51 +0000335 # make sure correct data is in correct file
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000336 self.assertEqual(fdata, open(writtenfile, "rb").read())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000337 os.remove(writtenfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000338
339 # remove the test file subdirectories
340 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
341
Ezio Melottid5a23e32009-07-15 17:07:04 +0000342 def test_extract_all(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000343 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
344 for fpath, fdata in SMALL_TEST_DATA:
345 zipfp.writestr(fpath, fdata)
Georg Brandl62416bc2008-01-07 18:47:44 +0000346
Ezio Melotti569e61f2009-12-30 06:14:51 +0000347 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
348 zipfp.extractall()
349 for fpath, fdata in SMALL_TEST_DATA:
350 if os.path.isabs(fpath):
351 outfile = os.path.join(os.getcwd(), fpath[1:])
352 else:
353 outfile = os.path.join(os.getcwd(), fpath)
Georg Brandl62416bc2008-01-07 18:47:44 +0000354
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000355 self.assertEqual(fdata, open(outfile, "rb").read())
Ezio Melotti569e61f2009-12-30 06:14:51 +0000356 os.remove(outfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000357
358 # remove the test file subdirectories
359 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
360
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000361 def zip_test_writestr_permissions(self, f, compression):
362 # Make sure that writestr creates files with mode 0600,
363 # when it is passed a name rather than a ZipInfo instance.
364
Ezio Melottid5a23e32009-07-15 17:07:04 +0000365 self.make_test_archive(f, compression)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000366 with zipfile.ZipFile(f, "r") as zipfp:
367 zinfo = zipfp.getinfo('strfile')
368 self.assertEqual(zinfo.external_attr, 0600 << 16)
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000369
Ezio Melottid5a23e32009-07-15 17:07:04 +0000370 def test_writestr_permissions(self):
Antoine Pitrou5fdfa3e2008-07-25 19:42:26 +0000371 for f in (TESTFN2, TemporaryFile(), StringIO()):
372 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
373
Ezio Melotti569e61f2009-12-30 06:14:51 +0000374 def test_close(self):
375 """Check that the zipfile is closed after the 'with' block."""
376 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
377 for fpath, fdata in SMALL_TEST_DATA:
378 zipfp.writestr(fpath, fdata)
379 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
380 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
381
382 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
383 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
384 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
385
386 def test_close_on_exception(self):
387 """Check that the zipfile is closed if an exception is raised in the
388 'with' block."""
389 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
390 for fpath, fdata in SMALL_TEST_DATA:
391 zipfp.writestr(fpath, fdata)
392
393 try:
394 with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
395 raise zipfile.BadZipfile()
396 except zipfile.BadZipfile:
397 self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
398
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000399 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000400 unlink(TESTFN)
401 unlink(TESTFN2)
402
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000403
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000404class TestZip64InSmallFiles(unittest.TestCase):
405 # These tests test the ZIP64 functionality without using large files,
406 # see test_zipfile64 for proper tests.
407
408 def setUp(self):
409 self._limit = zipfile.ZIP64_LIMIT
410 zipfile.ZIP64_LIMIT = 5
411
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000412 line_gen = ("Test of zipfile line %d." % i
413 for i in range(0, FIXEDTEST_SIZE))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000414 self.data = '\n'.join(line_gen)
415
416 # Make a source file with some lines
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000417 with open(TESTFN, "wb") as fp:
418 fp.write(self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000419
Ezio Melottid5a23e32009-07-15 17:07:04 +0000420 def large_file_exception_test(self, f, compression):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000421 with zipfile.ZipFile(f, "w", compression) as zipfp:
422 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000423 zipfp.write, TESTFN, "another.name")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000424
Ezio Melottid5a23e32009-07-15 17:07:04 +0000425 def large_file_exception_test2(self, f, compression):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000426 with zipfile.ZipFile(f, "w", compression) as zipfp:
427 self.assertRaises(zipfile.LargeZipFile,
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000428 zipfp.writestr, "another.name", self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000429
Ezio Melottid5a23e32009-07-15 17:07:04 +0000430 def test_large_file_exception(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000431 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000432 self.large_file_exception_test(f, zipfile.ZIP_STORED)
433 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000434
Ezio Melottid5a23e32009-07-15 17:07:04 +0000435 def zip_test(self, f, compression):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000436 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000437 with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000438 zipfp.write(TESTFN, "another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +0000439 zipfp.write(TESTFN, TESTFN)
440 zipfp.writestr("strfile", self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000441
442 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000443 with zipfile.ZipFile(f, "r", compression) as zipfp:
444 self.assertEqual(zipfp.read(TESTFN), self.data)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000445 self.assertEqual(zipfp.read("another.name"), self.data)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000446 self.assertEqual(zipfp.read("strfile"), self.data)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000447
Ezio Melotti569e61f2009-12-30 06:14:51 +0000448 # Print the ZIP directory
449 fp = StringIO()
450 stdout = sys.stdout
451 try:
452 sys.stdout = fp
453 zipfp.printdir()
454 finally:
455 sys.stdout = stdout
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000456
Ezio Melotti569e61f2009-12-30 06:14:51 +0000457 directory = fp.getvalue()
458 lines = directory.splitlines()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000459 self.assertEqual(len(lines), 4) # Number of files + header
Tim Petersa608bb22006-06-15 18:06:29 +0000460
Ezio Melotti569e61f2009-12-30 06:14:51 +0000461 self.assertTrue('File Name' in lines[0])
462 self.assertTrue('Modified' in lines[0])
463 self.assertTrue('Size' in lines[0])
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000464
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000465 fn, date, time_, size = lines[1].split()
466 self.assertEqual(fn, 'another.name')
467 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
468 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
469 self.assertEqual(size, str(len(self.data)))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000470
Ezio Melotti569e61f2009-12-30 06:14:51 +0000471 # Check the namelist
472 names = zipfp.namelist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000473 self.assertEqual(len(names), 3)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000474 self.assertTrue(TESTFN in names)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000475 self.assertTrue("another.name" in names)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000476 self.assertTrue("strfile" in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000477
Ezio Melotti569e61f2009-12-30 06:14:51 +0000478 # Check infolist
479 infos = zipfp.infolist()
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000480 names = [i.filename for i in infos]
481 self.assertEqual(len(names), 3)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000482 self.assertTrue(TESTFN in names)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000483 self.assertTrue("another.name" in names)
Ezio Melotti569e61f2009-12-30 06:14:51 +0000484 self.assertTrue("strfile" in names)
485 for i in infos:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000486 self.assertEqual(i.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000487
Ezio Melotti569e61f2009-12-30 06:14:51 +0000488 # check getinfo
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000489 for nm in (TESTFN, "another.name", "strfile"):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000490 info = zipfp.getinfo(nm)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000491 self.assertEqual(info.filename, nm)
492 self.assertEqual(info.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000493
Ezio Melotti569e61f2009-12-30 06:14:51 +0000494 # Check that testzip doesn't raise an exception
495 zipfp.testzip()
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000496
Ezio Melottid5a23e32009-07-15 17:07:04 +0000497 def test_stored(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000498 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000499 self.zip_test(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000500
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000501 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000502 def test_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000503 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000504 self.zip_test(f, zipfile.ZIP_DEFLATED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000505
Ezio Melottid5a23e32009-07-15 17:07:04 +0000506 def test_absolute_arcnames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000507 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
508 allowZip64=True) as zipfp:
509 zipfp.write(TESTFN, "/absolute")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000510
Ezio Melotti569e61f2009-12-30 06:14:51 +0000511 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
512 self.assertEqual(zipfp.namelist(), ["absolute"])
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000513
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000514 def tearDown(self):
515 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000516 unlink(TESTFN)
517 unlink(TESTFN2)
518
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000519
520class PyZipFileTests(unittest.TestCase):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000521 def test_write_pyfile(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000522 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
523 fn = __file__
524 if fn.endswith('.pyc') or fn.endswith('.pyo'):
525 fn = fn[:-1]
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000526
Ezio Melotti569e61f2009-12-30 06:14:51 +0000527 zipfp.writepy(fn)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000528
Ezio Melotti569e61f2009-12-30 06:14:51 +0000529 bn = os.path.basename(fn)
530 self.assertTrue(bn not in zipfp.namelist())
531 self.assertTrue(bn + 'o' in zipfp.namelist() or
532 bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000533
Ezio Melotti569e61f2009-12-30 06:14:51 +0000534 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
535 fn = __file__
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000536 if fn.endswith(('.pyc', '.pyo')):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000537 fn = fn[:-1]
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000538
Ezio Melotti569e61f2009-12-30 06:14:51 +0000539 zipfp.writepy(fn, "testpackage")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000540
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000541 bn = "%s/%s" % ("testpackage", os.path.basename(fn))
Ezio Melotti569e61f2009-12-30 06:14:51 +0000542 self.assertTrue(bn not in zipfp.namelist())
543 self.assertTrue(bn + 'o' in zipfp.namelist() or
544 bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000545
Ezio Melottid5a23e32009-07-15 17:07:04 +0000546 def test_write_python_package(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000547 import email
548 packagedir = os.path.dirname(email.__file__)
549
Ezio Melotti569e61f2009-12-30 06:14:51 +0000550 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
551 zipfp.writepy(packagedir)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000552
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000553 # Check for a couple of modules at different levels of the
554 # hierarchy
Ezio Melotti569e61f2009-12-30 06:14:51 +0000555 names = zipfp.namelist()
556 self.assertTrue('email/__init__.pyo' in names or
557 'email/__init__.pyc' in names)
558 self.assertTrue('email/mime/text.pyo' in names or
559 'email/mime/text.pyc' in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000560
Ezio Melottid5a23e32009-07-15 17:07:04 +0000561 def test_write_python_directory(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000562 os.mkdir(TESTFN2)
563 try:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000564 with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
Ezio Melotti763f1e82009-12-31 13:27:41 +0000565 fp.write("print(42)\n")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000566
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000567 with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
Ezio Melotti763f1e82009-12-31 13:27:41 +0000568 fp.write("print(42 * 42)\n")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000569
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000570 with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp:
571 fp.write("bla bla bla\n")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000572
573 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
574 zipfp.writepy(TESTFN2)
575
576 names = zipfp.namelist()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000577 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
578 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
579 self.assertTrue('mod2.txt' not in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000580
581 finally:
582 shutil.rmtree(TESTFN2)
583
Ezio Melottid5a23e32009-07-15 17:07:04 +0000584 def test_write_non_pyfile(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000585 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000586 open(TESTFN, 'w').write('most definitely not a python file')
Ezio Melotti569e61f2009-12-30 06:14:51 +0000587 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
588 os.remove(TESTFN)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000589
590
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000591class OtherTests(unittest.TestCase):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000592 def test_unicode_filenames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000593 with zipfile.ZipFile(TESTFN, "w") as zf:
594 zf.writestr(u"foo.txt", "Test for unicode filename")
595 zf.writestr(u"\xf6.txt", "Test for unicode filename")
596 self.assertTrue(isinstance(zf.infolist()[0].filename, unicode))
597
598 with zipfile.ZipFile(TESTFN, "r") as zf:
599 self.assertEqual(zf.filelist[0].filename, "foo.txt")
600 self.assertEqual(zf.filelist[1].filename, u"\xf6.txt")
Martin v. Löwis471617d2008-05-05 17:16:58 +0000601
Ezio Melottid5a23e32009-07-15 17:07:04 +0000602 def test_create_non_existent_file_for_append(self):
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000603 if os.path.exists(TESTFN):
604 os.unlink(TESTFN)
Tim Petersea5962f2007-03-12 18:07:52 +0000605
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000606 filename = 'testfile.txt'
607 content = 'hello, world. this is some content.'
Tim Petersea5962f2007-03-12 18:07:52 +0000608
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000609 try:
Ezio Melotti569e61f2009-12-30 06:14:51 +0000610 with zipfile.ZipFile(TESTFN, 'a') as zf:
611 zf.writestr(filename, content)
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000612 except IOError:
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000613 self.fail('Could not append data to a non-existent zip file.')
614
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000615 self.assertTrue(os.path.exists(TESTFN))
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000616
Ezio Melotti569e61f2009-12-30 06:14:51 +0000617 with zipfile.ZipFile(TESTFN, 'r') as zf:
618 self.assertEqual(zf.read(filename), content)
Tim Petersea5962f2007-03-12 18:07:52 +0000619
Ezio Melottid5a23e32009-07-15 17:07:04 +0000620 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000621 # This test checks that the ZipFile constructor closes the file object
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000622 # it opens if there's an error in the file. If it doesn't, the
623 # traceback holds a reference to the ZipFile object and, indirectly,
624 # the file object.
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000625 # On Windows, this causes the os.unlink() call to fail because the
626 # underlying file is still open. This is SF bug #412214.
627 #
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000628 with open(TESTFN, "w") as fp:
629 fp.write("this is not a legal zip file\n")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000630 try:
631 zf = zipfile.ZipFile(TESTFN)
632 except zipfile.BadZipfile:
Collin Winter04a51ec2007-03-29 02:28:16 +0000633 pass
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000634
Ezio Melottid5a23e32009-07-15 17:07:04 +0000635 def test_is_zip_erroneous_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000636 """Check that is_zipfile() correctly identifies non-zip files."""
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000637 # - passing a filename
638 with open(TESTFN, "w") as fp:
639 fp.write("this is not a legal zip file\n")
Tim Petersea5962f2007-03-12 18:07:52 +0000640 chk = zipfile.is_zipfile(TESTFN)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000641 self.assertFalse(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000642 # - passing a file object
643 with open(TESTFN, "rb") as fp:
644 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000645 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000646 # - passing a file-like object
647 fp = StringIO()
648 fp.write("this is not a legal zip file\n")
649 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000650 self.assertTrue(not chk)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000651 fp.seek(0, 0)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000652 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000653 self.assertTrue(not chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000654
Ezio Melottid5a23e32009-07-15 17:07:04 +0000655 def test_is_zip_valid_file(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000656 """Check that is_zipfile() correctly identifies zip files."""
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000657 # - passing a filename
Ezio Melotti569e61f2009-12-30 06:14:51 +0000658 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
659 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Tim Petersea5962f2007-03-12 18:07:52 +0000660 chk = zipfile.is_zipfile(TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000661 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000662 # - passing a file object
663 with open(TESTFN, "rb") as fp:
664 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000665 self.assertTrue(chk)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000666 fp.seek(0, 0)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000667 zip_contents = fp.read()
668 # - passing a file-like object
669 fp = StringIO()
670 fp.write(zip_contents)
671 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000672 self.assertTrue(chk)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000673 fp.seek(0, 0)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000674 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000675 self.assertTrue(chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000676
Ezio Melottid5a23e32009-07-15 17:07:04 +0000677 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000678 # make sure we don't raise an AttributeError when a partially-constructed
679 # ZipFile instance is finalized; this tests for regression on SF tracker
680 # bug #403871.
681
682 # The bug we're testing for caused an AttributeError to be raised
683 # when a ZipFile instance was created for a file that did not
684 # exist; the .fp member was not initialized but was needed by the
685 # __del__() method. Since the AttributeError is in the __del__(),
686 # it is ignored, but the user should be sufficiently annoyed by
687 # the message on the output that regression will be noticed
688 # quickly.
689 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
690
Amaury Forgeot d'Arc3e5b0272009-07-28 22:15:30 +0000691 def test_empty_file_raises_BadZipFile(self):
692 f = open(TESTFN, 'w')
693 f.close()
694 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
695
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000696 with open(TESTFN, 'w') as fp:
697 fp.write("short file")
Amaury Forgeot d'Arc3e5b0272009-07-28 22:15:30 +0000698 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
699
Ezio Melottid5a23e32009-07-15 17:07:04 +0000700 def test_closed_zip_raises_RuntimeError(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000701 """Verify that testzip() doesn't swallow inappropriate exceptions."""
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000702 data = StringIO()
Ezio Melotti569e61f2009-12-30 06:14:51 +0000703 with zipfile.ZipFile(data, mode="w") as zipf:
704 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000705
706 # This is correct; calling .read on a closed ZipFile should throw
707 # a RuntimeError, and so should calling .testzip. An earlier
708 # version of .testzip would swallow this exception (and any other)
709 # and report that the first file in the archive was corrupt.
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000710 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
711 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000712 self.assertRaises(RuntimeError, zipf.testzip)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000713 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000714 open(TESTFN, 'w').write('zipfile test data')
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000715 self.assertRaises(RuntimeError, zipf.write, TESTFN)
716
Ezio Melottid5a23e32009-07-15 17:07:04 +0000717 def test_bad_constructor_mode(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000718 """Check that bad modes passed to ZipFile constructor are caught."""
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000719 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
720
Ezio Melottid5a23e32009-07-15 17:07:04 +0000721 def test_bad_open_mode(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000722 """Check that bad modes passed to ZipFile.open are caught."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000723 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
724 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
725
726 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000727 # read the data to make sure the file is there
Ezio Melotti569e61f2009-12-30 06:14:51 +0000728 zipf.read("foo.txt")
729 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000730
Ezio Melottid5a23e32009-07-15 17:07:04 +0000731 def test_read0(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000732 """Check that calling read(0) on a ZipExtFile object returns an empty
733 string and doesn't advance file pointer."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000734 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
735 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
736 # read the data to make sure the file is there
737 f = zipf.open("foo.txt")
738 for i in xrange(FIXEDTEST_SIZE):
739 self.assertEqual(f.read(0), '')
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000740
Ezio Melotti569e61f2009-12-30 06:14:51 +0000741 self.assertEqual(f.read(), "O, for a Muse of Fire!")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000742
Ezio Melottid5a23e32009-07-15 17:07:04 +0000743 def test_open_non_existent_item(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000744 """Check that attempting to call open() for an item that doesn't
745 exist in the archive raises a RuntimeError."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000746 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
747 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000748
Ezio Melottid5a23e32009-07-15 17:07:04 +0000749 def test_bad_compression_mode(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000750 """Check that bad compression methods passed to ZipFile.open are
751 caught."""
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000752 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
753
Ezio Melottid5a23e32009-07-15 17:07:04 +0000754 def test_null_byte_in_filename(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000755 """Check that a filename containing a null byte is properly
756 terminated."""
Ezio Melotti569e61f2009-12-30 06:14:51 +0000757 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
758 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
759 self.assertEqual(zipf.namelist(), ['foo.txt'])
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000760
Ezio Melottid5a23e32009-07-15 17:07:04 +0000761 def test_struct_sizes(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000762 """Check that ZIP internal structure sizes are calculated correctly."""
Martin v. Löwis8c436412008-07-03 12:51:14 +0000763 self.assertEqual(zipfile.sizeEndCentDir, 22)
764 self.assertEqual(zipfile.sizeCentralDir, 46)
765 self.assertEqual(zipfile.sizeEndCentDir64, 56)
766 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
767
Ezio Melottid5a23e32009-07-15 17:07:04 +0000768 def test_comments(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000769 """Check that comments on the archive are handled properly."""
Martin v. Löwis8c436412008-07-03 12:51:14 +0000770
771 # check default comment is empty
Ezio Melotti569e61f2009-12-30 06:14:51 +0000772 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
773 self.assertEqual(zipf.comment, '')
774 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
775
776 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
777 self.assertEqual(zipf.comment, '')
Martin v. Löwis8c436412008-07-03 12:51:14 +0000778
779 # check a simple short comment
780 comment = 'Bravely taking to his feet, he beat a very brave retreat.'
Ezio Melotti569e61f2009-12-30 06:14:51 +0000781 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
782 zipf.comment = comment
783 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
784 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
785 self.assertEqual(zipf.comment, comment)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000786
787 # check a comment of max length
788 comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)])
Ezio Melotti569e61f2009-12-30 06:14:51 +0000789 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
790 zipf.comment = comment2
791 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
792
793 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
794 self.assertEqual(zipf.comment, comment2)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000795
796 # check a comment that is too long is truncated
Ezio Melotti569e61f2009-12-30 06:14:51 +0000797 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
798 zipf.comment = comment2 + 'oops'
799 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
800 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
801 self.assertEqual(zipf.comment, comment2)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000802
Collin Winter04a51ec2007-03-29 02:28:16 +0000803 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000804 unlink(TESTFN)
805 unlink(TESTFN2)
806
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000807
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000808class DecryptionTests(unittest.TestCase):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000809 """Check that ZIP decryption works. Since the library does not
810 support encryption at the moment, we use a pre-generated encrypted
811 ZIP file."""
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000812
813 data = (
814 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
815 '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
816 '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
817 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
818 '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
819 '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
820 '\x00\x00L\x00\x00\x00\x00\x00' )
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000821 data2 = (
822 'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
823 '\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
824 '\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
825 'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
826 '\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
827 '\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
828 'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
829 '\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000830
831 plain = 'zipfile.py encryption test'
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000832 plain2 = '\x00'*512
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000833
834 def setUp(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000835 with open(TESTFN, "wb") as fp:
836 fp.write(self.data)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000837 self.zip = zipfile.ZipFile(TESTFN, "r")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000838 with open(TESTFN2, "wb") as fp:
839 fp.write(self.data2)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000840 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000841
842 def tearDown(self):
843 self.zip.close()
844 os.unlink(TESTFN)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000845 self.zip2.close()
846 os.unlink(TESTFN2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000847
Ezio Melottid5a23e32009-07-15 17:07:04 +0000848 def test_no_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000849 # Reading the encrypted file without password
850 # must generate a RunTime exception
851 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000852 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000853
Ezio Melottid5a23e32009-07-15 17:07:04 +0000854 def test_bad_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000855 self.zip.setpassword("perl")
856 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000857 self.zip2.setpassword("perl")
858 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Tim Petersea5962f2007-03-12 18:07:52 +0000859
Ezio Melotti1036a7f2009-09-12 14:43:43 +0000860 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000861 def test_good_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000862 self.zip.setpassword("python")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000863 self.assertEqual(self.zip.read("test.txt"), self.plain)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000864 self.zip2.setpassword("12345")
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000865 self.assertEqual(self.zip2.read("zero"), self.plain2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000866
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000867
868class TestsWithRandomBinaryFiles(unittest.TestCase):
869 def setUp(self):
870 datacount = randint(16, 64)*1024 + randint(1, 1024)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000871 self.data = ''.join(struct.pack('<f', random()*randint(-1000, 1000))
Ezio Melotti763f1e82009-12-31 13:27:41 +0000872 for i in xrange(datacount))
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000873
874 # Make a source file with some lines
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000875 with open(TESTFN, "wb") as fp:
876 fp.write(self.data)
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000877
Collin Winter04a51ec2007-03-29 02:28:16 +0000878 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000879 unlink(TESTFN)
880 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000881
Ezio Melottid5a23e32009-07-15 17:07:04 +0000882 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000883 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000884 with zipfile.ZipFile(f, "w", compression) as zipfp:
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000885 zipfp.write(TESTFN, "another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +0000886 zipfp.write(TESTFN, TESTFN)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000887
Ezio Melottid5a23e32009-07-15 17:07:04 +0000888 def zip_test(self, f, compression):
889 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000890
891 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000892 with zipfile.ZipFile(f, "r", compression) as zipfp:
893 testdata = zipfp.read(TESTFN)
894 self.assertEqual(len(testdata), len(self.data))
895 self.assertEqual(testdata, self.data)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000896 self.assertEqual(zipfp.read("another.name"), self.data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000897
Ezio Melottid5a23e32009-07-15 17:07:04 +0000898 def test_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000899 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000900 self.zip_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +0000901
Ezio Melottid5a23e32009-07-15 17:07:04 +0000902 def zip_open_test(self, f, compression):
903 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000904
905 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000906 with zipfile.ZipFile(f, "r", compression) as zipfp:
907 zipdata1 = []
908 zipopen1 = zipfp.open(TESTFN)
909 while True:
910 read_data = zipopen1.read(256)
911 if not read_data:
912 break
913 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000914
Ezio Melotti569e61f2009-12-30 06:14:51 +0000915 zipdata2 = []
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000916 zipopen2 = zipfp.open("another.name")
Ezio Melotti569e61f2009-12-30 06:14:51 +0000917 while True:
918 read_data = zipopen2.read(256)
919 if not read_data:
920 break
921 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +0000922
Ezio Melotti569e61f2009-12-30 06:14:51 +0000923 testdata1 = ''.join(zipdata1)
924 self.assertEqual(len(testdata1), len(self.data))
925 self.assertEqual(testdata1, self.data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000926
Ezio Melotti569e61f2009-12-30 06:14:51 +0000927 testdata2 = ''.join(zipdata2)
Ezio Melotti6d6b53c2009-12-31 13:00:43 +0000928 self.assertEqual(len(testdata2), len(self.data))
929 self.assertEqual(testdata2, self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000930
Ezio Melottid5a23e32009-07-15 17:07:04 +0000931 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000932 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000933 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000934
Ezio Melottid5a23e32009-07-15 17:07:04 +0000935 def zip_random_open_test(self, f, compression):
936 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000937
938 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000939 with zipfile.ZipFile(f, "r", compression) as zipfp:
940 zipdata1 = []
941 zipopen1 = zipfp.open(TESTFN)
942 while True:
943 read_data = zipopen1.read(randint(1, 1024))
944 if not read_data:
945 break
946 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000947
Ezio Melotti569e61f2009-12-30 06:14:51 +0000948 testdata = ''.join(zipdata1)
949 self.assertEqual(len(testdata), len(self.data))
950 self.assertEqual(testdata, self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000951
Ezio Melottid5a23e32009-07-15 17:07:04 +0000952 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000953 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000954 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000955
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000956
Ezio Melotti1036a7f2009-09-12 14:43:43 +0000957@skipUnless(zlib, "requires zlib")
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000958class TestsWithMultipleOpens(unittest.TestCase):
959 def setUp(self):
960 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000961 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
962 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
963 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
Tim Petersea5962f2007-03-12 18:07:52 +0000964
Ezio Melottid5a23e32009-07-15 17:07:04 +0000965 def test_same_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000966 # Verify that (when the ZipFile is in control of creating file objects)
967 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +0000968 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
969 zopen1 = zipf.open('ones')
970 zopen2 = zipf.open('ones')
971 data1 = zopen1.read(500)
972 data2 = zopen2.read(500)
973 data1 += zopen1.read(500)
974 data2 += zopen2.read(500)
975 self.assertEqual(data1, data2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000976
Ezio Melottid5a23e32009-07-15 17:07:04 +0000977 def test_different_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000978 # Verify that (when the ZipFile is in control of creating file objects)
979 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +0000980 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
981 zopen1 = zipf.open('ones')
982 zopen2 = zipf.open('twos')
983 data1 = zopen1.read(500)
984 data2 = zopen2.read(500)
985 data1 += zopen1.read(500)
986 data2 += zopen2.read(500)
987 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
988 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000989
Ezio Melottid5a23e32009-07-15 17:07:04 +0000990 def test_interleaved(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000991 # Verify that (when the ZipFile is in control of creating file objects)
992 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +0000993 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
994 zopen1 = zipf.open('ones')
995 data1 = zopen1.read(500)
996 zopen2 = zipf.open('twos')
997 data2 = zopen2.read(500)
998 data1 += zopen1.read(500)
999 data2 += zopen2.read(500)
1000 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1001 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
Tim Petersea5962f2007-03-12 18:07:52 +00001002
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001003 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001004 unlink(TESTFN2)
1005
Tim Petersea5962f2007-03-12 18:07:52 +00001006
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001007class TestWithDirectory(unittest.TestCase):
1008 def setUp(self):
1009 os.mkdir(TESTFN2)
1010
Ezio Melottid5a23e32009-07-15 17:07:04 +00001011 def test_extract_dir(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +00001012 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
1013 zipf.extractall(TESTFN2)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001014 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1015 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1016 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1017
Ezio Melottid5a23e32009-07-15 17:07:04 +00001018 def test_bug_6050(self):
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001019 # Extraction should succeed if directories already exist
1020 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottid5a23e32009-07-15 17:07:04 +00001021 self.test_extract_dir()
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001022
Ezio Melottid5a23e32009-07-15 17:07:04 +00001023 def test_store_dir(self):
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001024 os.mkdir(os.path.join(TESTFN2, "x"))
1025 zipf = zipfile.ZipFile(TESTFN, "w")
1026 zipf.write(os.path.join(TESTFN2, "x"), "x")
1027 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1028
1029 def tearDown(self):
1030 shutil.rmtree(TESTFN2)
1031 if os.path.exists(TESTFN):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001032 unlink(TESTFN)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001033
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001034
1035class UniversalNewlineTests(unittest.TestCase):
1036 def setUp(self):
Ezio Melotti6d6b53c2009-12-31 13:00:43 +00001037 self.line_gen = ["Test of zipfile line %d." % i
1038 for i in xrange(FIXEDTEST_SIZE)]
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001039 self.seps = ('\r', '\r\n', '\n')
1040 self.arcdata, self.arcfiles = {}, {}
1041 for n, s in enumerate(self.seps):
1042 self.arcdata[s] = s.join(self.line_gen) + s
1043 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Brett Cannon6cef0762007-05-25 20:17:15 +00001044 open(self.arcfiles[s], "wb").write(self.arcdata[s])
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001045
Ezio Melottid5a23e32009-07-15 17:07:04 +00001046 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001047 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001048 with zipfile.ZipFile(f, "w", compression) as zipfp:
1049 for fn in self.arcfiles.values():
1050 zipfp.write(fn, fn)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001051
Ezio Melottid5a23e32009-07-15 17:07:04 +00001052 def read_test(self, f, compression):
1053 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001054
1055 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001056 with zipfile.ZipFile(f, "r") as zipfp:
1057 for sep, fn in self.arcfiles.items():
1058 zipdata = zipfp.open(fn, "rU").read()
1059 self.assertEqual(self.arcdata[sep], zipdata)
Tim Petersea5962f2007-03-12 18:07:52 +00001060
Ezio Melottid5a23e32009-07-15 17:07:04 +00001061 def readline_test(self, f, compression):
1062 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001063
1064 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001065 with zipfile.ZipFile(f, "r") as zipfp:
1066 for sep, fn in self.arcfiles.items():
1067 zipopen = zipfp.open(fn, "rU")
1068 for line in self.line_gen:
1069 linedata = zipopen.readline()
1070 self.assertEqual(linedata, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001071
Ezio Melottid5a23e32009-07-15 17:07:04 +00001072 def readlines_test(self, f, compression):
1073 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001074
1075 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001076 with zipfile.ZipFile(f, "r") as zipfp:
1077 for sep, fn in self.arcfiles.items():
1078 ziplines = zipfp.open(fn, "rU").readlines()
1079 for line, zipline in zip(self.line_gen, ziplines):
1080 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001081
Ezio Melottid5a23e32009-07-15 17:07:04 +00001082 def iterlines_test(self, f, compression):
1083 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001084
1085 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001086 with zipfile.ZipFile(f, "r") as zipfp:
1087 for sep, fn in self.arcfiles.items():
1088 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
1089 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001090
Ezio Melottid5a23e32009-07-15 17:07:04 +00001091 def test_read_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001092 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001093 self.read_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001094
Ezio Melottid5a23e32009-07-15 17:07:04 +00001095 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001096 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001097 self.readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001098
Ezio Melottid5a23e32009-07-15 17:07:04 +00001099 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001100 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001101 self.readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001102
Ezio Melottid5a23e32009-07-15 17:07:04 +00001103 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001104 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001105 self.iterlines_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001106
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001107 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001108 def test_read_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001109 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001110 self.read_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001111
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001112 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001113 def test_readline_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001114 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001115 self.readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001116
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001117 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001118 def test_readlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001119 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001120 self.readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001121
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001122 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001123 def test_iterlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001124 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001125 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001126
1127 def tearDown(self):
1128 for sep, fn in self.arcfiles.items():
1129 os.remove(fn)
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001130 unlink(TESTFN)
1131 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001132
1133
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001134def test_main():
Tim Petersea5962f2007-03-12 18:07:52 +00001135 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1136 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001137 TestWithDirectory, UniversalNewlineTests,
1138 TestsWithRandomBinaryFiles)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001139
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001140if __name__ == "__main__":
1141 test_main()