Python gzip omitting original filename and timestamp

Folks, I am creating an md5sum of a gzip file. Technically every time you compress the same file, but the md5sum result is different. How can I tell to use a flag -n

to omit the original filename and timestamp?

f_in = open(tmpFile, 'rb')
f_out = gzip.open(uploadFile, 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()

      

Thank!

+3


source to share


2 answers


The class GzipFile

allows you to explicitly specify the filename and timestamp for the header.

eg:.



#!/usr/bin/python
import sys
import gzip

f = open('out.gz', 'wb')
gz = gzip.GzipFile('', 'wb', 9, f, 0.)
gz.write(str.encode('this is a test'))
gz.close()
f.close()

      

This will create a gzip header with no filename and zero modification time, which means no modification time for RFC 1952 for gzip.

+3


source


If you want to write utf-8 text to a gz file without the filename in the header, here's a way to do it:



import gzip, io

ofile = open("./stuff.txt.gz", 'wb')
ogzfile = gzip.GzipFile('', 'w', 9, ofile, 0.)
ogztextfile = io.TextIOWrapper(ogzfile, 'utf-8')

ogztextfile.write("  \n \n")

ogztextfile.close()
ogzfile.close()
ofile.close()

      

0


source







All Articles