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.
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. So, you do not need to install them as a PIP module.
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 include 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. This way you can extract contents 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
# 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 sets the password 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 versions, compressed and uncompressed file sizes, and so on.