Pythondex
Python Conversion Programs

Convert Zip File To Base64 In Python

Last updated January 31, 2024 by Jarvis Silva

In this article we will see how to convert zip file to base64 using python, To convert zip file to base64 we will use the python library Base64 it allows us to encode and decode base64 code, It comes preinstalled with python so you don’t have to install it.

Python Code To Convert Zip File To Base64


# Import base64 module
import base64

zip_file = open('sample.zip','rb')

# Enconding the zip file data
bs64 = base64.b64encode(zip_file.read())

print(bs64)

The above code converts zip file to base 64 string let’s see how this code works:

  • Import the base64 module to work with base64 encoding.
  • Open a zip file named ‘sample.zip’ in binary mode.
  • Read the content of the zip file.
  • Use the base64 module to encode the data read from the zip file.
  • Store the encoded data in the variable ‘bs64’.
  • Print the base64-encoded data.
  • End the program execution.

After running the program you will see that it printed the base64 code of the zip file. Want to know how to convert base64 to zip then read this article: Convert base64 to zip file in python.

Here are some more python programs you will find helpful:

I hope you found what you were looking for from this python tutorial, and if you want more python tutorials like this, do join our Telegram channel to get updated.

Thanks for reading, have a nice day 🙂