逐块编码二进制文件失败

问题描述

我需要编码的Zip文件似乎太重了,下面的方法给我错误

with open("/tmp/pdf/pdffiles.zip","rb") as f:
    binary_file = f.read()
    encoded = base64.b64encode(binary_file)

self.download_zip = encoded

所以我尝试将其分块,但最终下载的文件已损坏, 任何人都可以看看下面的代码,并给我任何提示吗?

zipfile = open("/tmp/pdf/pdffiles.zip","rb")
encoded = False
while True:
    chunk = zipfile.read(8192) 
    if not chunk:
        break
    if encoded:
        encoded += base64.b64encode(chunk)
    else:   
        encoded = base64.b64encode(chunk)
zipfile.close()


self.download_zip = encoded

解决方法

对base64进行分块时,重要的是,分块大小必须是6的倍数,否则数据将无法正确连接。您可以尝试使用8208之类的数字,它应该可以使用。