/Lib/test/test_zipfile.py

http://unladen-swallow.googlecode.com/ · Python · 1125 lines · 805 code · 211 blank · 109 comment · 104 complexity · 8cfd8fff14c414500f50a9104d601528 MD5 · raw file

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