Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base64 encode a zip file in Python

Tags:

python

base64

zip

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.

like image 565
xyzims Avatar asked Jul 16 '12 20:07

xyzims


2 Answers

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.

like image 57
Jon Clements Avatar answered Nov 13 '22 05:11

Jon Clements


import base64

with open("some_file.zip", "rb") as f:
    bytes = f.read()
    encoded = base64.b64encode(bytes)
like image 45
steveha Avatar answered Nov 13 '22 03:11

steveha