Extract files from zip file and retain mod date?

前端 未结 4 1365
迷失自我
迷失自我 2020-12-05 14:48

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 (whic

4条回答
  •  既然无缘
    2020-12-05 15:11

    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.

提交回复
热议问题