Compresse file with preserve modified time stamp


I'm stuck with setting up file time stamp, also as per python <a href="http://www.afpy.org/doc/python/2.7/library/gzip.html" rel="nofollow">gzip document</a> , 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


As you can see in the <a href="http://docs.python.org/3.3/library/gzip#gzip.GzipFile" rel="nofollow">documentation</a>, 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.


>>> 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:

<pre class="lang-none prettyprint-override">$ 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 <a href="http://docs.python.org/3.3/library/os#os.utime" rel="nofollow">os.utime</a> to change it:

... st = os.stat(file) ... os.utime(outputfile, (st.st_atime, st.st_mtime)) ...


  • python - calculate orthographic similarity between words of a list
  • Boost binary serialization doesn't work occasionally. The parsed data is corrupted sometimes
  • How to handle empty space in url when downloading image from web?
  • Serve file to user over http via php
  • How dotnet build chooses the output name
  • Upgrade project from WorkLight 6.1 to MobileFirst 7.1
  • What is this error in fortran and how to stop the program when it occurs?
  • Unable to connect to OnVif enabled camera using C#
  • How do I import an existing Grails 3 (3.0.12) project in IntelliJ 15
  • How to Save JSON data to SQL server database in C#?
  • File extension of zlib zipped html page?
  • Can't locate Module/Build.pm in @INC (@INC contains: /usr/local/lib64/perl5
  • Javascript CORS - No 'Access-Control-Allow-Origin' header is present
  • How to save a record and immediately use its GUID
  • Error processing multiple files
  • Repository Browser Only - \"Repository moved permanently to… please relocate”
  • opencv display image without x server
  • How do I shift the decimal place in Python?
  • Application level floating views with navigation in Android
  • Row to Column conversion in Talend
  • jQuery Orbit - How to make a Random Slideshow?
  • Is there a way to do normal logging with EureakLog?
  • PHP buffered output depending on server setting?
  • Bad request using file_get_contents for PUT request in PHP
  • Could not find rake using whenever rails
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Can I make an Android app that runs a web view in Chrome 39?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • coudnt use logback because of log4j
  • embed rChart in Markdown
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • Can't mass-assign protected attributes when import data from csv file
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app