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.
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.
open('output.zip.b64', 'wb')
otherwise you'll get an error like "argument must be str, not bytes" –
Hubie import base64
with open("some_file.zip", "rb") as f:
bytes = f.read()
encoded = base64.b64encode(bytes)
encoded.decode('utf-8')
to get the base64 string instead of the base64 bytes. –
Consummation © 2022 - 2024 — McMap. All rights reserved.