blob: a66ec1eaa5bd2d275bf3d1fb021386e25a060f32 [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
9import shutil
10import struct
11import zipfile
12import unittest
Tim Petersa19a1682001-03-29 04:36:09 +000013
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000014from StringIO import StringIO
15from tempfile import TemporaryFile
Martin v. Löwis3eb76482007-03-06 10:41:24 +000016from random import randint, random
Ezio Melottie7a0cc22009-07-04 14:58:27 +000017from unittest import skipUnless
Tim Petersa19a1682001-03-29 04:36:09 +000018
Ezio Melotti6cbfc122009-07-10 20:25:56 +000019from test.test_support import TESTFN, run_unittest, findfile, unlink
Guido van Rossum368f04a2000-04-10 13:23:04 +000020
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000021TESTFN2 = TESTFN + "2"
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +000022TESTFNDIR = TESTFN + "d"
Georg Brandl4b3ab6f2007-07-12 09:59:22 +000023FIXEDTEST_SIZE = 1000
Guido van Rossum368f04a2000-04-10 13:23:04 +000024
Georg Brandl62416bc2008-01-07 18:47:44 +000025SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
26 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
27 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
28 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
29
Ezio Melotti6cbfc122009-07-10 20:25:56 +000030
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000031class TestsWithSourceFile(unittest.TestCase):
32 def setUp(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +000033 self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random())
34 for i in xrange(FIXEDTEST_SIZE)]
Martin v. Löwis3eb76482007-03-06 10:41:24 +000035 self.data = '\n'.join(self.line_gen) + '\n'
Fred Drake6e7e4852001-02-28 05:34:16 +000036
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +000037 # Make a source file with some lines
38 fp = open(TESTFN, "wb")
39 fp.write(self.data)
40 fp.close()
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:
45 zipfp.write(TESTFN, "another"+os.extsep+"name")
46 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)
55 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
56 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()
69 self.assertEquals(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 Melotti569e61f2009-12-30 06:14:51 +000075 fn, date, time, size = lines[1].split()
76 self.assertEquals(fn, 'another.name')
77 # XXX: timestamp is not tested
78 self.assertEquals(size, str(len(self.data)))
Ronald Oussoren143cefb2006-06-15 08:14:18 +000079
Ezio Melotti569e61f2009-12-30 06:14:51 +000080 # Check the namelist
81 names = zipfp.namelist()
82 self.assertEquals(len(names), 3)
83 self.assertTrue(TESTFN in names)
84 self.assertTrue("another"+os.extsep+"name" in names)
85 self.assertTrue("strfile" in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +000086
Ezio Melotti569e61f2009-12-30 06:14:51 +000087 # Check infolist
88 infos = zipfp.infolist()
89 names = [ i.filename for i in infos ]
90 self.assertEquals(len(names), 3)
91 self.assertTrue(TESTFN in names)
92 self.assertTrue("another"+os.extsep+"name" in names)
93 self.assertTrue("strfile" in names)
94 for i in infos:
95 self.assertEquals(i.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +000096
Ezio Melotti569e61f2009-12-30 06:14:51 +000097 # check getinfo
98 for nm in (TESTFN, "another"+os.extsep+"name", "strfile"):
99 info = zipfp.getinfo(nm)
100 self.assertEquals(info.filename, nm)
101 self.assertEquals(info.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000102
Ezio Melotti569e61f2009-12-30 06:14:51 +0000103 # Check that testzip doesn't raise an exception
104 zipfp.testzip()
Tim Peters7d3bad62001-04-04 18:56:49 +0000105
Ezio Melottid5a23e32009-07-15 17:07:04 +0000106 def test_stored(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000107 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000108 self.zip_test(f, zipfile.ZIP_STORED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000109
Ezio Melottid5a23e32009-07-15 17:07:04 +0000110 def zip_open_test(self, f, compression):
111 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000112
113 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000114 with zipfile.ZipFile(f, "r", compression) as zipfp:
115 zipdata1 = []
116 zipopen1 = zipfp.open(TESTFN)
117 while True:
118 read_data = zipopen1.read(256)
119 if not read_data:
120 break
121 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000122
Ezio Melotti569e61f2009-12-30 06:14:51 +0000123 zipdata2 = []
124 zipopen2 = zipfp.open("another"+os.extsep+"name")
125 while True:
126 read_data = zipopen2.read(256)
127 if not read_data:
128 break
129 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +0000130
Ezio Melotti569e61f2009-12-30 06:14:51 +0000131 self.assertEqual(''.join(zipdata1), self.data)
132 self.assertEqual(''.join(zipdata2), self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000133
Ezio Melottid5a23e32009-07-15 17:07:04 +0000134 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000135 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000136 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000137
Ezio Melottid5a23e32009-07-15 17:07:04 +0000138 def test_open_via_zip_info(self):
Georg Brandl112aa502008-05-20 08:25:48 +0000139 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000140 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
141 zipfp.writestr("name", "foo")
142 zipfp.writestr("name", "bar")
Georg Brandl112aa502008-05-20 08:25:48 +0000143
Ezio Melotti569e61f2009-12-30 06:14:51 +0000144 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
145 infos = zipfp.infolist()
146 data = ""
147 for info in infos:
148 data += zipfp.open(info).read()
149 self.assertTrue(data == "foobar" or data == "barfoo")
150 data = ""
151 for info in infos:
152 data += zipfp.read(info)
153 self.assertTrue(data == "foobar" or data == "barfoo")
Georg Brandl112aa502008-05-20 08:25:48 +0000154
Ezio Melottid5a23e32009-07-15 17:07:04 +0000155 def zip_random_open_test(self, f, compression):
156 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000157
158 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000159 with zipfile.ZipFile(f, "r", compression) as zipfp:
160 zipdata1 = []
161 zipopen1 = zipfp.open(TESTFN)
162 while True:
163 read_data = zipopen1.read(randint(1, 1024))
164 if not read_data:
165 break
166 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000167
Ezio Melotti569e61f2009-12-30 06:14:51 +0000168 self.assertEqual(''.join(zipdata1), self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000169
Ezio Melottid5a23e32009-07-15 17:07:04 +0000170 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000171 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000172 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +0000173
Ezio Melottid5a23e32009-07-15 17:07:04 +0000174 def zip_readline_test(self, f, compression):
175 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000176
177 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000178 with zipfile.ZipFile(f, "r") as zipfp:
179 zipopen = zipfp.open(TESTFN)
180 for line in self.line_gen:
181 linedata = zipopen.readline()
182 self.assertEqual(linedata, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000183
Ezio Melottid5a23e32009-07-15 17:07:04 +0000184 def zip_readlines_test(self, f, compression):
185 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000186
187 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000188 with zipfile.ZipFile(f, "r") as zipfp:
189 ziplines = zipfp.open(TESTFN).readlines()
190 for line, zipline in zip(self.line_gen, ziplines):
191 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000192
Ezio Melottid5a23e32009-07-15 17:07:04 +0000193 def zip_iterlines_test(self, f, compression):
194 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000195
196 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000197 with zipfile.ZipFile(f, "r") as zipfp:
198 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
199 self.assertEqual(zipline, line + '\n')
Tim Petersea5962f2007-03-12 18:07:52 +0000200
Ezio Melottid5a23e32009-07-15 17:07:04 +0000201 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000202 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000203 self.zip_readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000204
Ezio Melottid5a23e32009-07-15 17:07:04 +0000205 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000206 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000207 self.zip_readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000208
Ezio Melottid5a23e32009-07-15 17:07:04 +0000209 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000210 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000211 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000212
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000213 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000214 def test_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000215 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000216 self.zip_test(f, zipfile.ZIP_DEFLATED)
Raymond Hettingerc0fac962003-06-27 22:25:03 +0000217
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000218 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000219 def test_open_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000220 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000221 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000222
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000223 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000224 def test_random_open_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000225 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000226 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000227
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000228 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000229 def test_readline_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000230 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000231 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000232
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000233 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000234 def test_readlines_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000235 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000236 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000237
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000238 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000239 def test_iterlines_deflated(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000240 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000241 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
Tim Petersea5962f2007-03-12 18:07:52 +0000242
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000243 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000244 def test_low_compression(self):
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000245 # Checks for cases where compressed data is larger than original
246 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000247 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
248 zipfp.writestr("strfile", '12')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000249
Ezio Melottie7a0cc22009-07-04 14:58:27 +0000250 # Get an open object for strfile
Ezio Melotti569e61f2009-12-30 06:14:51 +0000251 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
252 openobj = zipfp.open("strfile")
253 self.assertEqual(openobj.read(1), '1')
254 self.assertEqual(openobj.read(1), '2')
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000255
Ezio Melottid5a23e32009-07-15 17:07:04 +0000256 def test_absolute_arcnames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000257 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
258 zipfp.write(TESTFN, "/absolute")
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000259
Ezio Melotti569e61f2009-12-30 06:14:51 +0000260 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
261 self.assertEqual(zipfp.namelist(), ["absolute"])
Tim Peters32cbc962006-02-20 21:42:18 +0000262
Ezio Melottid5a23e32009-07-15 17:07:04 +0000263 def test_append_to_zip_file(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000264 # Test appending to an existing zipfile
Ezio Melotti569e61f2009-12-30 06:14:51 +0000265 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
266 zipfp.write(TESTFN, TESTFN)
267
268 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
269 zipfp.writestr("strfile", self.data)
270 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000271
Ezio Melottid5a23e32009-07-15 17:07:04 +0000272 def test_append_to_non_zip_file(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000273 # Test appending to an existing file that is not a zipfile
274 # NOTE: this test fails if len(d) < 22 because of the first
275 # line "fpin.seek(-22, 2)" in _EndRecData
276 d = 'I am not a ZipFile!'*10
277 f = file(TESTFN2, 'wb')
278 f.write(d)
279 f.close()
Ezio Melotti569e61f2009-12-30 06:14:51 +0000280 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
281 zipfp.write(TESTFN, TESTFN)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000282
283 f = file(TESTFN2, 'rb')
284 f.seek(len(d))
Ezio Melotti569e61f2009-12-30 06:14:51 +0000285 with zipfile.ZipFile(f, "r") as zipfp:
286 self.assertEqual(zipfp.namelist(), [TESTFN])
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000287 f.close()
288
Ezio Melottid5a23e32009-07-15 17:07:04 +0000289 def test_write_default_name(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000290 # Check that calling ZipFile.write without arcname specified produces the expected result
Ezio Melotti569e61f2009-12-30 06:14:51 +0000291 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
292 zipfp.write(TESTFN)
293 self.assertEqual(zipfp.read(TESTFN), file(TESTFN).read())
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000294
Ezio Melotti1036a7f2009-09-12 14:43:43 +0000295 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000296 def test_per_file_compression(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000297 # Check that files within a Zip archive can have different compression options
Ezio Melotti569e61f2009-12-30 06:14:51 +0000298 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
299 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
300 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
301 sinfo = zipfp.getinfo('storeme')
302 dinfo = zipfp.getinfo('deflateme')
303 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
304 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000305
Ezio Melottid5a23e32009-07-15 17:07:04 +0000306 def test_write_to_readonly(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000307 # Check that trying to call write() on a readonly ZipFile object
308 # raises a RuntimeError
Ezio Melotti569e61f2009-12-30 06:14:51 +0000309 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
310 zipfp.writestr("somefile.txt", "bogus")
311
312 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
313 self.assertRaises(RuntimeError, zipfp.write, TESTFN)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000314
Ezio Melottid5a23e32009-07-15 17:07:04 +0000315 def test_extract(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000316 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
317 for fpath, fdata in SMALL_TEST_DATA:
318 zipfp.writestr(fpath, fdata)
Georg Brandl62416bc2008-01-07 18:47:44 +0000319
Ezio Melotti569e61f2009-12-30 06:14:51 +0000320 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
321 for fpath, fdata in SMALL_TEST_DATA:
322 writtenfile = zipfp.extract(fpath)
Georg Brandl62416bc2008-01-07 18:47:44 +0000323
Ezio Melotti569e61f2009-12-30 06:14:51 +0000324 # make sure it was written to the right place
325 if os.path.isabs(fpath):
326 correctfile = os.path.join(os.getcwd(), fpath[1:])
327 else:
328 correctfile = os.path.join(os.getcwd(), fpath)
329 correctfile = os.path.normpath(correctfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000330
Ezio Melotti569e61f2009-12-30 06:14:51 +0000331 self.assertEqual(writtenfile, correctfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000332
Ezio Melotti569e61f2009-12-30 06:14:51 +0000333 # make sure correct data is in correct file
334 self.assertEqual(fdata, file(writtenfile, "rb").read())
Georg Brandl62416bc2008-01-07 18:47:44 +0000335
Ezio Melotti569e61f2009-12-30 06:14:51 +0000336 os.remove(writtenfile)
Georg Brandl62416bc2008-01-07 18:47:44 +0000337
338 # remove the test file subdirectories
339 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
340
Ezio Melottid5a23e32009-07-15 17:07:04 +0000341 def test_extract_all(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000342 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
343 for fpath, fdata in SMALL_TEST_DATA:
344 zipfp.writestr(fpath, fdata)
Georg Brandl62416bc2008-01-07 18:47:44 +0000345
Ezio Melotti569e61f2009-12-30 06:14:51 +0000346 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
347 zipfp.extractall()
348 for fpath, fdata in SMALL_TEST_DATA:
349 if os.path.isabs(fpath):
350 outfile = os.path.join(os.getcwd(), fpath[1:])
351 else:
352 outfile = os.path.join(os.getcwd(), fpath)
Georg Brandl62416bc2008-01-07 18:47:44 +0000353
Ezio Melotti569e61f2009-12-30 06:14:51 +0000354 self.assertEqual(fdata, file(outfile, "rb").read())
Georg Brandl62416bc2008-01-07 18:47:44 +0000355
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
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000412 line_gen = ("Test of zipfile line %d." % i for i in range(0, FIXEDTEST_SIZE))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000413 self.data = '\n'.join(line_gen)
414
415 # Make a source file with some lines
416 fp = open(TESTFN, "wb")
417 fp.write(self.data)
418 fp.close()
419
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,
423 zipfp.write, TESTFN, "another"+os.extsep+"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,
428 zipfp.writestr, "another"+os.extsep+"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:
438 zipfp.write(TESTFN, "another"+os.extsep+"name")
439 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)
445 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
446 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()
459 self.assertEquals(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 Melotti569e61f2009-12-30 06:14:51 +0000465 fn, date, time, size = lines[1].split()
466 self.assertEquals(fn, 'another.name')
467 # XXX: timestamp is not tested
468 self.assertEquals(size, str(len(self.data)))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000469
Ezio Melotti569e61f2009-12-30 06:14:51 +0000470 # Check the namelist
471 names = zipfp.namelist()
472 self.assertEquals(len(names), 3)
473 self.assertTrue(TESTFN in names)
474 self.assertTrue("another"+os.extsep+"name" in names)
475 self.assertTrue("strfile" in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000476
Ezio Melotti569e61f2009-12-30 06:14:51 +0000477 # Check infolist
478 infos = zipfp.infolist()
479 names = [ i.filename for i in infos ]
480 self.assertEquals(len(names), 3)
481 self.assertTrue(TESTFN in names)
482 self.assertTrue("another"+os.extsep+"name" in names)
483 self.assertTrue("strfile" in names)
484 for i in infos:
485 self.assertEquals(i.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000486
Ezio Melotti569e61f2009-12-30 06:14:51 +0000487 # check getinfo
488 for nm in (TESTFN, "another"+os.extsep+"name", "strfile"):
489 info = zipfp.getinfo(nm)
490 self.assertEquals(info.filename, nm)
491 self.assertEquals(info.file_size, len(self.data))
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000492
Ezio Melotti569e61f2009-12-30 06:14:51 +0000493 # Check that testzip doesn't raise an exception
494 zipfp.testzip()
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000495
Ezio Melottid5a23e32009-07-15 17:07:04 +0000496 def test_stored(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000497 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000498 self.zip_test(f, zipfile.ZIP_STORED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000499
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000500 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000501 def test_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000502 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000503 self.zip_test(f, zipfile.ZIP_DEFLATED)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000504
Ezio Melottid5a23e32009-07-15 17:07:04 +0000505 def test_absolute_arcnames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000506 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
507 allowZip64=True) as zipfp:
508 zipfp.write(TESTFN, "/absolute")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000509
Ezio Melotti569e61f2009-12-30 06:14:51 +0000510 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
511 self.assertEqual(zipfp.namelist(), ["absolute"])
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000512
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000513 def tearDown(self):
514 zipfile.ZIP64_LIMIT = self._limit
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000515 unlink(TESTFN)
516 unlink(TESTFN2)
517
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000518
519class PyZipFileTests(unittest.TestCase):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000520 def test_write_pyfile(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000521 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
522 fn = __file__
523 if fn.endswith('.pyc') or fn.endswith('.pyo'):
524 fn = fn[:-1]
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000525
Ezio Melotti569e61f2009-12-30 06:14:51 +0000526 zipfp.writepy(fn)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000527
Ezio Melotti569e61f2009-12-30 06:14:51 +0000528 bn = os.path.basename(fn)
529 self.assertTrue(bn not in zipfp.namelist())
530 self.assertTrue(bn + 'o' in zipfp.namelist() or
531 bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000532
Ezio Melotti569e61f2009-12-30 06:14:51 +0000533 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
534 fn = __file__
535 if fn.endswith('.pyc') or fn.endswith('.pyo'):
536 fn = fn[:-1]
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000537
Ezio Melotti569e61f2009-12-30 06:14:51 +0000538 zipfp.writepy(fn, "testpackage")
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000539
Ezio Melotti569e61f2009-12-30 06:14:51 +0000540 bn = "%s/%s"%("testpackage", os.path.basename(fn))
541 self.assertTrue(bn not in zipfp.namelist())
542 self.assertTrue(bn + 'o' in zipfp.namelist() or
543 bn + 'c' in zipfp.namelist())
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000544
Ezio Melottid5a23e32009-07-15 17:07:04 +0000545 def test_write_python_package(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000546 import email
547 packagedir = os.path.dirname(email.__file__)
548
Ezio Melotti569e61f2009-12-30 06:14:51 +0000549 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
550 zipfp.writepy(packagedir)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000551
Ezio Melotti569e61f2009-12-30 06:14:51 +0000552 # Check for a couple of modules at different levels of the hieararchy
553 names = zipfp.namelist()
554 self.assertTrue('email/__init__.pyo' in names or
555 'email/__init__.pyc' in names)
556 self.assertTrue('email/mime/text.pyo' in names or
557 'email/mime/text.pyc' in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000558
Ezio Melottid5a23e32009-07-15 17:07:04 +0000559 def test_write_python_directory(self):
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000560 os.mkdir(TESTFN2)
561 try:
562 fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
563 fp.write("print 42\n")
564 fp.close()
565
566 fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
567 fp.write("print 42 * 42\n")
568 fp.close()
569
570 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
571 fp.write("bla bla bla\n")
572 fp.close()
573
574 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
575 zipfp.writepy(TESTFN2)
576
577 names = zipfp.namelist()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000578 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
579 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
580 self.assertTrue('mod2.txt' not in names)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000581
582 finally:
583 shutil.rmtree(TESTFN2)
584
Ezio Melottid5a23e32009-07-15 17:07:04 +0000585 def test_write_non_pyfile(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000586 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
587 file(TESTFN, 'w').write('most definitely not a python file')
588 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
589 os.remove(TESTFN)
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000590
591
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000592class OtherTests(unittest.TestCase):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000593 def test_unicode_filenames(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +0000594 with zipfile.ZipFile(TESTFN, "w") as zf:
595 zf.writestr(u"foo.txt", "Test for unicode filename")
596 zf.writestr(u"\xf6.txt", "Test for unicode filename")
597 self.assertTrue(isinstance(zf.infolist()[0].filename, unicode))
598
599 with zipfile.ZipFile(TESTFN, "r") as zf:
600 self.assertEqual(zf.filelist[0].filename, "foo.txt")
601 self.assertEqual(zf.filelist[1].filename, u"\xf6.txt")
Martin v. Löwis471617d2008-05-05 17:16:58 +0000602
Ezio Melottid5a23e32009-07-15 17:07:04 +0000603 def test_create_non_existent_file_for_append(self):
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000604 if os.path.exists(TESTFN):
605 os.unlink(TESTFN)
Tim Petersea5962f2007-03-12 18:07:52 +0000606
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000607 filename = 'testfile.txt'
608 content = 'hello, world. this is some content.'
Tim Petersea5962f2007-03-12 18:07:52 +0000609
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000610 try:
Ezio Melotti569e61f2009-12-30 06:14:51 +0000611 with zipfile.ZipFile(TESTFN, 'a') as zf:
612 zf.writestr(filename, content)
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000613 except IOError:
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000614 self.fail('Could not append data to a non-existent zip file.')
615
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000616 self.assertTrue(os.path.exists(TESTFN))
Martin v. Löwis84f6de92007-02-13 10:10:39 +0000617
Ezio Melotti569e61f2009-12-30 06:14:51 +0000618 with zipfile.ZipFile(TESTFN, 'r') as zf:
619 self.assertEqual(zf.read(filename), content)
Tim Petersea5962f2007-03-12 18:07:52 +0000620
Ezio Melottid5a23e32009-07-15 17:07:04 +0000621 def test_close_erroneous_file(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000622 # This test checks that the ZipFile constructor closes the file object
623 # it opens if there's an error in the file. If it doesn't, the traceback
624 # holds a reference to the ZipFile object and, indirectly, the file object.
625 # On Windows, this causes the os.unlink() call to fail because the
626 # underlying file is still open. This is SF bug #412214.
627 #
628 fp = open(TESTFN, "w")
629 fp.write("this is not a legal zip file\n")
630 fp.close()
631 try:
632 zf = zipfile.ZipFile(TESTFN)
633 except zipfile.BadZipfile:
Collin Winter04a51ec2007-03-29 02:28:16 +0000634 pass
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000635
Ezio Melottid5a23e32009-07-15 17:07:04 +0000636 def test_is_zip_erroneous_file(self):
Tim Petersea5962f2007-03-12 18:07:52 +0000637 # This test checks that the is_zipfile function correctly identifies
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000638 # a file that is not a zip file
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000639
640 # - passing a filename
641 with open(TESTFN, "w") as fp:
642 fp.write("this is not a legal zip file\n")
Tim Petersea5962f2007-03-12 18:07:52 +0000643 chk = zipfile.is_zipfile(TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000644 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000645 # - passing a file object
646 with open(TESTFN, "rb") as fp:
647 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000648 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000649 # - passing a file-like object
650 fp = StringIO()
651 fp.write("this is not a legal zip file\n")
652 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000653 self.assertTrue(not chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000654 fp.seek(0,0)
655 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000656 self.assertTrue(not chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000657
Ezio Melottid5a23e32009-07-15 17:07:04 +0000658 def test_is_zip_valid_file(self):
Tim Petersea5962f2007-03-12 18:07:52 +0000659 # This test checks that the is_zipfile function correctly identifies
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000660 # a file that is a zip file
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000661
662 # - passing a filename
Ezio Melotti569e61f2009-12-30 06:14:51 +0000663 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
664 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Tim Petersea5962f2007-03-12 18:07:52 +0000665 chk = zipfile.is_zipfile(TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000666 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000667 # - passing a file object
668 with open(TESTFN, "rb") as fp:
669 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000670 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000671 fp.seek(0,0)
672 zip_contents = fp.read()
673 # - passing a file-like object
674 fp = StringIO()
675 fp.write(zip_contents)
676 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000677 self.assertTrue(chk)
Antoine Pitrou6f193e02008-12-27 15:43:12 +0000678 fp.seek(0,0)
679 chk = zipfile.is_zipfile(fp)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000680 self.assertTrue(chk)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000681
Ezio Melottid5a23e32009-07-15 17:07:04 +0000682 def test_non_existent_file_raises_IOError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000683 # make sure we don't raise an AttributeError when a partially-constructed
684 # ZipFile instance is finalized; this tests for regression on SF tracker
685 # bug #403871.
686
687 # The bug we're testing for caused an AttributeError to be raised
688 # when a ZipFile instance was created for a file that did not
689 # exist; the .fp member was not initialized but was needed by the
690 # __del__() method. Since the AttributeError is in the __del__(),
691 # it is ignored, but the user should be sufficiently annoyed by
692 # the message on the output that regression will be noticed
693 # quickly.
694 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
695
Amaury Forgeot d'Arc3e5b0272009-07-28 22:15:30 +0000696 def test_empty_file_raises_BadZipFile(self):
697 f = open(TESTFN, 'w')
698 f.close()
699 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
700
701 f = open(TESTFN, 'w')
702 f.write("short file")
703 f.close()
704 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
705
Ezio Melottid5a23e32009-07-15 17:07:04 +0000706 def test_closed_zip_raises_RuntimeError(self):
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000707 # Verify that testzip() doesn't swallow inappropriate exceptions.
708 data = StringIO()
Ezio Melotti569e61f2009-12-30 06:14:51 +0000709 with zipfile.ZipFile(data, mode="w") as zipf:
710 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000711
712 # This is correct; calling .read on a closed ZipFile should throw
713 # a RuntimeError, and so should calling .testzip. An earlier
714 # version of .testzip would swallow this exception (and any other)
715 # and report that the first file in the archive was corrupt.
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000716 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
717 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000718 self.assertRaises(RuntimeError, zipf.testzip)
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000719 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
720 file(TESTFN, 'w').write('zipfile test data')
721 self.assertRaises(RuntimeError, zipf.write, TESTFN)
722
Ezio Melottid5a23e32009-07-15 17:07:04 +0000723 def test_bad_constructor_mode(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000724 # Check that bad modes passed to ZipFile constructor are caught
725 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
726
Ezio Melottid5a23e32009-07-15 17:07:04 +0000727 def test_bad_open_mode(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000728 # Check that bad modes passed to ZipFile.open are caught
Ezio Melotti569e61f2009-12-30 06:14:51 +0000729 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
730 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
731
732 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000733 # read the data to make sure the file is there
Ezio Melotti569e61f2009-12-30 06:14:51 +0000734 zipf.read("foo.txt")
735 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000736
Ezio Melottid5a23e32009-07-15 17:07:04 +0000737 def test_read0(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000738 # Check that calling read(0) on a ZipExtFile object returns an empty
739 # string and doesn't advance file pointer
Ezio Melotti569e61f2009-12-30 06:14:51 +0000740 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
741 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
742 # read the data to make sure the file is there
743 f = zipf.open("foo.txt")
744 for i in xrange(FIXEDTEST_SIZE):
745 self.assertEqual(f.read(0), '')
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000746
Ezio Melotti569e61f2009-12-30 06:14:51 +0000747 self.assertEqual(f.read(), "O, for a Muse of Fire!")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000748
Ezio Melottid5a23e32009-07-15 17:07:04 +0000749 def test_open_non_existent_item(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000750 # Check that attempting to call open() for an item that doesn't
751 # exist in the archive raises a RuntimeError
Ezio Melotti569e61f2009-12-30 06:14:51 +0000752 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
753 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000754
Ezio Melottid5a23e32009-07-15 17:07:04 +0000755 def test_bad_compression_mode(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000756 # Check that bad compression methods passed to ZipFile.open are caught
757 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
758
Ezio Melottid5a23e32009-07-15 17:07:04 +0000759 def test_null_byte_in_filename(self):
Georg Brandl4b3ab6f2007-07-12 09:59:22 +0000760 # Check that a filename containing a null byte is properly terminated
Ezio Melotti569e61f2009-12-30 06:14:51 +0000761 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
762 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
763 self.assertEqual(zipf.namelist(), ['foo.txt'])
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000764
Ezio Melottid5a23e32009-07-15 17:07:04 +0000765 def test_struct_sizes(self):
Martin v. Löwis8c436412008-07-03 12:51:14 +0000766 # check that ZIP internal structure sizes are calculated correctly
767 self.assertEqual(zipfile.sizeEndCentDir, 22)
768 self.assertEqual(zipfile.sizeCentralDir, 46)
769 self.assertEqual(zipfile.sizeEndCentDir64, 56)
770 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
771
Ezio Melottid5a23e32009-07-15 17:07:04 +0000772 def test_comments(self):
Martin v. Löwis8c436412008-07-03 12:51:14 +0000773 # This test checks that comments on the archive are handled properly
774
775 # check default comment is empty
Ezio Melotti569e61f2009-12-30 06:14:51 +0000776 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
777 self.assertEqual(zipf.comment, '')
778 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
779
780 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
781 self.assertEqual(zipf.comment, '')
Martin v. Löwis8c436412008-07-03 12:51:14 +0000782
783 # check a simple short comment
784 comment = 'Bravely taking to his feet, he beat a very brave retreat.'
Ezio Melotti569e61f2009-12-30 06:14:51 +0000785 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
786 zipf.comment = comment
787 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
788 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
789 self.assertEqual(zipf.comment, comment)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000790
791 # check a comment of max length
792 comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)])
Ezio Melotti569e61f2009-12-30 06:14:51 +0000793 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
794 zipf.comment = comment2
795 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
796
797 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
798 self.assertEqual(zipf.comment, comment2)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000799
800 # check a comment that is too long is truncated
Ezio Melotti569e61f2009-12-30 06:14:51 +0000801 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
802 zipf.comment = comment2 + 'oops'
803 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
804 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
805 self.assertEqual(zipf.comment, comment2)
Martin v. Löwis8c436412008-07-03 12:51:14 +0000806
Collin Winter04a51ec2007-03-29 02:28:16 +0000807 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000808 unlink(TESTFN)
809 unlink(TESTFN2)
810
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +0000811
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000812class DecryptionTests(unittest.TestCase):
813 # This test checks that ZIP decryption works. Since the library does not
814 # support encryption at the moment, we use a pre-generated encrypted
815 # ZIP file
816
817 data = (
818 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
819 '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
820 '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
821 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
822 '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
823 '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
824 '\x00\x00L\x00\x00\x00\x00\x00' )
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000825 data2 = (
826 'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
827 '\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
828 '\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
829 'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
830 '\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
831 '\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
832 'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
833 '\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000834
835 plain = 'zipfile.py encryption test'
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000836 plain2 = '\x00'*512
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000837
838 def setUp(self):
839 fp = open(TESTFN, "wb")
840 fp.write(self.data)
841 fp.close()
842 self.zip = zipfile.ZipFile(TESTFN, "r")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000843 fp = open(TESTFN2, "wb")
844 fp.write(self.data2)
845 fp.close()
846 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000847
848 def tearDown(self):
849 self.zip.close()
850 os.unlink(TESTFN)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000851 self.zip2.close()
852 os.unlink(TESTFN2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000853
Ezio Melottid5a23e32009-07-15 17:07:04 +0000854 def test_no_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000855 # Reading the encrypted file without password
856 # must generate a RunTime exception
857 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000858 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000859
Ezio Melottid5a23e32009-07-15 17:07:04 +0000860 def test_bad_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000861 self.zip.setpassword("perl")
862 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000863 self.zip2.setpassword("perl")
864 self.assertRaises(RuntimeError, self.zip2.read, "zero")
Tim Petersea5962f2007-03-12 18:07:52 +0000865
Ezio Melotti1036a7f2009-09-12 14:43:43 +0000866 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +0000867 def test_good_password(self):
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000868 self.zip.setpassword("python")
869 self.assertEquals(self.zip.read("test.txt"), self.plain)
Gregory P. Smith0c63fc22008-01-20 01:21:03 +0000870 self.zip2.setpassword("12345")
871 self.assertEquals(self.zip2.read("zero"), self.plain2)
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000872
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000873
874class TestsWithRandomBinaryFiles(unittest.TestCase):
875 def setUp(self):
876 datacount = randint(16, 64)*1024 + randint(1, 1024)
877 self.data = ''.join((struct.pack('<f', random()*randint(-1000, 1000)) for i in xrange(datacount)))
878
879 # Make a source file with some lines
880 fp = open(TESTFN, "wb")
881 fp.write(self.data)
882 fp.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000883
Collin Winter04a51ec2007-03-29 02:28:16 +0000884 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000885 unlink(TESTFN)
886 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000887
Ezio Melottid5a23e32009-07-15 17:07:04 +0000888 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000889 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000890 with zipfile.ZipFile(f, "w", compression) as zipfp:
891 zipfp.write(TESTFN, "another"+os.extsep+"name")
892 zipfp.write(TESTFN, TESTFN)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000893
Ezio Melottid5a23e32009-07-15 17:07:04 +0000894 def zip_test(self, f, compression):
895 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000896
897 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000898 with zipfile.ZipFile(f, "r", compression) as zipfp:
899 testdata = zipfp.read(TESTFN)
900 self.assertEqual(len(testdata), len(self.data))
901 self.assertEqual(testdata, self.data)
902 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000903
Ezio Melottid5a23e32009-07-15 17:07:04 +0000904 def test_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000905 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000906 self.zip_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +0000907
Ezio Melottid5a23e32009-07-15 17:07:04 +0000908 def zip_open_test(self, f, compression):
909 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000910
911 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000912 with zipfile.ZipFile(f, "r", compression) as zipfp:
913 zipdata1 = []
914 zipopen1 = zipfp.open(TESTFN)
915 while True:
916 read_data = zipopen1.read(256)
917 if not read_data:
918 break
919 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000920
Ezio Melotti569e61f2009-12-30 06:14:51 +0000921 zipdata2 = []
922 zipopen2 = zipfp.open("another"+os.extsep+"name")
923 while True:
924 read_data = zipopen2.read(256)
925 if not read_data:
926 break
927 zipdata2.append(read_data)
Tim Petersea5962f2007-03-12 18:07:52 +0000928
Ezio Melotti569e61f2009-12-30 06:14:51 +0000929 testdata1 = ''.join(zipdata1)
930 self.assertEqual(len(testdata1), len(self.data))
931 self.assertEqual(testdata1, self.data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000932
Ezio Melotti569e61f2009-12-30 06:14:51 +0000933 testdata2 = ''.join(zipdata2)
934 self.assertEqual(len(testdata1), len(self.data))
935 self.assertEqual(testdata1, self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000936
Ezio Melottid5a23e32009-07-15 17:07:04 +0000937 def test_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000938 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000939 self.zip_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000940
Ezio Melottid5a23e32009-07-15 17:07:04 +0000941 def zip_random_open_test(self, f, compression):
942 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000943
944 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000945 with zipfile.ZipFile(f, "r", compression) as zipfp:
946 zipdata1 = []
947 zipopen1 = zipfp.open(TESTFN)
948 while True:
949 read_data = zipopen1.read(randint(1, 1024))
950 if not read_data:
951 break
952 zipdata1.append(read_data)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000953
Ezio Melotti569e61f2009-12-30 06:14:51 +0000954 testdata = ''.join(zipdata1)
955 self.assertEqual(len(testdata), len(self.data))
956 self.assertEqual(testdata, self.data)
Tim Petersea5962f2007-03-12 18:07:52 +0000957
Ezio Melottid5a23e32009-07-15 17:07:04 +0000958 def test_random_open_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000959 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +0000960 self.zip_random_open_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000961
Ezio Melotti6cbfc122009-07-10 20:25:56 +0000962
Ezio Melotti1036a7f2009-09-12 14:43:43 +0000963@skipUnless(zlib, "requires zlib")
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000964class TestsWithMultipleOpens(unittest.TestCase):
965 def setUp(self):
966 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +0000967 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
968 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
969 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
Tim Petersea5962f2007-03-12 18:07:52 +0000970
Ezio Melottid5a23e32009-07-15 17:07:04 +0000971 def test_same_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000972 # Verify that (when the ZipFile is in control of creating file objects)
973 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +0000974 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
975 zopen1 = zipf.open('ones')
976 zopen2 = zipf.open('ones')
977 data1 = zopen1.read(500)
978 data2 = zopen2.read(500)
979 data1 += zopen1.read(500)
980 data2 += zopen2.read(500)
981 self.assertEqual(data1, data2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000982
Ezio Melottid5a23e32009-07-15 17:07:04 +0000983 def test_different_file(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000984 # Verify that (when the ZipFile is in control of creating file objects)
985 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +0000986 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
987 zopen1 = zipf.open('ones')
988 zopen2 = zipf.open('twos')
989 data1 = zopen1.read(500)
990 data2 = zopen2.read(500)
991 data1 += zopen1.read(500)
992 data2 += zopen2.read(500)
993 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
994 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000995
Ezio Melottid5a23e32009-07-15 17:07:04 +0000996 def test_interleaved(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +0000997 # Verify that (when the ZipFile is in control of creating file objects)
998 # multiple open() calls can be made without interfering with each other.
Ezio Melotti569e61f2009-12-30 06:14:51 +0000999 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1000 zopen1 = zipf.open('ones')
1001 data1 = zopen1.read(500)
1002 zopen2 = zipf.open('twos')
1003 data2 = zopen2.read(500)
1004 data1 += zopen1.read(500)
1005 data2 += zopen2.read(500)
1006 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
1007 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
Tim Petersea5962f2007-03-12 18:07:52 +00001008
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001009 def tearDown(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001010 unlink(TESTFN2)
1011
Tim Petersea5962f2007-03-12 18:07:52 +00001012
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001013class TestWithDirectory(unittest.TestCase):
1014 def setUp(self):
1015 os.mkdir(TESTFN2)
1016
Ezio Melottid5a23e32009-07-15 17:07:04 +00001017 def test_extract_dir(self):
Ezio Melotti569e61f2009-12-30 06:14:51 +00001018 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
1019 zipf.extractall(TESTFN2)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001020 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
1021 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
1022 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
1023
Ezio Melottid5a23e32009-07-15 17:07:04 +00001024 def test_bug_6050(self):
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001025 # Extraction should succeed if directories already exist
1026 os.mkdir(os.path.join(TESTFN2, "a"))
Ezio Melottid5a23e32009-07-15 17:07:04 +00001027 self.test_extract_dir()
Martin v. Löwis0b09c422009-05-24 19:30:52 +00001028
Ezio Melottid5a23e32009-07-15 17:07:04 +00001029 def test_store_dir(self):
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001030 os.mkdir(os.path.join(TESTFN2, "x"))
1031 zipf = zipfile.ZipFile(TESTFN, "w")
1032 zipf.write(os.path.join(TESTFN2, "x"), "x")
1033 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
1034
1035 def tearDown(self):
1036 shutil.rmtree(TESTFN2)
1037 if os.path.exists(TESTFN):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001038 unlink(TESTFN)
Martin v. Löwis0dfcfc82009-01-24 14:00:33 +00001039
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001040
1041class UniversalNewlineTests(unittest.TestCase):
1042 def setUp(self):
1043 self.line_gen = ["Test of zipfile line %d." % i for i in xrange(FIXEDTEST_SIZE)]
1044 self.seps = ('\r', '\r\n', '\n')
1045 self.arcdata, self.arcfiles = {}, {}
1046 for n, s in enumerate(self.seps):
1047 self.arcdata[s] = s.join(self.line_gen) + s
1048 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
Brett Cannon6cef0762007-05-25 20:17:15 +00001049 open(self.arcfiles[s], "wb").write(self.arcdata[s])
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001050
Ezio Melottid5a23e32009-07-15 17:07:04 +00001051 def make_test_archive(self, f, compression):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001052 # Create the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001053 with zipfile.ZipFile(f, "w", compression) as zipfp:
1054 for fn in self.arcfiles.values():
1055 zipfp.write(fn, fn)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001056
Ezio Melottid5a23e32009-07-15 17:07:04 +00001057 def read_test(self, f, compression):
1058 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001059
1060 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001061 with zipfile.ZipFile(f, "r") as zipfp:
1062 for sep, fn in self.arcfiles.items():
1063 zipdata = zipfp.open(fn, "rU").read()
1064 self.assertEqual(self.arcdata[sep], zipdata)
Tim Petersea5962f2007-03-12 18:07:52 +00001065
Ezio Melottid5a23e32009-07-15 17:07:04 +00001066 def readline_test(self, f, compression):
1067 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001068
1069 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001070 with zipfile.ZipFile(f, "r") as zipfp:
1071 for sep, fn in self.arcfiles.items():
1072 zipopen = zipfp.open(fn, "rU")
1073 for line in self.line_gen:
1074 linedata = zipopen.readline()
1075 self.assertEqual(linedata, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001076
Ezio Melottid5a23e32009-07-15 17:07:04 +00001077 def readlines_test(self, f, compression):
1078 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001079
1080 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001081 with zipfile.ZipFile(f, "r") as zipfp:
1082 for sep, fn in self.arcfiles.items():
1083 ziplines = zipfp.open(fn, "rU").readlines()
1084 for line, zipline in zip(self.line_gen, ziplines):
1085 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001086
Ezio Melottid5a23e32009-07-15 17:07:04 +00001087 def iterlines_test(self, f, compression):
1088 self.make_test_archive(f, compression)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001089
1090 # Read the ZIP archive
Ezio Melotti569e61f2009-12-30 06:14:51 +00001091 with zipfile.ZipFile(f, "r") as zipfp:
1092 for sep, fn in self.arcfiles.items():
1093 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
1094 self.assertEqual(zipline, line + '\n')
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001095
Ezio Melottid5a23e32009-07-15 17:07:04 +00001096 def test_read_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001097 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001098 self.read_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001099
Ezio Melottid5a23e32009-07-15 17:07:04 +00001100 def test_readline_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001101 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001102 self.readline_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001103
Ezio Melottid5a23e32009-07-15 17:07:04 +00001104 def test_readlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001105 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001106 self.readlines_test(f, zipfile.ZIP_STORED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001107
Ezio Melottid5a23e32009-07-15 17:07:04 +00001108 def test_iterlines_stored(self):
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001109 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001110 self.iterlines_test(f, zipfile.ZIP_STORED)
Tim Petersea5962f2007-03-12 18:07:52 +00001111
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001112 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001113 def test_read_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.read_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_readline_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.readline_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_readlines_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.readlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001126
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001127 @skipUnless(zlib, "requires zlib")
Ezio Melottid5a23e32009-07-15 17:07:04 +00001128 def test_iterlines_deflated(self):
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001129 for f in (TESTFN2, TemporaryFile(), StringIO()):
Ezio Melottid5a23e32009-07-15 17:07:04 +00001130 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001131
1132 def tearDown(self):
1133 for sep, fn in self.arcfiles.items():
1134 os.remove(fn)
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001135 unlink(TESTFN)
1136 unlink(TESTFN2)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001137
1138
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001139def test_main():
Tim Petersea5962f2007-03-12 18:07:52 +00001140 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
1141 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
Ezio Melotti6cbfc122009-07-10 20:25:56 +00001142 TestWithDirectory, UniversalNewlineTests,
1143 TestsWithRandomBinaryFiles)
Martin v. Löwis3eb76482007-03-06 10:41:24 +00001144
Johannes Gijsbers3caf9c12004-08-19 15:11:50 +00001145if __name__ == "__main__":
1146 test_main()