Python has the ability to zip and unzip files and folders. It uses the ZipFile class from the zipfile (mind the class has Z and F in caps) module to accomplish this. In this session, I will show you how you can:
- Create a file using Python
- Create an empty zip folder using the ZipFile class
- Use a compression method for the zipped folder
- Add multiple files to the empty zipped folder
- Unzip/Extract the files in the zipped folder into another folder
As usual, we are going to use Visual Studio Code for this step by step tutorial. Please, leave your comments in the comment section if you have any questions.
CREATE A FILE IN PYTHON
1) Open Visual Studio Code and select a folder by clicking on the File Menu >> Open Folder >> Browse to your folder location, select it and click the Open button
Notice that the folder already contains several files (4 png images and a pdf file). We are going to create a text file (.txt) making it the sixth file. These files will be part of a compressed folder we will create soon in our program.

2) Create a new python file by hovering your mouse on the folder name (ZIPDEMO in Fig 1) and clicking the New File icon. Write any name of your choice making sure that it has the .py extension at the end.

3) Before writing the code to create the file, we first import the zipfile module and write the code as shown in fig 3 below:

In line 4, we have created a variable – myfile and used the open function to create a file with the name pythonfile.txt. The write( )method is used to write a content to the newly created file. The close( ) method, closes the file which frees the memory.
CREATE THE EMPTY ZIP FOLDER AND ADD FILES TO IT
4) To create an empty zip folder, we call the ZipFile class via the zipfile module

Notice that mycompressed_folder.zip is the name of the zip file. We have chosen the compressed type as ZIP_DEFLATED. Now, save and run the code to see the newly created file (pythonfile.txt), the newly created zipped folder (mycompressed_folder.zip) and the files inside the zipped folder. These files are a total of 5.34 MB but after the compression, the zipped folder is now 4.39 MB. Use any file of your choice on your hard drive and check the file size of the zipped folder.
Visual Studio Code may note open your zipped folder. You may get a warning similar to fig 5. In that case, you have to browse to your folder in windows explorer and open the zipped folder from there.

UNZIPPING THE FILES TO A NEW FOLDER
To unzip the file to a new folder, we use the read parameter of the ZipClass and call the extractall( ) method to extract all the files in the zipped folder to the new folder. If you want to extract (unzip) a single file, then you must call the extract( ) method and pass the name of the file as a string parameter.

Before running lines 22 and 23, make sure to comment on the previous codes. See the full folder structure in fig 7

import zipfile
myfile=open('pythonfile.txt', 'w')
myfile.write('Hello World')
myfile.close()
#Create an empty zip folder
myzipped_folder=zipfile.ZipFile('mycompressed_folder.zip', 'w')
#Add all the files in the same directory to mycompressed_folder
myzipped_folder.write('pythonfile.txt', compress_type=zipfile.ZIP_DEFLATED)
myzipped_folder.write('1.png', compress_type=zipfile.ZIP_DEFLATED)
myzipped_folder.write('1a.png', compress_type=zipfile.ZIP_DEFLATED)
myzipped_folder.write('2.png', compress_type=zipfile.ZIP_DEFLATED)
myzipped_folder.write('3.png', compress_type=zipfile.ZIP_DEFLATED)
myzipped_folder.write('Python Pocket Manual.pdf', compress_type=zipfile.ZIP_DEFLATED)
myzipped_folder.close()
#Extract all files from mycompressed_folder into a new folder
unzipped_obj=zipfile.ZipFile('mycompressed_folder.zip', 'r')
unzipped_obj.extractall('unzipped_content')



