【问题标题】:Change the last-modified time of a zip file elements更改 zip 文件元素的最后修改时间
【发布时间】:2014-05-06 15:44:05
【问题描述】:

我正在尝试更改 zip 文件中存在的所有文件和文件夹的最后修改时间。 如果我在解释器中逐行尝试脚本,最后修改时间似乎已更改。但同样没有反映在 zip 文件中。

我在 python 中使用 zipfile 模块。
下面是我的源代码。

import zipfile as zp
import datetime
import datetime

def main():
    zipfile = raw_input("enter the path for your zipfile :")
    if os.path.exists(zipfile) and zp.is_zipfile(zipfile):
            time = raw_input("enter the time for which your files and folders in zip file want to be changed. format: dd-mm-yyyy HH:MM:SS -")
            if time is not None :
                time = datetime.datetime.strptime(time, "%d-%m-%Y %H:%M:%S")
                newtime = time
                time = (time.year, time.month, time.day, time.hour, time.minute, time.second)             
                print "time =", time
                z = zp.ZipFile(zipfile, "a", zp.ZIP_DEFLATED)
                z.printdir()
                try :
                    for i in range(len(z.filelist)):
                            z.infolist()[i].date_time = time
                    z.printdir()
                finally :
                    z.close()
            else :
                print "you have not entered a valid time!" 
        else :
            print " you have not entered a valid zipfile name. "

if __name__ == "__main__":
    main()

【问题讨论】:

    标签: python python-2.7 datetime zipfile


    【解决方案1】:

    这在 Python 的默认 zipfile 模块中无法直接实现。参见例如这个问题:overwriting file in ziparchive

    如果您查看ZipFile 类代码,您会看到在close 方法中它只会将数据附加到原始zip 文件的末尾。 要修改文件日期,您必须使用一些站点包,如 myZip 或自己实现此功能。

    另一种方法是完全解压缩您的 zip 文件,然后使用更新的日期和时间重新打包。有关详细信息,请参阅上述问题的答案。

    【讨论】:

      猜你喜欢
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      相关资源
      最近更新 更多