How to Zip and Unzip Files in Python


Zip is an archive file format that supports lossless data compression. A zip file may contain one or more files or directories that may have been compressed. Do you know we can easily zip and unzip the files in Python using a built-in module called zipfile?

I hope that you have seen a zip file before and sent it to someone close friend by compressing some huge files in order to save the network bandwidth and increase transfer speed.


Some advantages of using zip files are: Improve memory utilization, reduce transport time, improve performance, etc.

In this article, to perform zip and unzip operations, we will be using zipfile module which is the Python standard library.


Zip and Unzip files in Python

zipfile module provides features like creating, reading, writing, appending, and listing a zip file. We will be using the class named ZipFile provided by this module to perform the following operations:

Create zip file

Let us create the zip file of the following files as shown in the figure.

Create zip file

import zipfile
from zipfile import ZipFile

with ZipFile(file="", mode="w", compression=zipfile.ZIP_DEFLATED) as f:

First, we import the zipfile module and then ZipFile class. After that create a zip file name as in writing mode. Here in that file, we can place our three files called apple.txt, ball.txt, and

files zip using python

Here ZIP_DEFLATED is the compression method that uses for writing the archive. Other compression methods the zipfile module provides are:

ZIP_DEFLATED, ZIP_BZIP2, ZIP_LZMA: These three methods are used for compressing the files.

ZIP_STORED: It is used for uncompressing the zip files.

Note: ZIP_STORED is the default value if we don’t specify any compression methods.

These compression methods are nothing but numeric constants.

# constants for Zip file compression methods
ZIP_BZIP2 = 12


Perform unzip operation

This operation extracts the zip folder that we created previously.

with ZipFile(file="", mode="r", compression=zipfile.ZIP_STORED) as f:
    f.extractall(path="C:\Workstation\Blogs") # extracting in the specified directory

Here we can specify the location to put the extracted contents. This is all possible with the extractall() method included by ZipFile class.

Extracted files python


List out files in a zip file

To get all the file names present in the “” file, we can use the namelist() method.

with ZipFile(file="", mode="r", compression=zipfile.ZIP_STORED) as f:
    file_names = f.namelist()
    for name in file_names:
        print("File Name: ", name)


File Name: apple.txt
File Name: ball.txt
File Name:


To read the zip file contents

In order to read out the actual contents on the zip file without extracting, we can twist the above namelist() method to get the file names. And with that file name read the contents using the Python open() method.

with ZipFile(file="", mode="r", compression=zipfile.ZIP_STORED) as f:
    file_names = f.namelist()
    for name in file_names:
        with open(name, 'r') as f2:
            print(f'{name} -- {}')


apple.txt -- This is apple text file.
ball.txt -- This is ball text file. -- # This is cat python file.


Using Command-line interface

Python zipfile module also provides a command-line version to interact with the zip archives. For that, we will be implementing the above functions like creating, extracting, and listing using the terminal.

Creating zip file

(venv) C:\Workstation\Blogs\zipfiles>python -m zipfile -c apple.txt ball.txt


Extracting zip file

(venv) C:\Workstation\Blogs\zipfiles>python -m zipfile -e C:\Workstation\Blogs


Listing out files in a zip file

(venv) C:\Workstation\Blogs\zipfiles>python -m zipfile -l




Here in this article, we discussed how we can zip the files using the zipfile module which is very efficient in compressing the range of files.

We extract the zip file using the extractall() method of ZipFile class and list out all the file names that were compressed using the namelist() method.

Also, we implemented all the above methods using a command-line interface which is a very easy way to create, extract, and list zip files.


Check out other articles:

Feature Selection Methods in Machine Learning

How to Implement Interface in Python

Duck Typing and Strong Typing in Python

Abstract Class in Python

Leave a Comment