Introduction
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.
import zipfile from zipfile import ZipFile with ZipFile(file="files.zip", mode="w", compression=zipfile.ZIP_DEFLATED) as f: f.write('apple.txt') f.write('ball.txt') f.write('cat.py')
First, we import the zipfile module and then ZipFile class. After that create a zip file name as files.zip in writing mode. Here in that file, we can place our three files called apple.txt, ball.txt, and cat.py.
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_STORED = 0 ZIP_DEFLATED = 8 ZIP_BZIP2 = 12 ZIP_LZMA = 14
Perform unzip operation
This operation extracts the zip folder that we created previously.
with ZipFile(file="files.zip", 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.
List out files in a zip file
To get all the file names present in the “files.zip” file, we can use the namelist() method.
with ZipFile(file="files.zip", mode="r", compression=zipfile.ZIP_STORED) as f: file_names = f.namelist() for name in file_names: print("File Name: ", name)
Output:
File Name: apple.txt File Name: ball.txt File Name: cat.py
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="files.zip", 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} -- {f2.read()}')
Output:
apple.txt -- This is apple text file. ball.txt -- This is ball text file. cat.py -- # 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 files.zip apple.txt ball.txt cat.py
Extracting zip file
(venv) C:\Workstation\Blogs\zipfiles>python -m zipfile -e files.zip C:\Workstation\Blogs
Listing out files in a zip file
(venv) C:\Workstation\Blogs\zipfiles>python -m zipfile -l files.zip
Output:
Conclusion
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