Index: Lib/zipfile.py =================================================================== --- Lib/zipfile.py (revision 54876) +++ Lib/zipfile.py (working copy) @@ -706,7 +706,9 @@ """Print a table of contents for the zip file.""" print "%-46s %19s %12s" % ("File Name", "Modified ", "Size") for zinfo in self.filelist: - date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time + dt = zinfo.date_time + dinfo = (dt[0], dt[1], dt[2], dt[3], dt[4], dt[5]) + date = "%d-%02d-%02d %02d:%02d:%02d" % dinfo print "%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size) def testzip(self): @@ -891,8 +893,9 @@ 'bytes'. 'zinfo_or_arcname' is either a ZipInfo instance or the name of the file in the archive.""" if not isinstance(zinfo_or_arcname, ZipInfo): - zinfo = ZipInfo(filename=zinfo_or_arcname, - date_time=time.localtime(time.time())) + loctime = time.localtime(time.time()) + dt = loctime[0:6] + zinfo = ZipInfo(filename=zinfo_or_arcname, date_time=dt) zinfo.compress_type = self.compression else: zinfo = zinfo_or_arcname Index: Lib/test/test_zipfile.py =================================================================== --- Lib/test/test_zipfile.py (revision 54876) +++ Lib/test/test_zipfile.py (working copy) @@ -4,7 +4,13 @@ except ImportError: zlib = None -import zipfile, os, unittest, sys, shutil, struct +import zipfile +import os +import unittest +import sys +import shutil +import struct +import time from StringIO import StringIO from tempfile import TemporaryFile @@ -64,7 +70,9 @@ fn, date, time, size = lines[1].split() self.assertEquals(fn, 'another.name') - # XXX: timestamp is not tested + + # Note: timestamp is tested in OtherTests.testPrintDirModifiedTime + self.assertEquals(size, str(len(self.data))) # Check the namelist @@ -310,7 +318,9 @@ fn, date, time, size = lines[1].split() self.assertEquals(fn, 'another.name') - # XXX: timestamp is not tested + + # Note: timestamp is tested in OtherTests.testPrintDirModifiedTime + self.assertEquals(size, str(len(self.data))) # Check the namelist @@ -514,6 +524,38 @@ # version of .testzip would swallow this exception (and any other) # and report that the first file in the archive was corrupt. self.assertRaises(RuntimeError, zipf.testzip) + + def testPrintDirModifiedTime(self): + # This tests the fix for the situation addressed in bug #1698398, + # in which the ZipFile.printdir method was assuming that + # ZipInfo.date_time was a tuple (when it was a time.struct_time + # if a file was added with ZipFile.writestr). Although writestr + # was fixed to make date_time a tuple, this checks that printdir + # can cope with user-created ZipInfo objects that might be + # constructed in the same way. + + # Create the ZIP archive + zipf = zipfile.ZipFile(TESTFN, "w") + f1 = zipfile.ZipInfo("foo.txt", time.localtime()) + zipf.writestr(f1, "O, for a Muse of Fire!") + + # Print the ZIP directory + fp = StringIO() + stdout = sys.stdout + try: + sys.stdout = fp + + zipf.printdir() + finally: + sys.stdout = stdout + zipf.close() + + directory = fp.getvalue() + lines = directory.splitlines() + + fn, fdate, ftime, size = lines[1].split() + expected_date = time.strftime("%Y-%m-%d %H:%M:%S", f1.date_time) + self.assertEquals("%s %s" % (fdate, ftime), expected_date) def tearDown(self): support.unlink(TESTFN)