简体   繁体   中英

base64 encode a zip file in Python

Can someone give me some advice on how to encode a zip file into base64 in Python? There are examples on how to encode files in Python using the module base64, but I have not found any resources on zipfile encoding.

Thanks.

This is no different than encoding any other file...

import base64

with open('input.zip', 'rb') as fin, open('output.zip.b64', 'w') as fout:
    base64.encode(fin, fout)

NB: This avoids reading the file into memory to encode it, so should be more efficient.

import base64

with open("some_file.zip", "rb") as f:
    bytes = f.read()
    encoded = base64.b64encode(bytes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM