|
1 # We can test part of the module without zlib. |
|
2 try: |
|
3 import zlib |
|
4 except ImportError: |
|
5 zlib = None |
|
6 |
|
7 import zipfile, os, unittest, sys, shutil, struct |
|
8 |
|
9 from StringIO import StringIO |
|
10 from tempfile import TemporaryFile |
|
11 from random import randint, random |
|
12 |
|
13 import test.test_support as support |
|
14 from test.test_support import TESTFN, run_unittest |
|
15 |
|
16 TESTFN2 = TESTFN + "2" |
|
17 FIXEDTEST_SIZE = 1000 |
|
18 |
|
19 SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'), |
|
20 ('ziptest2dir/_ziptest2', 'qawsedrftg'), |
|
21 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'), |
|
22 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')] |
|
23 |
|
24 class TestsWithSourceFile(unittest.TestCase): |
|
25 def setUp(self): |
|
26 self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random()) |
|
27 for i in xrange(FIXEDTEST_SIZE)] |
|
28 self.data = '\n'.join(self.line_gen) + '\n' |
|
29 |
|
30 # Make a source file with some lines |
|
31 fp = open(TESTFN, "wb") |
|
32 fp.write(self.data) |
|
33 fp.close() |
|
34 |
|
35 def makeTestArchive(self, f, compression): |
|
36 # Create the ZIP archive |
|
37 zipfp = zipfile.ZipFile(f, "w", compression) |
|
38 zipfp.write(TESTFN, "another"+os.extsep+"name") |
|
39 zipfp.write(TESTFN, TESTFN) |
|
40 zipfp.writestr("strfile", self.data) |
|
41 zipfp.close() |
|
42 |
|
43 def zipTest(self, f, compression): |
|
44 self.makeTestArchive(f, compression) |
|
45 |
|
46 # Read the ZIP archive |
|
47 zipfp = zipfile.ZipFile(f, "r", compression) |
|
48 self.assertEqual(zipfp.read(TESTFN), self.data) |
|
49 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data) |
|
50 self.assertEqual(zipfp.read("strfile"), self.data) |
|
51 |
|
52 # Print the ZIP directory |
|
53 fp = StringIO() |
|
54 stdout = sys.stdout |
|
55 try: |
|
56 sys.stdout = fp |
|
57 |
|
58 zipfp.printdir() |
|
59 finally: |
|
60 sys.stdout = stdout |
|
61 |
|
62 directory = fp.getvalue() |
|
63 lines = directory.splitlines() |
|
64 self.assertEquals(len(lines), 4) # Number of files + header |
|
65 |
|
66 self.assert_('File Name' in lines[0]) |
|
67 self.assert_('Modified' in lines[0]) |
|
68 self.assert_('Size' in lines[0]) |
|
69 |
|
70 fn, date, time, size = lines[1].split() |
|
71 self.assertEquals(fn, 'another.name') |
|
72 # XXX: timestamp is not tested |
|
73 self.assertEquals(size, str(len(self.data))) |
|
74 |
|
75 # Check the namelist |
|
76 names = zipfp.namelist() |
|
77 self.assertEquals(len(names), 3) |
|
78 self.assert_(TESTFN in names) |
|
79 self.assert_("another"+os.extsep+"name" in names) |
|
80 self.assert_("strfile" in names) |
|
81 |
|
82 # Check infolist |
|
83 infos = zipfp.infolist() |
|
84 names = [ i.filename for i in infos ] |
|
85 self.assertEquals(len(names), 3) |
|
86 self.assert_(TESTFN in names) |
|
87 self.assert_("another"+os.extsep+"name" in names) |
|
88 self.assert_("strfile" in names) |
|
89 for i in infos: |
|
90 self.assertEquals(i.file_size, len(self.data)) |
|
91 |
|
92 # check getinfo |
|
93 for nm in (TESTFN, "another"+os.extsep+"name", "strfile"): |
|
94 info = zipfp.getinfo(nm) |
|
95 self.assertEquals(info.filename, nm) |
|
96 self.assertEquals(info.file_size, len(self.data)) |
|
97 |
|
98 # Check that testzip doesn't raise an exception |
|
99 zipfp.testzip() |
|
100 zipfp.close() |
|
101 |
|
102 def testStored(self): |
|
103 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
104 self.zipTest(f, zipfile.ZIP_STORED) |
|
105 |
|
106 def zipOpenTest(self, f, compression): |
|
107 self.makeTestArchive(f, compression) |
|
108 |
|
109 # Read the ZIP archive |
|
110 zipfp = zipfile.ZipFile(f, "r", compression) |
|
111 zipdata1 = [] |
|
112 zipopen1 = zipfp.open(TESTFN) |
|
113 while 1: |
|
114 read_data = zipopen1.read(256) |
|
115 if not read_data: |
|
116 break |
|
117 zipdata1.append(read_data) |
|
118 |
|
119 zipdata2 = [] |
|
120 zipopen2 = zipfp.open("another"+os.extsep+"name") |
|
121 while 1: |
|
122 read_data = zipopen2.read(256) |
|
123 if not read_data: |
|
124 break |
|
125 zipdata2.append(read_data) |
|
126 |
|
127 self.assertEqual(''.join(zipdata1), self.data) |
|
128 self.assertEqual(''.join(zipdata2), self.data) |
|
129 zipfp.close() |
|
130 |
|
131 def testOpenStored(self): |
|
132 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
133 self.zipOpenTest(f, zipfile.ZIP_STORED) |
|
134 |
|
135 def testOpenViaZipInfo(self): |
|
136 # Create the ZIP archive |
|
137 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
|
138 zipfp.writestr("name", "foo") |
|
139 zipfp.writestr("name", "bar") |
|
140 zipfp.close() |
|
141 |
|
142 zipfp = zipfile.ZipFile(TESTFN2, "r") |
|
143 infos = zipfp.infolist() |
|
144 data = "" |
|
145 for info in infos: |
|
146 data += zipfp.open(info).read() |
|
147 self.assert_(data == "foobar" or data == "barfoo") |
|
148 data = "" |
|
149 for info in infos: |
|
150 data += zipfp.read(info) |
|
151 self.assert_(data == "foobar" or data == "barfoo") |
|
152 zipfp.close() |
|
153 |
|
154 def zipRandomOpenTest(self, f, compression): |
|
155 self.makeTestArchive(f, compression) |
|
156 |
|
157 # Read the ZIP archive |
|
158 zipfp = zipfile.ZipFile(f, "r", compression) |
|
159 zipdata1 = [] |
|
160 zipopen1 = zipfp.open(TESTFN) |
|
161 while 1: |
|
162 read_data = zipopen1.read(randint(1, 1024)) |
|
163 if not read_data: |
|
164 break |
|
165 zipdata1.append(read_data) |
|
166 |
|
167 self.assertEqual(''.join(zipdata1), self.data) |
|
168 zipfp.close() |
|
169 |
|
170 def testRandomOpenStored(self): |
|
171 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
172 self.zipRandomOpenTest(f, zipfile.ZIP_STORED) |
|
173 |
|
174 def zipReadlineTest(self, f, compression): |
|
175 self.makeTestArchive(f, compression) |
|
176 |
|
177 # Read the ZIP archive |
|
178 zipfp = zipfile.ZipFile(f, "r") |
|
179 zipopen = zipfp.open(TESTFN) |
|
180 for line in self.line_gen: |
|
181 linedata = zipopen.readline() |
|
182 self.assertEqual(linedata, line + '\n') |
|
183 |
|
184 zipfp.close() |
|
185 |
|
186 def zipReadlinesTest(self, f, compression): |
|
187 self.makeTestArchive(f, compression) |
|
188 |
|
189 # Read the ZIP archive |
|
190 zipfp = zipfile.ZipFile(f, "r") |
|
191 ziplines = zipfp.open(TESTFN).readlines() |
|
192 for line, zipline in zip(self.line_gen, ziplines): |
|
193 self.assertEqual(zipline, line + '\n') |
|
194 |
|
195 zipfp.close() |
|
196 |
|
197 def zipIterlinesTest(self, f, compression): |
|
198 self.makeTestArchive(f, compression) |
|
199 |
|
200 # Read the ZIP archive |
|
201 zipfp = zipfile.ZipFile(f, "r") |
|
202 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)): |
|
203 self.assertEqual(zipline, line + '\n') |
|
204 |
|
205 zipfp.close() |
|
206 |
|
207 def testReadlineStored(self): |
|
208 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
209 self.zipReadlineTest(f, zipfile.ZIP_STORED) |
|
210 |
|
211 def testReadlinesStored(self): |
|
212 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
213 self.zipReadlinesTest(f, zipfile.ZIP_STORED) |
|
214 |
|
215 def testIterlinesStored(self): |
|
216 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
217 self.zipIterlinesTest(f, zipfile.ZIP_STORED) |
|
218 |
|
219 if zlib: |
|
220 def testDeflated(self): |
|
221 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
222 self.zipTest(f, zipfile.ZIP_DEFLATED) |
|
223 |
|
224 def testOpenDeflated(self): |
|
225 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
226 self.zipOpenTest(f, zipfile.ZIP_DEFLATED) |
|
227 |
|
228 def testRandomOpenDeflated(self): |
|
229 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
230 self.zipRandomOpenTest(f, zipfile.ZIP_DEFLATED) |
|
231 |
|
232 def testReadlineDeflated(self): |
|
233 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
234 self.zipReadlineTest(f, zipfile.ZIP_DEFLATED) |
|
235 |
|
236 def testReadlinesDeflated(self): |
|
237 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
238 self.zipReadlinesTest(f, zipfile.ZIP_DEFLATED) |
|
239 |
|
240 def testIterlinesDeflated(self): |
|
241 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
242 self.zipIterlinesTest(f, zipfile.ZIP_DEFLATED) |
|
243 |
|
244 def testLowCompression(self): |
|
245 # Checks for cases where compressed data is larger than original |
|
246 # Create the ZIP archive |
|
247 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) |
|
248 zipfp.writestr("strfile", '12') |
|
249 zipfp.close() |
|
250 |
|
251 # Get an open object for strfile |
|
252 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) |
|
253 openobj = zipfp.open("strfile") |
|
254 self.assertEqual(openobj.read(1), '1') |
|
255 self.assertEqual(openobj.read(1), '2') |
|
256 |
|
257 def testAbsoluteArcnames(self): |
|
258 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
|
259 zipfp.write(TESTFN, "/absolute") |
|
260 zipfp.close() |
|
261 |
|
262 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) |
|
263 self.assertEqual(zipfp.namelist(), ["absolute"]) |
|
264 zipfp.close() |
|
265 |
|
266 def testAppendToZipFile(self): |
|
267 # Test appending to an existing zipfile |
|
268 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
|
269 zipfp.write(TESTFN, TESTFN) |
|
270 zipfp.close() |
|
271 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) |
|
272 zipfp.writestr("strfile", self.data) |
|
273 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"]) |
|
274 zipfp.close() |
|
275 |
|
276 def testAppendToNonZipFile(self): |
|
277 # Test appending to an existing file that is not a zipfile |
|
278 # NOTE: this test fails if len(d) < 22 because of the first |
|
279 # line "fpin.seek(-22, 2)" in _EndRecData |
|
280 d = 'I am not a ZipFile!'*10 |
|
281 f = file(TESTFN2, 'wb') |
|
282 f.write(d) |
|
283 f.close() |
|
284 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) |
|
285 zipfp.write(TESTFN, TESTFN) |
|
286 zipfp.close() |
|
287 |
|
288 f = file(TESTFN2, 'rb') |
|
289 f.seek(len(d)) |
|
290 zipfp = zipfile.ZipFile(f, "r") |
|
291 self.assertEqual(zipfp.namelist(), [TESTFN]) |
|
292 zipfp.close() |
|
293 f.close() |
|
294 |
|
295 def test_WriteDefaultName(self): |
|
296 # Check that calling ZipFile.write without arcname specified produces the expected result |
|
297 zipfp = zipfile.ZipFile(TESTFN2, "w") |
|
298 zipfp.write(TESTFN) |
|
299 self.assertEqual(zipfp.read(TESTFN), file(TESTFN).read()) |
|
300 zipfp.close() |
|
301 |
|
302 def test_PerFileCompression(self): |
|
303 # Check that files within a Zip archive can have different compression options |
|
304 zipfp = zipfile.ZipFile(TESTFN2, "w") |
|
305 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED) |
|
306 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED) |
|
307 sinfo = zipfp.getinfo('storeme') |
|
308 dinfo = zipfp.getinfo('deflateme') |
|
309 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED) |
|
310 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED) |
|
311 zipfp.close() |
|
312 |
|
313 def test_WriteToReadonly(self): |
|
314 # Check that trying to call write() on a readonly ZipFile object |
|
315 # raises a RuntimeError |
|
316 zipf = zipfile.ZipFile(TESTFN2, mode="w") |
|
317 zipf.writestr("somefile.txt", "bogus") |
|
318 zipf.close() |
|
319 zipf = zipfile.ZipFile(TESTFN2, mode="r") |
|
320 self.assertRaises(RuntimeError, zipf.write, TESTFN) |
|
321 zipf.close() |
|
322 |
|
323 def testExtract(self): |
|
324 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
|
325 for fpath, fdata in SMALL_TEST_DATA: |
|
326 zipfp.writestr(fpath, fdata) |
|
327 zipfp.close() |
|
328 |
|
329 zipfp = zipfile.ZipFile(TESTFN2, "r") |
|
330 for fpath, fdata in SMALL_TEST_DATA: |
|
331 writtenfile = zipfp.extract(fpath) |
|
332 |
|
333 # make sure it was written to the right place |
|
334 if os.path.isabs(fpath): |
|
335 correctfile = os.path.join(os.getcwd(), fpath[1:]) |
|
336 else: |
|
337 correctfile = os.path.join(os.getcwd(), fpath) |
|
338 correctfile = os.path.normpath(correctfile) |
|
339 |
|
340 self.assertEqual(writtenfile, correctfile) |
|
341 |
|
342 # make sure correct data is in correct file |
|
343 self.assertEqual(fdata, file(writtenfile, "rb").read()) |
|
344 |
|
345 os.remove(writtenfile) |
|
346 |
|
347 zipfp.close() |
|
348 |
|
349 # remove the test file subdirectories |
|
350 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
|
351 |
|
352 def testExtractAll(self): |
|
353 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) |
|
354 for fpath, fdata in SMALL_TEST_DATA: |
|
355 zipfp.writestr(fpath, fdata) |
|
356 zipfp.close() |
|
357 |
|
358 zipfp = zipfile.ZipFile(TESTFN2, "r") |
|
359 zipfp.extractall() |
|
360 for fpath, fdata in SMALL_TEST_DATA: |
|
361 if os.path.isabs(fpath): |
|
362 outfile = os.path.join(os.getcwd(), fpath[1:]) |
|
363 else: |
|
364 outfile = os.path.join(os.getcwd(), fpath) |
|
365 |
|
366 self.assertEqual(fdata, file(outfile, "rb").read()) |
|
367 |
|
368 os.remove(outfile) |
|
369 |
|
370 zipfp.close() |
|
371 |
|
372 # remove the test file subdirectories |
|
373 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
|
374 |
|
375 def zip_test_writestr_permissions(self, f, compression): |
|
376 # Make sure that writestr creates files with mode 0600, |
|
377 # when it is passed a name rather than a ZipInfo instance. |
|
378 |
|
379 self.makeTestArchive(f, compression) |
|
380 zipfp = zipfile.ZipFile(f, "r") |
|
381 zinfo = zipfp.getinfo('strfile') |
|
382 self.assertEqual(zinfo.external_attr, 0600 << 16) |
|
383 |
|
384 def test_writestr_permissions(self): |
|
385 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
386 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED) |
|
387 |
|
388 def tearDown(self): |
|
389 os.remove(TESTFN) |
|
390 os.remove(TESTFN2) |
|
391 |
|
392 class TestZip64InSmallFiles(unittest.TestCase): |
|
393 # These tests test the ZIP64 functionality without using large files, |
|
394 # see test_zipfile64 for proper tests. |
|
395 |
|
396 def setUp(self): |
|
397 self._limit = zipfile.ZIP64_LIMIT |
|
398 zipfile.ZIP64_LIMIT = 5 |
|
399 |
|
400 line_gen = ("Test of zipfile line %d." % i for i in range(0, FIXEDTEST_SIZE)) |
|
401 self.data = '\n'.join(line_gen) |
|
402 |
|
403 # Make a source file with some lines |
|
404 fp = open(TESTFN, "wb") |
|
405 fp.write(self.data) |
|
406 fp.close() |
|
407 |
|
408 def largeFileExceptionTest(self, f, compression): |
|
409 zipfp = zipfile.ZipFile(f, "w", compression) |
|
410 self.assertRaises(zipfile.LargeZipFile, |
|
411 zipfp.write, TESTFN, "another"+os.extsep+"name") |
|
412 zipfp.close() |
|
413 |
|
414 def largeFileExceptionTest2(self, f, compression): |
|
415 zipfp = zipfile.ZipFile(f, "w", compression) |
|
416 self.assertRaises(zipfile.LargeZipFile, |
|
417 zipfp.writestr, "another"+os.extsep+"name", self.data) |
|
418 zipfp.close() |
|
419 |
|
420 def testLargeFileException(self): |
|
421 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
422 self.largeFileExceptionTest(f, zipfile.ZIP_STORED) |
|
423 self.largeFileExceptionTest2(f, zipfile.ZIP_STORED) |
|
424 |
|
425 def zipTest(self, f, compression): |
|
426 # Create the ZIP archive |
|
427 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True) |
|
428 zipfp.write(TESTFN, "another"+os.extsep+"name") |
|
429 zipfp.write(TESTFN, TESTFN) |
|
430 zipfp.writestr("strfile", self.data) |
|
431 zipfp.close() |
|
432 |
|
433 # Read the ZIP archive |
|
434 zipfp = zipfile.ZipFile(f, "r", compression) |
|
435 self.assertEqual(zipfp.read(TESTFN), self.data) |
|
436 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data) |
|
437 self.assertEqual(zipfp.read("strfile"), self.data) |
|
438 |
|
439 # Print the ZIP directory |
|
440 fp = StringIO() |
|
441 stdout = sys.stdout |
|
442 try: |
|
443 sys.stdout = fp |
|
444 |
|
445 zipfp.printdir() |
|
446 finally: |
|
447 sys.stdout = stdout |
|
448 |
|
449 directory = fp.getvalue() |
|
450 lines = directory.splitlines() |
|
451 self.assertEquals(len(lines), 4) # Number of files + header |
|
452 |
|
453 self.assert_('File Name' in lines[0]) |
|
454 self.assert_('Modified' in lines[0]) |
|
455 self.assert_('Size' in lines[0]) |
|
456 |
|
457 fn, date, time, size = lines[1].split() |
|
458 self.assertEquals(fn, 'another.name') |
|
459 # XXX: timestamp is not tested |
|
460 self.assertEquals(size, str(len(self.data))) |
|
461 |
|
462 # Check the namelist |
|
463 names = zipfp.namelist() |
|
464 self.assertEquals(len(names), 3) |
|
465 self.assert_(TESTFN in names) |
|
466 self.assert_("another"+os.extsep+"name" in names) |
|
467 self.assert_("strfile" in names) |
|
468 |
|
469 # Check infolist |
|
470 infos = zipfp.infolist() |
|
471 names = [ i.filename for i in infos ] |
|
472 self.assertEquals(len(names), 3) |
|
473 self.assert_(TESTFN in names) |
|
474 self.assert_("another"+os.extsep+"name" in names) |
|
475 self.assert_("strfile" in names) |
|
476 for i in infos: |
|
477 self.assertEquals(i.file_size, len(self.data)) |
|
478 |
|
479 # check getinfo |
|
480 for nm in (TESTFN, "another"+os.extsep+"name", "strfile"): |
|
481 info = zipfp.getinfo(nm) |
|
482 self.assertEquals(info.filename, nm) |
|
483 self.assertEquals(info.file_size, len(self.data)) |
|
484 |
|
485 # Check that testzip doesn't raise an exception |
|
486 zipfp.testzip() |
|
487 |
|
488 |
|
489 zipfp.close() |
|
490 |
|
491 def testStored(self): |
|
492 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
493 self.zipTest(f, zipfile.ZIP_STORED) |
|
494 |
|
495 |
|
496 if zlib: |
|
497 def testDeflated(self): |
|
498 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
499 self.zipTest(f, zipfile.ZIP_DEFLATED) |
|
500 |
|
501 def testAbsoluteArcnames(self): |
|
502 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True) |
|
503 zipfp.write(TESTFN, "/absolute") |
|
504 zipfp.close() |
|
505 |
|
506 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) |
|
507 self.assertEqual(zipfp.namelist(), ["absolute"]) |
|
508 zipfp.close() |
|
509 |
|
510 def tearDown(self): |
|
511 zipfile.ZIP64_LIMIT = self._limit |
|
512 os.remove(TESTFN) |
|
513 os.remove(TESTFN2) |
|
514 |
|
515 class PyZipFileTests(unittest.TestCase): |
|
516 def testWritePyfile(self): |
|
517 zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
|
518 fn = __file__ |
|
519 if fn.endswith('.pyc') or fn.endswith('.pyo'): |
|
520 fn = fn[:-1] |
|
521 |
|
522 zipfp.writepy(fn) |
|
523 |
|
524 bn = os.path.basename(fn) |
|
525 self.assert_(bn not in zipfp.namelist()) |
|
526 self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist()) |
|
527 zipfp.close() |
|
528 |
|
529 |
|
530 zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
|
531 fn = __file__ |
|
532 if fn.endswith('.pyc') or fn.endswith('.pyo'): |
|
533 fn = fn[:-1] |
|
534 |
|
535 zipfp.writepy(fn, "testpackage") |
|
536 |
|
537 bn = "%s/%s"%("testpackage", os.path.basename(fn)) |
|
538 self.assert_(bn not in zipfp.namelist()) |
|
539 self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist()) |
|
540 zipfp.close() |
|
541 |
|
542 def testWritePythonPackage(self): |
|
543 import email |
|
544 packagedir = os.path.dirname(email.__file__) |
|
545 |
|
546 zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
|
547 zipfp.writepy(packagedir) |
|
548 |
|
549 # Check for a couple of modules at different levels of the hieararchy |
|
550 names = zipfp.namelist() |
|
551 self.assert_('email/__init__.pyo' in names or 'email/__init__.pyc' in names) |
|
552 self.assert_('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names) |
|
553 |
|
554 def testWritePythonDirectory(self): |
|
555 os.mkdir(TESTFN2) |
|
556 try: |
|
557 fp = open(os.path.join(TESTFN2, "mod1.py"), "w") |
|
558 fp.write("print 42\n") |
|
559 fp.close() |
|
560 |
|
561 fp = open(os.path.join(TESTFN2, "mod2.py"), "w") |
|
562 fp.write("print 42 * 42\n") |
|
563 fp.close() |
|
564 |
|
565 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w") |
|
566 fp.write("bla bla bla\n") |
|
567 fp.close() |
|
568 |
|
569 zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
|
570 zipfp.writepy(TESTFN2) |
|
571 |
|
572 names = zipfp.namelist() |
|
573 self.assert_('mod1.pyc' in names or 'mod1.pyo' in names) |
|
574 self.assert_('mod2.pyc' in names or 'mod2.pyo' in names) |
|
575 self.assert_('mod2.txt' not in names) |
|
576 |
|
577 finally: |
|
578 shutil.rmtree(TESTFN2) |
|
579 |
|
580 def testWriteNonPyfile(self): |
|
581 zipfp = zipfile.PyZipFile(TemporaryFile(), "w") |
|
582 file(TESTFN, 'w').write('most definitely not a python file') |
|
583 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) |
|
584 os.remove(TESTFN) |
|
585 |
|
586 |
|
587 class OtherTests(unittest.TestCase): |
|
588 def testUnicodeFilenames(self): |
|
589 zf = zipfile.ZipFile(TESTFN, "w") |
|
590 zf.writestr(u"foo.txt", "Test for unicode filename") |
|
591 zf.writestr(u"\xf6.txt", "Test for unicode filename") |
|
592 self.assertTrue(isinstance(zf.infolist()[0].filename, unicode)) |
|
593 zf.close() |
|
594 zf = zipfile.ZipFile(TESTFN, "r") |
|
595 self.assertEqual(zf.filelist[0].filename, "foo.txt") |
|
596 self.assertEqual(zf.filelist[1].filename, u"\xf6.txt") |
|
597 zf.close() |
|
598 |
|
599 def testCreateNonExistentFileForAppend(self): |
|
600 if os.path.exists(TESTFN): |
|
601 os.unlink(TESTFN) |
|
602 |
|
603 filename = 'testfile.txt' |
|
604 content = 'hello, world. this is some content.' |
|
605 |
|
606 try: |
|
607 zf = zipfile.ZipFile(TESTFN, 'a') |
|
608 zf.writestr(filename, content) |
|
609 zf.close() |
|
610 except IOError, (errno, errmsg): |
|
611 self.fail('Could not append data to a non-existent zip file.') |
|
612 |
|
613 self.assert_(os.path.exists(TESTFN)) |
|
614 |
|
615 zf = zipfile.ZipFile(TESTFN, 'r') |
|
616 self.assertEqual(zf.read(filename), content) |
|
617 zf.close() |
|
618 |
|
619 def testCloseErroneousFile(self): |
|
620 # This test checks that the ZipFile constructor closes the file object |
|
621 # it opens if there's an error in the file. If it doesn't, the traceback |
|
622 # holds a reference to the ZipFile object and, indirectly, the file object. |
|
623 # On Windows, this causes the os.unlink() call to fail because the |
|
624 # underlying file is still open. This is SF bug #412214. |
|
625 # |
|
626 fp = open(TESTFN, "w") |
|
627 fp.write("this is not a legal zip file\n") |
|
628 fp.close() |
|
629 try: |
|
630 zf = zipfile.ZipFile(TESTFN) |
|
631 except zipfile.BadZipfile: |
|
632 pass |
|
633 |
|
634 def testIsZipErroneousFile(self): |
|
635 # This test checks that the is_zipfile function correctly identifies |
|
636 # a file that is not a zip file |
|
637 fp = open(TESTFN, "w") |
|
638 fp.write("this is not a legal zip file\n") |
|
639 fp.close() |
|
640 chk = zipfile.is_zipfile(TESTFN) |
|
641 self.assert_(chk is False) |
|
642 |
|
643 def testIsZipValidFile(self): |
|
644 # This test checks that the is_zipfile function correctly identifies |
|
645 # a file that is a zip file |
|
646 zipf = zipfile.ZipFile(TESTFN, mode="w") |
|
647 zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
|
648 zipf.close() |
|
649 chk = zipfile.is_zipfile(TESTFN) |
|
650 self.assert_(chk is True) |
|
651 |
|
652 def testNonExistentFileRaisesIOError(self): |
|
653 # make sure we don't raise an AttributeError when a partially-constructed |
|
654 # ZipFile instance is finalized; this tests for regression on SF tracker |
|
655 # bug #403871. |
|
656 |
|
657 # The bug we're testing for caused an AttributeError to be raised |
|
658 # when a ZipFile instance was created for a file that did not |
|
659 # exist; the .fp member was not initialized but was needed by the |
|
660 # __del__() method. Since the AttributeError is in the __del__(), |
|
661 # it is ignored, but the user should be sufficiently annoyed by |
|
662 # the message on the output that regression will be noticed |
|
663 # quickly. |
|
664 self.assertRaises(IOError, zipfile.ZipFile, TESTFN) |
|
665 |
|
666 def testClosedZipRaisesRuntimeError(self): |
|
667 # Verify that testzip() doesn't swallow inappropriate exceptions. |
|
668 data = StringIO() |
|
669 zipf = zipfile.ZipFile(data, mode="w") |
|
670 zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
|
671 zipf.close() |
|
672 |
|
673 # This is correct; calling .read on a closed ZipFile should throw |
|
674 # a RuntimeError, and so should calling .testzip. An earlier |
|
675 # version of .testzip would swallow this exception (and any other) |
|
676 # and report that the first file in the archive was corrupt. |
|
677 self.assertRaises(RuntimeError, zipf.read, "foo.txt") |
|
678 self.assertRaises(RuntimeError, zipf.open, "foo.txt") |
|
679 self.assertRaises(RuntimeError, zipf.testzip) |
|
680 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus") |
|
681 file(TESTFN, 'w').write('zipfile test data') |
|
682 self.assertRaises(RuntimeError, zipf.write, TESTFN) |
|
683 |
|
684 def test_BadConstructorMode(self): |
|
685 # Check that bad modes passed to ZipFile constructor are caught |
|
686 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q") |
|
687 |
|
688 def test_BadOpenMode(self): |
|
689 # Check that bad modes passed to ZipFile.open are caught |
|
690 zipf = zipfile.ZipFile(TESTFN, mode="w") |
|
691 zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
|
692 zipf.close() |
|
693 zipf = zipfile.ZipFile(TESTFN, mode="r") |
|
694 # read the data to make sure the file is there |
|
695 zipf.read("foo.txt") |
|
696 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q") |
|
697 zipf.close() |
|
698 |
|
699 def test_Read0(self): |
|
700 # Check that calling read(0) on a ZipExtFile object returns an empty |
|
701 # string and doesn't advance file pointer |
|
702 zipf = zipfile.ZipFile(TESTFN, mode="w") |
|
703 zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
|
704 # read the data to make sure the file is there |
|
705 f = zipf.open("foo.txt") |
|
706 for i in xrange(FIXEDTEST_SIZE): |
|
707 self.assertEqual(f.read(0), '') |
|
708 |
|
709 self.assertEqual(f.read(), "O, for a Muse of Fire!") |
|
710 zipf.close() |
|
711 |
|
712 def test_OpenNonexistentItem(self): |
|
713 # Check that attempting to call open() for an item that doesn't |
|
714 # exist in the archive raises a RuntimeError |
|
715 zipf = zipfile.ZipFile(TESTFN, mode="w") |
|
716 self.assertRaises(KeyError, zipf.open, "foo.txt", "r") |
|
717 |
|
718 def test_BadCompressionMode(self): |
|
719 # Check that bad compression methods passed to ZipFile.open are caught |
|
720 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1) |
|
721 |
|
722 def test_NullByteInFilename(self): |
|
723 # Check that a filename containing a null byte is properly terminated |
|
724 zipf = zipfile.ZipFile(TESTFN, mode="w") |
|
725 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!") |
|
726 self.assertEqual(zipf.namelist(), ['foo.txt']) |
|
727 |
|
728 def test_StructSizes(self): |
|
729 # check that ZIP internal structure sizes are calculated correctly |
|
730 self.assertEqual(zipfile.sizeEndCentDir, 22) |
|
731 self.assertEqual(zipfile.sizeCentralDir, 46) |
|
732 self.assertEqual(zipfile.sizeEndCentDir64, 56) |
|
733 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20) |
|
734 |
|
735 def testComments(self): |
|
736 # This test checks that comments on the archive are handled properly |
|
737 |
|
738 # check default comment is empty |
|
739 zipf = zipfile.ZipFile(TESTFN, mode="w") |
|
740 self.assertEqual(zipf.comment, '') |
|
741 zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
|
742 zipf.close() |
|
743 zipfr = zipfile.ZipFile(TESTFN, mode="r") |
|
744 self.assertEqual(zipfr.comment, '') |
|
745 zipfr.close() |
|
746 |
|
747 # check a simple short comment |
|
748 comment = 'Bravely taking to his feet, he beat a very brave retreat.' |
|
749 zipf = zipfile.ZipFile(TESTFN, mode="w") |
|
750 zipf.comment = comment |
|
751 zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
|
752 zipf.close() |
|
753 zipfr = zipfile.ZipFile(TESTFN, mode="r") |
|
754 self.assertEqual(zipfr.comment, comment) |
|
755 zipfr.close() |
|
756 |
|
757 # check a comment of max length |
|
758 comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)]) |
|
759 zipf = zipfile.ZipFile(TESTFN, mode="w") |
|
760 zipf.comment = comment2 |
|
761 zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
|
762 zipf.close() |
|
763 zipfr = zipfile.ZipFile(TESTFN, mode="r") |
|
764 self.assertEqual(zipfr.comment, comment2) |
|
765 zipfr.close() |
|
766 |
|
767 # check a comment that is too long is truncated |
|
768 zipf = zipfile.ZipFile(TESTFN, mode="w") |
|
769 zipf.comment = comment2 + 'oops' |
|
770 zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
|
771 zipf.close() |
|
772 zipfr = zipfile.ZipFile(TESTFN, mode="r") |
|
773 self.assertEqual(zipfr.comment, comment2) |
|
774 zipfr.close() |
|
775 |
|
776 def tearDown(self): |
|
777 support.unlink(TESTFN) |
|
778 support.unlink(TESTFN2) |
|
779 |
|
780 class DecryptionTests(unittest.TestCase): |
|
781 # This test checks that ZIP decryption works. Since the library does not |
|
782 # support encryption at the moment, we use a pre-generated encrypted |
|
783 # ZIP file |
|
784 |
|
785 data = ( |
|
786 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00' |
|
787 '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y' |
|
788 '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl' |
|
789 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00' |
|
790 '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81' |
|
791 '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00' |
|
792 '\x00\x00L\x00\x00\x00\x00\x00' ) |
|
793 data2 = ( |
|
794 'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02' |
|
795 '\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04' |
|
796 '\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0' |
|
797 'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03' |
|
798 '\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00' |
|
799 '\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze' |
|
800 'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01' |
|
801 '\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' ) |
|
802 |
|
803 plain = 'zipfile.py encryption test' |
|
804 plain2 = '\x00'*512 |
|
805 |
|
806 def setUp(self): |
|
807 fp = open(TESTFN, "wb") |
|
808 fp.write(self.data) |
|
809 fp.close() |
|
810 self.zip = zipfile.ZipFile(TESTFN, "r") |
|
811 fp = open(TESTFN2, "wb") |
|
812 fp.write(self.data2) |
|
813 fp.close() |
|
814 self.zip2 = zipfile.ZipFile(TESTFN2, "r") |
|
815 |
|
816 def tearDown(self): |
|
817 self.zip.close() |
|
818 os.unlink(TESTFN) |
|
819 self.zip2.close() |
|
820 os.unlink(TESTFN2) |
|
821 |
|
822 def testNoPassword(self): |
|
823 # Reading the encrypted file without password |
|
824 # must generate a RunTime exception |
|
825 self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
|
826 self.assertRaises(RuntimeError, self.zip2.read, "zero") |
|
827 |
|
828 def testBadPassword(self): |
|
829 self.zip.setpassword("perl") |
|
830 self.assertRaises(RuntimeError, self.zip.read, "test.txt") |
|
831 self.zip2.setpassword("perl") |
|
832 self.assertRaises(RuntimeError, self.zip2.read, "zero") |
|
833 |
|
834 def testGoodPassword(self): |
|
835 self.zip.setpassword("python") |
|
836 self.assertEquals(self.zip.read("test.txt"), self.plain) |
|
837 self.zip2.setpassword("12345") |
|
838 self.assertEquals(self.zip2.read("zero"), self.plain2) |
|
839 |
|
840 |
|
841 class TestsWithRandomBinaryFiles(unittest.TestCase): |
|
842 def setUp(self): |
|
843 datacount = randint(16, 64)*1024 + randint(1, 1024) |
|
844 self.data = ''.join((struct.pack('<f', random()*randint(-1000, 1000)) for i in xrange(datacount))) |
|
845 |
|
846 # Make a source file with some lines |
|
847 fp = open(TESTFN, "wb") |
|
848 fp.write(self.data) |
|
849 fp.close() |
|
850 |
|
851 def tearDown(self): |
|
852 support.unlink(TESTFN) |
|
853 support.unlink(TESTFN2) |
|
854 |
|
855 def makeTestArchive(self, f, compression): |
|
856 # Create the ZIP archive |
|
857 zipfp = zipfile.ZipFile(f, "w", compression) |
|
858 zipfp.write(TESTFN, "another"+os.extsep+"name") |
|
859 zipfp.write(TESTFN, TESTFN) |
|
860 zipfp.close() |
|
861 |
|
862 def zipTest(self, f, compression): |
|
863 self.makeTestArchive(f, compression) |
|
864 |
|
865 # Read the ZIP archive |
|
866 zipfp = zipfile.ZipFile(f, "r", compression) |
|
867 testdata = zipfp.read(TESTFN) |
|
868 self.assertEqual(len(testdata), len(self.data)) |
|
869 self.assertEqual(testdata, self.data) |
|
870 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data) |
|
871 zipfp.close() |
|
872 |
|
873 def testStored(self): |
|
874 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
875 self.zipTest(f, zipfile.ZIP_STORED) |
|
876 |
|
877 def zipOpenTest(self, f, compression): |
|
878 self.makeTestArchive(f, compression) |
|
879 |
|
880 # Read the ZIP archive |
|
881 zipfp = zipfile.ZipFile(f, "r", compression) |
|
882 zipdata1 = [] |
|
883 zipopen1 = zipfp.open(TESTFN) |
|
884 while 1: |
|
885 read_data = zipopen1.read(256) |
|
886 if not read_data: |
|
887 break |
|
888 zipdata1.append(read_data) |
|
889 |
|
890 zipdata2 = [] |
|
891 zipopen2 = zipfp.open("another"+os.extsep+"name") |
|
892 while 1: |
|
893 read_data = zipopen2.read(256) |
|
894 if not read_data: |
|
895 break |
|
896 zipdata2.append(read_data) |
|
897 |
|
898 testdata1 = ''.join(zipdata1) |
|
899 self.assertEqual(len(testdata1), len(self.data)) |
|
900 self.assertEqual(testdata1, self.data) |
|
901 |
|
902 testdata2 = ''.join(zipdata2) |
|
903 self.assertEqual(len(testdata1), len(self.data)) |
|
904 self.assertEqual(testdata1, self.data) |
|
905 zipfp.close() |
|
906 |
|
907 def testOpenStored(self): |
|
908 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
909 self.zipOpenTest(f, zipfile.ZIP_STORED) |
|
910 |
|
911 def zipRandomOpenTest(self, f, compression): |
|
912 self.makeTestArchive(f, compression) |
|
913 |
|
914 # Read the ZIP archive |
|
915 zipfp = zipfile.ZipFile(f, "r", compression) |
|
916 zipdata1 = [] |
|
917 zipopen1 = zipfp.open(TESTFN) |
|
918 while 1: |
|
919 read_data = zipopen1.read(randint(1, 1024)) |
|
920 if not read_data: |
|
921 break |
|
922 zipdata1.append(read_data) |
|
923 |
|
924 testdata = ''.join(zipdata1) |
|
925 self.assertEqual(len(testdata), len(self.data)) |
|
926 self.assertEqual(testdata, self.data) |
|
927 zipfp.close() |
|
928 |
|
929 def testRandomOpenStored(self): |
|
930 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
931 self.zipRandomOpenTest(f, zipfile.ZIP_STORED) |
|
932 |
|
933 class TestsWithMultipleOpens(unittest.TestCase): |
|
934 def setUp(self): |
|
935 # Create the ZIP archive |
|
936 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) |
|
937 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE) |
|
938 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE) |
|
939 zipfp.close() |
|
940 |
|
941 def testSameFile(self): |
|
942 # Verify that (when the ZipFile is in control of creating file objects) |
|
943 # multiple open() calls can be made without interfering with each other. |
|
944 zipf = zipfile.ZipFile(TESTFN2, mode="r") |
|
945 zopen1 = zipf.open('ones') |
|
946 zopen2 = zipf.open('ones') |
|
947 data1 = zopen1.read(500) |
|
948 data2 = zopen2.read(500) |
|
949 data1 += zopen1.read(500) |
|
950 data2 += zopen2.read(500) |
|
951 self.assertEqual(data1, data2) |
|
952 zipf.close() |
|
953 |
|
954 def testDifferentFile(self): |
|
955 # Verify that (when the ZipFile is in control of creating file objects) |
|
956 # multiple open() calls can be made without interfering with each other. |
|
957 zipf = zipfile.ZipFile(TESTFN2, mode="r") |
|
958 zopen1 = zipf.open('ones') |
|
959 zopen2 = zipf.open('twos') |
|
960 data1 = zopen1.read(500) |
|
961 data2 = zopen2.read(500) |
|
962 data1 += zopen1.read(500) |
|
963 data2 += zopen2.read(500) |
|
964 self.assertEqual(data1, '1'*FIXEDTEST_SIZE) |
|
965 self.assertEqual(data2, '2'*FIXEDTEST_SIZE) |
|
966 zipf.close() |
|
967 |
|
968 def testInterleaved(self): |
|
969 # Verify that (when the ZipFile is in control of creating file objects) |
|
970 # multiple open() calls can be made without interfering with each other. |
|
971 zipf = zipfile.ZipFile(TESTFN2, mode="r") |
|
972 zopen1 = zipf.open('ones') |
|
973 data1 = zopen1.read(500) |
|
974 zopen2 = zipf.open('twos') |
|
975 data2 = zopen2.read(500) |
|
976 data1 += zopen1.read(500) |
|
977 data2 += zopen2.read(500) |
|
978 self.assertEqual(data1, '1'*FIXEDTEST_SIZE) |
|
979 self.assertEqual(data2, '2'*FIXEDTEST_SIZE) |
|
980 zipf.close() |
|
981 |
|
982 def tearDown(self): |
|
983 os.remove(TESTFN2) |
|
984 |
|
985 |
|
986 class UniversalNewlineTests(unittest.TestCase): |
|
987 def setUp(self): |
|
988 self.line_gen = ["Test of zipfile line %d." % i for i in xrange(FIXEDTEST_SIZE)] |
|
989 self.seps = ('\r', '\r\n', '\n') |
|
990 self.arcdata, self.arcfiles = {}, {} |
|
991 for n, s in enumerate(self.seps): |
|
992 self.arcdata[s] = s.join(self.line_gen) + s |
|
993 self.arcfiles[s] = '%s-%d' % (TESTFN, n) |
|
994 open(self.arcfiles[s], "wb").write(self.arcdata[s]) |
|
995 |
|
996 def makeTestArchive(self, f, compression): |
|
997 # Create the ZIP archive |
|
998 zipfp = zipfile.ZipFile(f, "w", compression) |
|
999 for fn in self.arcfiles.values(): |
|
1000 zipfp.write(fn, fn) |
|
1001 zipfp.close() |
|
1002 |
|
1003 def readTest(self, f, compression): |
|
1004 self.makeTestArchive(f, compression) |
|
1005 |
|
1006 # Read the ZIP archive |
|
1007 zipfp = zipfile.ZipFile(f, "r") |
|
1008 for sep, fn in self.arcfiles.items(): |
|
1009 zipdata = zipfp.open(fn, "rU").read() |
|
1010 self.assertEqual(self.arcdata[sep], zipdata) |
|
1011 |
|
1012 zipfp.close() |
|
1013 |
|
1014 def readlineTest(self, f, compression): |
|
1015 self.makeTestArchive(f, compression) |
|
1016 |
|
1017 # Read the ZIP archive |
|
1018 zipfp = zipfile.ZipFile(f, "r") |
|
1019 for sep, fn in self.arcfiles.items(): |
|
1020 zipopen = zipfp.open(fn, "rU") |
|
1021 for line in self.line_gen: |
|
1022 linedata = zipopen.readline() |
|
1023 self.assertEqual(linedata, line + '\n') |
|
1024 |
|
1025 zipfp.close() |
|
1026 |
|
1027 def readlinesTest(self, f, compression): |
|
1028 self.makeTestArchive(f, compression) |
|
1029 |
|
1030 # Read the ZIP archive |
|
1031 zipfp = zipfile.ZipFile(f, "r") |
|
1032 for sep, fn in self.arcfiles.items(): |
|
1033 ziplines = zipfp.open(fn, "rU").readlines() |
|
1034 for line, zipline in zip(self.line_gen, ziplines): |
|
1035 self.assertEqual(zipline, line + '\n') |
|
1036 |
|
1037 zipfp.close() |
|
1038 |
|
1039 def iterlinesTest(self, f, compression): |
|
1040 self.makeTestArchive(f, compression) |
|
1041 |
|
1042 # Read the ZIP archive |
|
1043 zipfp = zipfile.ZipFile(f, "r") |
|
1044 for sep, fn in self.arcfiles.items(): |
|
1045 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")): |
|
1046 self.assertEqual(zipline, line + '\n') |
|
1047 |
|
1048 zipfp.close() |
|
1049 |
|
1050 def testReadStored(self): |
|
1051 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
1052 self.readTest(f, zipfile.ZIP_STORED) |
|
1053 |
|
1054 def testReadlineStored(self): |
|
1055 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
1056 self.readlineTest(f, zipfile.ZIP_STORED) |
|
1057 |
|
1058 def testReadlinesStored(self): |
|
1059 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
1060 self.readlinesTest(f, zipfile.ZIP_STORED) |
|
1061 |
|
1062 def testIterlinesStored(self): |
|
1063 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
1064 self.iterlinesTest(f, zipfile.ZIP_STORED) |
|
1065 |
|
1066 if zlib: |
|
1067 def testReadDeflated(self): |
|
1068 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
1069 self.readTest(f, zipfile.ZIP_DEFLATED) |
|
1070 |
|
1071 def testReadlineDeflated(self): |
|
1072 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
1073 self.readlineTest(f, zipfile.ZIP_DEFLATED) |
|
1074 |
|
1075 def testReadlinesDeflated(self): |
|
1076 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
1077 self.readlinesTest(f, zipfile.ZIP_DEFLATED) |
|
1078 |
|
1079 def testIterlinesDeflated(self): |
|
1080 for f in (TESTFN2, TemporaryFile(), StringIO()): |
|
1081 self.iterlinesTest(f, zipfile.ZIP_DEFLATED) |
|
1082 |
|
1083 def tearDown(self): |
|
1084 for sep, fn in self.arcfiles.items(): |
|
1085 os.remove(fn) |
|
1086 support.unlink(TESTFN) |
|
1087 support.unlink(TESTFN2) |
|
1088 |
|
1089 |
|
1090 def test_main(): |
|
1091 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests, |
|
1092 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens, |
|
1093 UniversalNewlineTests, TestsWithRandomBinaryFiles) |
|
1094 |
|
1095 if __name__ == "__main__": |
|
1096 test_main() |