我想压缩文件并使用 python 计算压缩文件的校验和。我第一次天真的尝试是使用 2 个函数:
def compress_file(input_filename, output_filename):
f_in = open(input_filename, 'rb')
f_out = gzip.open(output_filename, 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()
def md5sum(filename):
with open(filename) as f:
md5 = hashlib.md5(f.read()).hexdigest()
return md5
但是,它会导致压缩文件被写入然后重新读取。对于许多文件(> 10 000),压缩时每个文件数 MB,在 NFS 安装的驱动器中,速度很慢。
如何在缓冲区中压缩文件,然后在写入输出文件之前从该缓冲区计算校验和?
文件不是那么大,所以我可以负担得起将所有内容存储在内存中。然而,一个不错的增量版本也可能不错。
最后一个要求是它应该与多处理一起工作(以便并行压缩多个文件)。
我尝试使用 zlib.compress 但返回的字符串缺少 gzip 文件的 header 。
编辑:关注@abarnert sggestion , 我用的是 python3 gzip.compress:
def compress_md5(input_filename, output_filename):
f_in = open(input_filename, 'rb')
# Read in buffer
buff = f_in.read()
f_in.close()
# Compress this buffer
c_buff = gzip.compress(buff)
# Compute MD5
md5 = hashlib.md5(c_buff).hexdigest()
# Write compressed buffer
f_out = open(output_filename, 'wb')
f_out.write(c_buff)
f_out.close()
return md5
这会生成一个正确的 gzip 文件,但每次运行的输出都不同(md5 不同):
>>> compress_md5('4327_010.pdf', '4327_010.pdf.gz')
'0d0eb6a5f3fe2c1f3201bc3360201f71'
>>> compress_md5('4327_010.pdf', '4327_010.pdf.gz')
'8e4954ab5914a1dd0d8d0deb114640e5'
gzip 程序没有这个问题:
$ gzip -c 4327_010.pdf | md5sum
8965184bc4dace5325c41cc75c5837f1 -
$ gzip -c 4327_010.pdf | md5sum
8965184bc4dace5325c41cc75c5837f1 -
我猜这是因为 gzip 模块在创建文件时默认使用当前时间(我猜 gzip 程序使用输入文件的修改)。无法使用 gzip.compress 更改它。
我想在读/写模式下创建一个gzip.GzipFile,控制mtime,但是gzip.GzipFile没有这样的模式。
灵感来自 @zwol suggestion我编写了以下函数,它在 header 中正确设置了文件名和操作系统 (Unix):
def compress_md5(input_filename, output_filename):
f_in = open(input_filename, 'rb')
# Read data in buffer
buff = f_in.read()
# Create output buffer
c_buff = cStringIO.StringIO()
# Create gzip file
input_file_stat = os.stat(input_filename)
mtime = input_file_stat[8]
gzip_obj = gzip.GzipFile(input_filename, mode="wb", fileobj=c_buff, mtime=mtime)
# Compress data in memory
gzip_obj.write(buff)
# Close files
f_in.close()
gzip_obj.close()
# Retrieve compressed data
c_data = c_buff.getvalue()
# Change OS value
c_data = c_data[0:9] + '\003' + c_data[10:]
# Really write compressed data
f_out = open(output_filename, "wb")
f_out.write(c_data)
# Compute MD5
md5 = hashlib.md5(c_data).hexdigest()
return md5
不同的运行输出是一样的。此外,file 的输出与 gzip 相同:
$ gzip -9 -c 4327_010.pdf > ref_max/4327_010.pdf.gz
$ file ref_max/4327_010.pdf.gz
ref_max/4327_010.pdf.gz: gzip compressed data, was "4327_010.pdf", from Unix, last modified: Tue May 5 14:28:16 2015, max compression
$ file 4327_010.pdf.gz
4327_010.pdf.gz: gzip compressed data, was "4327_010.pdf", from Unix, last modified: Tue May 5 14:28:16 2015, max compression
但是md5不同:
$ md5sum 4327_010.pdf.gz ref_max/4327_010.pdf.gz
39dc3e5a52c71a25c53fcbc02e2702d5 4327_010.pdf.gz
213a599a382cd887f3c4f963e1d3dec4 ref_max/4327_010.pdf.gz
gzip -l 也不同:
$ gzip -l ref_max/4327_010.pdf.gz 4327_010.pdf.gz
compressed uncompressed ratio uncompressed_name
7286404 7600522 4.1% ref_max/4327_010.pdf
7297310 7600522 4.0% 4327_010.pdf
我猜是因为gzip 程序和python gzip 模块(基于C 库zlib)略有不同算法。
请您参考如下方法:
包裹一个gzip.GzipFile io.BytesIO 周围的物体目的。 (在 Python 2 中,请改用 cStringIO.StringIO。)关闭 GzipFile 后,您可以从 BytesIO 对象中检索压缩数据(使用 getvalue),对其进行哈希处理,然后将其写入真实文件。
