base64 encode a zip file in Python

18,334

Solution 1

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.

Solution 2

import base64
with open("some_file.zip", "rb") as f:
    bytes = f.read()
    encoded = base64.b64encode(bytes)
Share:
18,334
xyzims
Author by

xyzims

Updated on July 15, 2022

Comments

  • xyzims
    xyzims 6 months

    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.