Python ZIP file with Example

By | November 24, 2021

Python comes with many standard libraries, and here, in this Python tutorial, we will learn about a Python zip file in detail. This article explains how to compress (zip) and extract (or un-zip) files.

What is a Zip File?

The zip file is an archive file format that uses algorithms to compress files without losing the data. When files are compressed to a zip archive, the size of the file reduces, but the data remain the same. Zip provides an ideal way to compress or reduce the size of files and collect them in a single folder or directory.

Why Use a Zip File?

  • Zip files reduce the size of files. It reduces the storage space occupied by one or many files without losing data.
  • You can bind together many files in a single zip file.
  • With reduced size, it is very easy to send zip files over networks.

Creating a Zip File in Python Using the zipfile Module

Python comes with a standard zipfile module, which can compress files into a zip file.

Example

Let’s write Python code that will create a zip file.

Vamware

Here we are in a folder named dev. Now, you can see that we have three files, namely demo.txt, sublime.png and zip-tutorial.py. The zip-tutorial.py file contains the Python script that will create a zip file by compressing demo.txt and sublime.png files.

Vamware

Python Program to Create a Zip File

#zip-tutorial.py

import zipfile

print("Creating Zip File.../")

with zipfile.ZipFile("file.zip", "w", compression=zipfile.ZIP_DEFLATED) as create_zip:

    create_zip.write("demo.txt")   #compress the demo.txt file into file.zip
    create_zip.write("sublime.png")  #compress the sublime.png file into file.zip

print("Zip file by name file.zip has been created")

Execute the Python Program

The above code will create a zip file with the name file.zip in the same folder or directory where the python script, i.e., zip-tutorial.py, is located. Now when you open the same directory, you will see the zip file created there.

Behind the Code

Similar to the Python file handling method, to create a zip file, the zipfile module provides the ZipFile method. In the ZipFile(“file.zip”, “w”, compression=zipfile.ZIP_DEFLATED) method, we specified three parameters file.zip, “w”, andcompression=zipfile.ZIP_DEFLATED.

This means we want to write compressed files into the file.zip file. If this file is not available, this statement will create a file.zip file. Else, it will write in the existing file.zip file. The create_zip.write(“demo.txt”) and create_zip.write(“sublime.png”) statements copy and compress the files into a file.zip file.

Extracting a Zip File in Python Using the zipfile Module

Now you know how to create a zip file. Next, let’s discuss how you can extract a zip file using the Python zipfile module. As we have used the write mode, “w”, to create a zip file, similarly, we can use the read mode, “r”, to extract or read files from a zip file.

Example

In the dev folder, we have a file by name file.zip. Let’s write a Python program to extract the file.zip file.

Python Program to Unzip (Extract) a Zip File

#zip-tutorial.py

import zipfile
print("Extracting file.zip File.../")

with zipfile.ZipFile("file.zip", "r") as extract_zip:

    #extract all files and save them in folder Extracted files
    extract_zip.extractall("Extracted Files")

print("Zip file by name file.zip has been created")

Execute the Program

After executing the file, you will find the Extracted File folder in the development folder where your file.zip is located.

Behind the Code

Similar to the zip file creating technique, we used the zip file handling technique to extract the zip files. Using the statement zipfile.ZipFile("file.zip","r")  we specified that we want to read the file.zip file, and with the extractall("ExtractedFiles") statement, we extracted the file.zip files into Extracted Files folder.

The extractall() method extracts all the files present in the zip file. To extract specific files from the zip file, we can use the extract method. For instance, if we only want to extract the demo.txt file from the file.zip, we could have used the following code:

extract_zip.extracta("demo.txt")

Conclusion

In this Python tutorial, we learned how to create and extract zip files using Python. Python provides a standard library, zipfile, to create and extract zip files. The zipfile module is very useful when you want to create an automated script.

In the zipfile module, the ZipFile() method is used to open the zip file for reading and writing. While creating a zip file, we need to specify the compression=zipfile.ZIP_DEFLATED parameter to compress the files.

Python Tutorials

Introduction

Flow Control

Functions

Datatypes

File Handling

Object & Class

Additional Tutorials

Author: Vinay

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published.