Python-比ZipFile更快的压缩数据库



我正在Python 3.9中使用django 3.0.7作为框架开发一个web应用程序。在Python中,我正在创建一个函数,该函数可以使用ZipFile库将字典转换为json,然后再转换为zip文件。目前这是正在使用的代码:

def zip_dict(data: dict) -> bytes:
with io.BytesIO() as archive:
unzipped = bytes(json.dumps(data), "utf-8")
with zipfile.ZipFile(archive, mode="w", compression=zipfile.ZIP_DEFLATED) as zipFile:
zipFile.writestr(zinfo_or_arcname="data", data=unzipped)
return archive.getvalue()

然后我将zip保存在Azure Blob存储中。它可以工作,但问题是这个功能对我来说有点慢。我尝试使用zlib库,但性能没有改变,而且创建的zip文件似乎已损坏(我甚至无法使用WinRAR手动打开它(。是否有其他库可以提高压缩速度(不影响压缩比(?

尝试将compresslevel=3compresslevel=1参数添加到zipfile.ZipFile()对象创建中,看看这是否能提供更令人满意的速度和足够的压缩。

最新更新