Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

zipfile Module – Working with ZIP files in Python.

Python Zipfile Module

In this tutorial, we will see what zip files are and we will implement  code in python to automate working with zip files with the zipfile module. We will see how to create compressed and uncompressed  zip files and extract files from zip files. We will also see how to append new files to already created zip file.

What is a zip file?

ZIP is an archive file format that supports lossless data compression. It may contain one or more files or folders that may or may not be compressed. ZIP files are used as a base file format by many programs and use .zip file extension.

How to create a zip file with the zipfile module without compression?

In Python, we can create zip files using the ZipFile() method of the zipfile module. We can then add other files to the zip file. The following screenshot shows the files in the folder before creating a zipped file.

Folder Before Zipping
Folder Before Zipping

Next we’ll look at the code. Here the ZipFile() method takes filename of the zip file to be created as first argument and “w” for opening file in write mode.

Don’t forget to close the file in the end of the program.

#import module
import zipfile

#declare filename
filename="zip_tutorial/tutorial.zip"

# create a ZipFile object by opening file
zip_obj= zipfile.ZipFile(filename,"w")

#add files to zip file
zip_obj.write("zip_tutorial/plane.xml")
zip_obj.write("zip_tutorial/sample.txt")
zip_obj.write("zip_tutorial/person.ini")

#close the file object
zip_obj.close()

The following figure shows the Content of the folder after creation of the ZIP file.

Folder After Zipping
Folder After Zipping

How to create a compressed zip file with the zipfile module?

To create a compressed zip file, we just need to provide the algorithm with which we want to compress the files as an argument to the ZipFile() method.

Generally, we use DEFLATED algorithm to compress the files while creating zip files. The following figure shows the content of the folder before the creation of a compressed zip file.

Folder Before Zipping 1
Folder Before compressed Zipping

To create a compressed zip file we just need to add a compression argument specifying the algorithm for compression. Here we have used the zip_deflated algorithm hence compression=zipfile.ZIP_DEFLATED has been used while creating the ZipFile object.

#import module
import zipfile

#declare filename
filename="zip_tutorial/tutorial.zip"

# create a ZipFile object by opening file
zip_obj= zipfile.ZipFile(filename,"w",compression=zipfile.ZIP_DEFLATED)

#add files to zip file
zip_obj.write("zip_tutorial/plane.xml")
zip_obj.write("zip_tutorial/sample.txt")
zip_obj.write("zip_tutorial/person.ini")

#close the file object
zip_obj.close()

Following figure shows the content of the folder after the compressed zip file has been created.

Folder After Zipping 1
Folder After Zipping 1

How to check contents of a zipped folder without extracting the files in it?

To check the contents of a zip file, we can use the namelist() method of the zipfile module.

Here namelist() method returns a list of names of files in the zip file when invoked on the ZipFile object.

Here we have opened the file in “read” mode hence “r” is given as a second argument to ZipFile(). We’ll use the for loop here to loop over the contents of the zip file.

#import module
import zipfile

#declare filename
filename="zip_tutorial/tutorial.zip"

# create a ZipFile object by opening file
zip_obj= zipfile.ZipFile(filename,"r")

#print the content of zip file
print("Content of the ZIP file are: ")
content_list=zip_obj.namelist()
for fname in content_list:
    print(fname)

#close the file object
zip_obj.close()

Output of above code snippet is:

Content of the ZIP file are: 
zip_tutorial/plane.xml
zip_tutorial/sample.txt
zip_tutorial/person.ini

How to check metadata of a zipped file?

Metadata of a file is the data that contains the description of any file. It contains the information about date created, date modified , file size and other  info.

To get the metadata of a zip file, we can use the infolist() method of the zipfile module.

#import module
import zipfile

#declare filename
filename="zip_tutorial/tutorial.zip"

# create a ZipFile object by opening file
zip_obj= zipfile.ZipFile(filename,"r")

