问题
I'm stuck with setting up file time stamp, also as per python gzip document , the syntax not working like gzip.GzipFile(filename=outputfile,mode='wb',compresslevel=9,mtime=ftime) , but when I used gzip.GzipFile(outputfile,'wb',9,mtime=ftime) it's working but except time stamp.
def compresse_file(file,ftime):
data = open(file,'rb')
outputfile = file +".gz"
gzip_file = gzip.GzipFile(outputfile,'wb',9,mtime=ftime)
gzip_file.write(data.read())
gzip_file.flush()
gzip_file.close()
data.close()
os.unlink(file)
Here is output :
root@ubuntu:~/PythonPractice-# python compresses_file.py
Size Date File Name
5 MB 30/12/13 test.sh
Compressing...
test.sh 1388403823.0
file status after compressesion
5 kB 31/12/13 test.sh.gz
root@ubuntu:~/PythonPractice-# date -d @1388403823.0
Mon Dec 30 17:13:43 IST 2013
回答1:
As you can see in the documentation, the mtime argument is the timestamp that is written to the stream, it doesn't affect the timestamp of the created gzip file. This is the timestamp the decompressed file will have if decompressed using gunzip -N.
Example:
>>> import datetime
>>> import gzip
>>> ts = datetime.datetime(2010, 11, 12, 13, 14).timestamp()
>>> zf = gzip.GzipFile('test.gz', mode='wb', mtime=ts)
>>> zf.write(b'test')
>>> zf.flush()
>>> zf.close()
And decompressed:
$ gunzip -N test.gz
$ stat -c%y test
2010-11-12 13:14:00.000000000 +0100
If you want the created gzip file to have a specific timestamp, use os.utime to change it:
...
st = os.stat(file)
...
os.utime(outputfile, (st.st_atime, st.st_mtime))
...
来源:https://stackoverflow.com/questions/20856987/compresse-file-with-preserve-modified-time-stamp