从字节字符串压缩内存中的文件



目前我正在编写这样的文件:

with open('derp.bin', 'wb') as f:
f.write(file_data)

但是有时这个文件非常大,我需要压缩它,我想尽量减少写入磁盘的次数并在内存中执行此操作。我知道我可以使用BytesIOZipFile从数据创建zip文件。这是我到目前为止所拥有的:

zipbuf = io.BytesIO(file_data)
z = zipfile.ZipFile(zipbuf, 'w')
z.close()
with open('derp.zip', 'wb') as f:
shutil.copyfileobj(zipbuf, f)

我怎样才能做到当你提取zip文件时,里面有原始derp.bin

z = zipfile.ZipFile('derp.bin','w')
z.writestr('derp.zip',file_data,8)
z.close()

最新更新