Table of Contents
What is ZIP format?
Zip files in python are simple to manipulate. A ZIP file is a data-compression archive that uses lossless compression. It can hold several files and folders, as well as additional compressed ZIP files.
This format supports a variety of compression methods, the most prevalent of which is DEFLATE. Essentially, a ZIP file is used to compress multiple huge files and directories into a single smaller archive file.
As a result, there are a variety of software options for creating ZIP files and extracting content from ZIP files.
Similarly, Python has various modules for working with, creating, and extracting data from ZIP files, such as zipfile, zipimport, shutil, and so on.
we will discuss how to zip folder using python with zipfile python examples, how python extracts the zip file, how python zip multiple files, and how python read zip file without extracting.
In this tutorial, we’ll use the zipfile and shutil modules. Shutil, like zipfile, is a Python module. As a result, there will be no need to install them externally.
Creating a zip file using
# Zip files in python example # importing ZipFile from zipfile import ZipFile # create a ZipFile object zip_obj = ZipFile('zip_file_name.zip', 'w') # Add files zip_obj.write('sample_image.png') zip_obj.write('text_file.txt') zip_obj.write('text_file_2.txt') # close the Zip File zip_obj.close()
It will create a zip file named ‘zip_file_name.zip‘ that contains the specified files.
The same can be done with “with open“. Here the zip file object will automatically close.
## Zip files in python example # importing ZipFile from zipfile import ZipFile # add files to zip and create it with ZipFile("file_name_to.zip", "w") as f: f.write("an_image.png") f.write("sample_file_name.pdf")
write() requires the file name:
filename – enter the name of the file to be included in the ZIP archive.
Creating a zip file using
# importing shutil module import shutil #zip a directory shutil.make_archive(base_name, 'format', root_dir)
make_archive() requires the following arguments.
base_name : the name of the output file you’d like to make.
format: specifies the archive file’s format. Zip, tar, gztar, bztar, xztar, and other compression formats are examples.
root_dir or base_dir: root_dir is the directory that will be the archive’s root directory, and base dir is the directory from which we will begin archiving.
Extract files from the zip file
We’ll use the
extractall() method to extract files. The contents of the zip file will be extracted from the current working directory.
# Zip files in python example # importing ZipFile from zipfile import ZipFile # specifying the zip file_name file_name = "zip_file_name.zip" # opening the zip file in READ mode with ZipFile(file_name, 'r') as zip: # extracting all contents zip.extractall()
Extract only the specified file
extract() method to extract a single file.
# Zip files in python example zip.extract('path/to/file.extension') # for example zip.extract('images/ocean.png')
This way, only the specified file will be extracted.
Read the specified file
If you want to read a specific file, then use the read() method:
# Zip files in python example text = zip.read('file_name.txt') print(text)
Prints a table of contents for the archive.
printdir() prints an archive’s table of contents.
# Zip files in python example # importing ZipFile from zipfile import ZipFile # specifying the zip file_name file_name = "file_name.zip" # opening the zip file in READ mode with ZipFile(file_name, 'r') as zip: # printing all the contents of a zip file zip.printdir()
There are many methods the
zipfile module offers. we will discuss some of them here.
is_zipfile(): The is
zipfile() checks whether a file is a legitimate ZIP file. It returns True for zip files and false if it is not a zip file.
ZipFile.namelist() returns a list of archive members arranged by name.
ZipFile. setpassword(): This method is used to set the password that is used to encrypt and extract the zip file.
infolist() creates a ZipInfo instance that contains all of the zip file’s information. We have access to all information, including file last modification dates, file names, the system on which the files were created, Zip version, compressed and uncompressed file sizes, and so on.
So, I hope you understand how to use Python to extract zip files. If you have any questions, feel free to post them in the comments section below.
This article “Zip files in python” is created with the help of official documentation.