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?.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With