summaryrefslogtreecommitdiff
path: root/libmat/archive.py
diff options
context:
space:
mode:
authorjvoisin2015-08-18 19:33:42 +0200
committerjvoisin2015-08-18 19:33:42 +0200
commit5d139d7e171494a026c1fef798c37a00504c95aa (patch)
tree2da153072305a7fc1670a350ec1fff6e2a1eab8d /libmat/archive.py
parentfcd5bdfdeb10175d0bbef9be2b1ac631dec703a5 (diff)
Improves the way MAT deals with ZipTimestamps
It seems that using os.utime is TZ-dependent, so lets use a ZipInfo thing instead.
Diffstat (limited to 'libmat/archive.py')
-rw-r--r--libmat/archive.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/libmat/archive.py b/libmat/archive.py
index 470b0cc..7a71717 100644
--- a/libmat/archive.py
+++ b/libmat/archive.py
@@ -14,9 +14,7 @@ import mat
14import parser 14import parser
15 15
16# Zip files do not support dates older than 01/01/1980 16# Zip files do not support dates older than 01/01/1980
17ZIP_EPOCH = (1980, 1, 1, 0, 0, 0) 17ZIP_EPOCH = (1980, 1, 1, 1, 0, 0)
18ZIP_EPOCH_SECONDS = (datetime.datetime(1980, 1, 1, 0, 0, 0)
19 - datetime.datetime(1970, 1, 1, 0, 0, 0)).total_seconds()
20 18
21 19
22class GenericArchiveStripper(parser.GenericParser): 20class GenericArchiveStripper(parser.GenericParser):
@@ -185,8 +183,14 @@ class ZipStripper(GenericArchiveStripper):
185 basename, ext = os.path.splitext(path) 183 basename, ext = os.path.splitext(path)
186 if not (self.add2archive or ext in parser.NOMETA): 184 if not (self.add2archive or ext in parser.NOMETA):
187 continue 185 continue
188 os.utime(path, (ZIP_EPOCH_SECONDS, ZIP_EPOCH_SECONDS)) 186 zinfo = zipfile.ZipInfo(item.filename, date_time=ZIP_EPOCH)
189 zipout.write(path, item.filename) 187 zinfo.compress_type = zipfile.ZIP_DEFLATED
188 zinfo.create_system = 3 # Linux
189 zinfo.comment = ''
190 with open(path, 'r') as f:
191 zipout.writestr(zinfo, f.read())
192 # os.utime(path, (ZIP_EPOCH_SECONDS, ZIP_EPOCH_SECONDS))
193 # zipout.write(path, item.filename)
190 zipin.close() 194 zipin.close()
191 zipout.close() 195 zipout.close()
192 196
@@ -199,7 +203,8 @@ class TarStripper(GenericArchiveStripper):
199 """ Represent a tarfile archive 203 """ Represent a tarfile archive
200 """ 204 """
201 205
202 def _remove(self, current_file): 206 @staticmethod
207 def _remove_tar_added(current_file):
203 """ Remove the meta added by tarfile itself to the file 208 """ Remove the meta added by tarfile itself to the file
204 """ 209 """
205 current_file.mtime = 0 210 current_file.mtime = 0
@@ -239,7 +244,7 @@ class TarStripper(GenericArchiveStripper):
239 continue 244 continue
240 tarout.add(unicode(path.decode('utf-8')), 245 tarout.add(unicode(path.decode('utf-8')),
241 unicode(item.name.decode('utf-8')), 246 unicode(item.name.decode('utf-8')),
242 filter=self._remove) 247 filter=self._remove_tar_added)
243 tarin.close() 248 tarin.close()
244 tarout.close() 249 tarout.close()
245 self.do_backup() 250 self.do_backup()
@@ -333,6 +338,7 @@ class TerminalZipStripper(ZipStripper):
333 It is used for formats like docx, which are basically 338 It is used for formats like docx, which are basically
334 ziped xml. 339 ziped xml.
335 """ 340 """
341 pass
336 342
337 343
338class GzipStripper(TarStripper): 344class GzipStripper(TarStripper):