Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract files from zip file and retain mod date?

Tags:

I'm trying to extract files from a zip file using Python 2.7.1 (on Windows, fyi) and each of my attempts shows extracted files with Modified Date = time of extraction (which is incorrect).

import os,zipfile outDirectory = 'C:\\_TEMP\\' inFile = 'test.zip' fh = open(os.path.join(outDirectory,inFile),'rb')  z = zipfile.ZipFile(fh) for name in z.namelist():     z.extract(name,outDirectory) fh.close() 

I also tried using the .extractall method, with the same results.

import os,zipfile outDirectory = 'C:\\_TEMP\\' inFile = 'test.zip' zFile = zipfile.ZipFile(os.path.join(outDirectory,inFile))         zFile.extractall(outDirectory) 

Can anyone tell me what I'm doing wrong?

I'd like to think this is possible without having to post-correct the modified time per How do I change the file creation date of a Windows file?.

like image 495
MTAdmin Avatar asked Mar 21 '12 21:03

MTAdmin


People also ask

Do zip files preserve timestamps?

Sub-folders retain the timestamp of the zip archive. However, if the folder is on the top level, it will lose the original timestamp and assume the timestamp of when it's extracted.

Does zip preserve metadata?

One way of preserving metadata when moving files around is to first put them into a 'container' file (like a ZIP file). That way, the ZIP's metadata gets changed but its contents remain untouched.

How do I change the date on a ZIP file?

Select the file (or folder) where you want to change the date/time attribute. It will show up as an entry on a list. Click on Actions and then Change Time/Attributes. Change the Date Created or Date Modified attribute.

Do you have to extract zip files for mods?

Windows can only unzip zip files. For rar and 7z you'll need an extra program like winRar or 7zip. If they are . package files, they go in your mods folder without unzipping.


1 Answers

Well, it does take a little post-processing, but it's not that bad:

import os import zipfile import time  outDirectory = 'C:\\TEMP\\' inFile = 'test.zip' fh = open(os.path.join(outDirectory,inFile),'rb')  z = zipfile.ZipFile(fh)  for f in z.infolist():     name, date_time = f.filename, f.date_time     name = os.path.join(outDirectory, name)     with open(name, 'wb') as outFile:         outFile.write(z.open(f).read())     date_time = time.mktime(date_time + (0, 0, -1))     os.utime(name, (date_time, date_time)) 

Okay, maybe it is that bad.

like image 159
Ethan Furman Avatar answered Sep 19 '22 11:09

Ethan Furman