Python

Python Zip File

Python is a general-purpose programming language. It is widely used in machine learning, deep learning, artificial intelligence, and data sciences projects. Python is loaded with handy built-in modules, functions, and statements. Therefore, it helps the programmers a lot to perform many types of tasks. Performing the file related task in Python is super easy due to the availability of related modules. We can perform any type of file-related tasks, i.e., reading, writing, searching, and deleting a file.

ZIP is a popular format of files that offers lossless compression. A ZIP file contains one or many compressed files and is a single file. The compression algorithms ensure that we can recreate the actual data from the compressed data without any loss. There are several benefits of using the zip file. By using the zip files, we can put all the related data in one single file with reduced file size. Encryption can also be applied while creating zip files. ZIP files are mostly created and used when we need to transfer data through online sources like social media applications and email. It ensures the fastest delivery of data. Python provides a built-in zipfile module to work on the ZIP files. In this guide, we will learn to perform various zip file-related tasks with examples.

Create a zipfile

Let’s create a zipfile for multiple related files.

#importing the zipfile module

from zipfile import ZipFile

#specifying the path of files

myfiles = ['/home/linuxhint/Documents/myfile.txt', '/home/linuxhint/Documents/myfile1.txt']

#specifying the name of the zip and path of the zip file

with ZipFile('myzipfile.zip', 'w') as zip:

   for i in myfiles:

       #wiring zip files

       zip.write(i)

print("The zip file is created successfully")

Output
A ZIP file is created successfully.

Alright! Now let’s understand the above-given code for creating a ZIP file.

#importing the zipfile module

from zipfile import ZipFile

In this line of code, we have imported the ZipFile class from the zipfile module. The ZipFile class is used to write the ZIP file. We do not need to use the other classes of zipfile for creating a ZIP file.

#specifying the path of files

myfiles = ['/home/linuxhint/Documents/myfile.txt', '/home/linuxhint/Documents/myfile1.txt']

Here, we have created a list of files that contains the path of files to be compressed.

#specifying the name of the zip and path of the zip file

with ZipFile('myzipfile.zip', 'w') as zip:

   for i in myfiles:

       #wiring zip files

       zip.write(i)

In this code block, we have created and opened a ZIP file in writing mode. The name of the newly created ZIP file is ‘myzipfile.zip’, and it is created in the current working directory. If you wish to create the ZIP file in another directory, then specify the path of that directory with the ZIP file name. The write() is a built-in function that writes the file in a ZIP file. To create a zip file for all files of a specific folder or directory, we need access to directories and subdirectories. Therefore, we need to import the os module and as well as the zipfile module in our Python script. Let’s create a ZIP file of all the files placed in the specified directory.

# importing the ZipFile class from the zipfile module

from zipfile import ZipFile

#importing the os module

import os

# a list o store the files name to be compressed

myfiles = []

for root, directories, files in os.walk("/home/linuxhint/Documents/myfolder"):

   for filename in files:

       # joining the strings to make the filepath

       filepath = os.path.join(root, filename)

       myfiles.append(filepath)

print("The files to be compressed are: ")

print(myfiles)

with ZipFile("/home/linuxhint/Downloads/myzipfile.zip", 'w') as zipObj:

   for i in myfiles:

       #writing the files

       zipObj.write(i)

print("The ZIP file is created successfully")

Output

Let’s divide the above-given code into chunks and try to understand it.

myfiles = []

for root, directories, files in os.walk("/home/linuxhint/Documents/myfolder"):

   for filename in files:

       # joining the strings to make the filepath

       filepath = os.path.join(root, filename)

       myfiles.append(filepath)

print("The files to be compressed are: ")

print(myfiles)

In the code block, first of all, we have created an empty list to store the path of all the files to be compressed.  We have used os. walk() function to get the path of all the files. The for loop is implemented to get the file path and store it in our list.

with ZipFile("\home\linuxhint\Downloads\myzipfile.zip", 'w') as zipObj:

In this code block,  we have created a file object and added the path of the ZIP file to be created. The file is opened in the writing mood.

for i in myfiles:

       #writing the files

       zipObj.write(i)

print("The ZIP file is created successfully")

In the above-given code block, we are iterating our list of files and writing all the files to the zip file using the write() function.

Alright! That was all about creating the ZIP files in Python.

See ZIP file contents

Now let’s discuss how to see the ZIP file contents. In the reading mood, we will open the ZIP file object to viewing the contents of the ZIP file.

# importing the ZipFile class from the zipfile module

from zipfile import ZipFile

with ZipFile("/home/linuxhint/Downloads/myzipfile.zip", 'r') as zipObj:

   zipObj.printdir()

The printdir() function prints the content of the ZIP file in table form.

Output
The output shows the content of the ZIP file.

Extract ZIP file content

Alright! Now we are familiar with creating the zip files and viewing the content of ZIP files. The next point is to extract the ZIP file content. We can extract the ZIP file content by using the extractall() built-in function. Let’s implement it in our Python script.

#importing the ZipFile class from the zipfile module

from zipfile import ZipFile

#storing the path of the zip file in a path variable

path="/home/linuxhint/Downloads/myzipfile.zip"

with ZipFile(path, 'r') as zipObj:

   # Extracting the zip file content

   zipObj.extractall()

print("The files are extracted successfully")

Output

Conclusion

ZIP is a popular file format that provides lossless compression. In Python, we can create and extract the zip files using the built-in zipfile module. This article explains the zip file related task with examples.

About the author

Kamran Sattar Awaisi

Kamran Sattar Awaisi

I am a software engineer and a research scholar. I like to write article and make tutorial on various IT topics including Python, Cloud Computing, Fog Computing and Deep Learning. I love to use Linux based operating systems.