Question

I am trying to change the last modified time of all the files and folders present in a zip file. If I try the script line-by-line in interpreter, the last-modified time seems to have been changed. But the same is not reflected in the zip file.

I am using the zipfile module in python.
Below is my source code.

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()
Était-ce utile?

La solution

This is not directly possible with Python's default zipfile module. See e.g. this question: overwriting file in ziparchive

If you look in ZipFile class code, you'll see that in close method it will just append data to the end of original zip file. To modify file dates, you will have to use some site package like myZip or implement this functionality yourself.

Alternative way is to fully unpack your zip file and repack it again with updated date&time. See answers to above-mentioned question for details.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top