Permalink
Browse files

fixed parsing of timestamps (Bugzilla #15)

  • Loading branch information...
1 parent 6cdc4ab commit 3c490775acfe25c2f25971040ff028e7f4fbe450 @fancycode committed Sep 12, 2011
Showing with 18 additions and 1 deletion.
  1. +1 −0 doc/THANKS.txt
  2. +12 −1 py7zlib.py
  3. +5 −0 tests/test_7zfiles.py
View
@@ -9,3 +9,4 @@ Nikolaus Rath - nikolaus <at> rath <dot> org
David Agnew - prescindor <at> gmail <dot> com
Sofring Chow - sofring912 <at> gmail <dot> com
David Turon - turon.david <at> seznam <dot> cz
+Ray Gardner - raygard <at> gmail <dot> com
View
@@ -97,6 +97,15 @@ def bytes(s, encoding):
COMPRESSION_METHOD_MISC_BZIP = unhexlify('0402') # '\x04\x02'
COMPRESSION_METHOD_7Z_AES256_SHA256 = unhexlify('06f10701') # '\x06\xf1\x07\x01'
+# number of seconds between 1601/01/01 and 1970/01/01
+# used to adjust 7z FILETIME to Python timestamp
+TIMESTAMP_ADJUST = -11644477200
+
+def toTimestamp(filetime):
+ """Convert 7z FILETIME to Python timestamp."""
+ # FILETIME is 100-nanosecond intervals since 1601/01/01 (UTC)
+ return (filetime / 10000000.0) + TIMESTAMP_ADJUST
+
class ArchiveError(Exception):
pass
@@ -384,9 +393,11 @@ class FilesInfo(Base):
def _readTimes(self, file, files, name):
defined = self._readBoolean(file, len(files), checkall=1)
+ # NOTE: the "external" flag is currently ignored, should be 0x00
+ external = file.read(1)
for i in range(len(files)):
if defined[i]:
- files[i][name] = self._readReal64Bit(file)[0] #unpack('<L', file.read(4))[0]
+ files[i][name] = toTimestamp(self._readReal64Bit(file)[0])
else:
files[i][name] = None
View
@@ -22,6 +22,7 @@
#
# $Id$
#
+from datetime import datetime
import os
import pylzma
from py7zlib import Archive7z, NoPasswordGivenError, WrongPasswordError
@@ -58,12 +59,16 @@ def _test_archive(self, filename):
self.failUnlessEqual(archive.getmember('test2.txt'), None)
cf = archive.getmember('test1.txt')
self.failUnless(cf.checkcrc())
+ self.failUnlessEqual(datetime.fromtimestamp(cf.lastwritetime).replace(microsecond=0), \
+ datetime(2006, 3, 15, 21, 43, 48))
self.failUnlessEqual(cf.read(), bytes('This file is located in the root.', 'ascii'))
cf.reset()
self.failUnlessEqual(cf.read(), bytes('This file is located in the root.', 'ascii'))
cf = archive.getmember('test/test2.txt')
self.failUnless(cf.checkcrc())
+ self.failUnlessEqual(datetime.fromtimestamp(cf.lastwritetime).replace(microsecond=0), \
+ datetime(2006, 3, 15, 21, 43, 36))
self.failUnlessEqual(cf.read(), bytes('This file is located in a folder.', 'ascii'))
cf.reset()
self.failUnlessEqual(cf.read(), bytes('This file is located in a folder.', 'ascii'))

0 comments on commit 3c49077

Please sign in to comment.