我需要在不解压的情况下计算 zip 文件内容的 CRC32、MD5 和 SHA1。
到目前为止,我发现了如何为 zip 文件本身计算这些,例如:
CRC32:
import zlib
zip_name = "test.zip"
def Crc32Hasher(file_path):
buf_size = 65536
crc32 = 0
with open(file_path, 'rb') as f:
while True:
data = f.read(buf_size)
if not data:
break
crc32 = zlib.crc32(data, crc32)
return format(crc32 & 0xFFFFFFFF, '08x')
print(Crc32Hasher(zip_name))
SHA1:(MD5 类似)
import hashlib
zip_name = "test.zip"
def Sha1Hasher(file_path):
buf_size = 65536
sha1 = hashlib.sha1()
with open(file_path, 'rb') as f:
while True:
data = f.read(buf_size)
if not data:
break
sha1.update(data)
return format(sha1.hexdigest())
print(Sha1Hasher(zip_name))
对于zip文件的内容,我可以直接从zip中读取CRC32而不需要计算如下:
读取zip内容的CRC32:
import zipfile
zip_name = "test.zip"
if zip_name.lower().endswith(('.zip')):
z = zipfile.ZipFile(zip_name, "r")
for info in z.infolist():
print(info.filename,
format(info.CRC & 0xFFFFFFFF, '08x'))
但我不知道如何在不先解压缩 zip 文件的情况下计算其内容的 SHA1(或 MD5)。 这有可能吗?
最佳答案
这是不可能的。您可以获得 CRC,因为它是在创建存档时为您精心预先计算的(用于完整性检查)。任何其他校验和/哈希值都必须从头开始计算,并且至少需要对存档内容进行流式传输,即解包。
UPD:可能的实现
libarchive
:额外的依赖,支持多种归档格式
import libarchive.public as libarchive
with libarchive.file_reader(fname) as archive:
for entry in archive:
md5 = hashlib.md5()
for block in entry.get_blocks():
md5.update(block)
print(str(entry), md5.hexdigest())
原生 zipfile
:无依赖,仅 zip
import zipfile
archive = zipfile.ZipFile(fname)
blocksize = 1024**2 #1M chunks
for fname in archive.namelist():
entry = archive.open(fname)
md5 = hashlib.md5()
while True:
block = entry.read(blocksize)
if not block:
break
md5.update(block)
print(fname, md5.hexdigest())
关于python - 在Python中计算不解压的zip内容的CRC32、MD5和SHA1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44104426/