#print the metadata of zip file
print("Metadata of the ZIP file are: ")
content_list=zip_obj.infolist()
for info in content_list:
    print(info)

#close the file object
zip_obj.close()

Output of above code is:

Metadata of the ZIP file are: 
<ZipInfo filename='zip_tutorial/plane.xml' compress_type=deflate filemode='-rw-rw-r--' file_size=264 compress_size=174>
<ZipInfo filename='zip_tutorial/sample.txt' compress_type=deflate filemode='-rw-rw-r--' file_size=409 compress_size=215>
<ZipInfo filename='zip_tutorial/person.ini' compress_type=deflate filemode='-rw-rw-r--' file_size=183 compress_size=141>

We can see that infolist() has returned info about filename, actual size, compressed size, compression algorithm, and file access modes of each file which are inside the zip file.

How to append files directly into a zip file?

We can add extra files into a zip file directly using the write() method from the zipfile module as we have done while creating zip file.

The only difference is that we have to open the file in append mode hence “a” is passed as the second argument to ZipFile() method.

#import module
import zipfile

#declare filename
filename="zip_tutorial/tutorial.zip"

# create a ZipFile object by opening file
zip_obj= zipfile.ZipFile(filename,"a")

#print the initial content of zip file
print("Initial Content of the ZIP file are: ")
content_list=zip_obj.namelist()
for fname in content_list:
    print(fname)

#Append a file to zip file
zip_obj.write("zip_tutorial/sampleoutput.txt")

#close the file objecz
zip_obj.close()

#read final content of the file after appending
nzip_obj= zipfile.ZipFile(filename,"a")

#print the initial content of zip file
print("Final Content of the ZIP file are: ")
ncontent_list=nzip_obj.namelist()
for fname in ncontent_list:
    print(fname)

#close the file
nzip_obj.close()

Output:

Initial Content of the ZIP file are: 
zip_tutorial/plane.xml
zip_tutorial/sample.txt
zip_tutorial/person.ini
Final Content of the ZIP file are: 
zip_tutorial/plane.xml
zip_tutorial/sample.txt
zip_tutorial/person.ini
zip_tutorial/sampleoutput.txt

How to extract a single file from a zipped folder?

To extract only a single file from a zipped folder, we can use the extract() method of the zipfile module. Here is the snap of the folder before extracting any file.

Folder Before Unzipping
Folder Before Unzipping

The extract() method takes a filename as an argument and extracts the file in our working directory.

#import module
import zipfile

#declare filename
filename="zip_tutorial/tutorial.zip"

# create a ZipFile object by opening file
zip_obj= zipfile.ZipFile(filename,"r")

#extract file
zip_obj.extract("zip_tutorial/sampleoutput.txt")

#close the zip file
zip_obj.close()

Image given below shows a snap of the folder after the file has been extracted from zip file.

Extracted File From ZIP
Extracted File From ZIP

How to extract all files from a zipped folder with the zipfile module?

To extract the whole zip folder instead of a single file, we can use the extractall() method of the zipfile module. The image given below shows the snap of the folder before extracting the contents of the zip file.

Folder Before Unzipping 1
Folder Before Unzipping whole file

The extractall() method takes the name of the output file as its argument and extracts the entire contents of the zip file into the folder in our working directory.

#import module
import zipfile

#declare filename
filename="zip_tutorial/tutorial.zip"

# create a ZipFile object by opening file
zip_obj= zipfile.ZipFile(filename,"r")

#extract all files 
zip_obj.extractall("zip_tutorial/tutorial_folder_after_extraction")

#close the zip file
zip_obj.close()

Below given image shows a snap of folder after whole content of the zip file has been extracted in an another folder.

Extracted Folder From ZIP
Extracted Folder From ZIP

Conclusion

In this tutorial, we have seen what zip files are and how to create and manipulate zip files with the Python zipfile module. We also saw how to extract a single file as well as the whole content of the zip files. Happy Learning!