Register Login

Python zip File and Extract zip File

Updated Feb 16, 2022

Python zip File and Extract zip File

We all are familiar with Zip files that helps in zipping or tying multiple files zipped or compressed under one file. Such files are easy to carry and a lot of applications allow zipping of files so that users can easily upload it. In this article, we will learn about creating zip using Python.

What is a Zip File?

ZIP format is a common file format which is used to compress a single or multiple files together present at a particular location.

Compression reduces the file size by encoding data into smaller bits by removing redundant data which helps to store and send files in a faster and easier way. This compression also ensures the original data is not modified; hence the recipient can unzip the file and use the file in the original format.  

What is a ZipFile?

ZipFile is the Python module that is used to perform operations on a ZIP file.
You can follow the below code snippet to compress a file:

ZipFile password

You can also protect your zip file by creating passwords. This can be achieved using the pyminizip module. Below code snippet compresses a given file and protects by providing a password.

# import pyminizip module
import pyminizip
# provide the input file path which is to be compressed
inp = "./Text.txt"
# provide prefix path if any
pre = None
# provide output file path for zip file
out = "./output.zip"
# set password to zip file
pwd = "compress"
# compress level
com_lvl = 5
# compressing file
pyminizip.compress(inp, None, out,
pwd, com_lvl)

Output:

You can also unlock the file using the following code snippet:

from zipfile import ZipFile
with ZipFile('output.zip') as myzip:
with myzip.open('Text.txt', pwd=bytes('compress', 'utf-8')) as myfile:
print(myfile.read())

List files

There can be multiple files or a single file in the archive. To get the details of each file you can use the infolist() method as shown in the following code snippet:

from zipfile import ZipFile
file_list = ZipFile('output.zip')
info = file_list.infolist()
print(info)

The above code snippet will return a list of all file details as shown below:

Following are some methods which gives more detail about each file in the archive:

file.is_dir(): Returns True if the component in the archive is a directory.

For example:

from zipfile import ZipFile
file_list = ZipFile('output.zip')
info = file_list.infolist()
for file in info:
print(file.is_dir())

Output:

file.printdir(): Returns the name, timestamp, and size of the component in the archive.

For example:

from zipfile import ZipFile
file_list = ZipFile('output.zip')
info = file_list.infolist()
for file in info:
print(file.printdir())

Output:

file.filename: Returns the name of each component in the archive.

For example:

from zipfile import ZipFile
file_list = ZipFile('output.zip')
info = file_list.infolist()
for file in info:
print(file.filename)

Output:

Alternatively, there is,

file.namelist: Returns the names of all components in the archive in list form:

For example:

from zipfile import ZipFile
file_list = ZipFile('output.zip')
info = file_list.namelist()
print(info)

Output:

file.date_time:  Returns the date and time when the component was last modified.

For example:

from zipfile import ZipFile
file_list = ZipFile('output.zip')
info = file_list.infolist()
for file in info:
print(file.date_time)

Output:

file.file_size: Returns the size of the component in the archive.

For example:

from zipfile import ZipFile
file_list = ZipFile('output.zip')
info = file_list.infolist()
for file in info:
print(file.file_size)

Output:

file.compress_size: Returns the size of the compressed component in the archive.

For example:

from zipfile import ZipFile
file_list = ZipFile('output.zip')
info = file_list.infolist()
for file in info:
print(file.compress_size)

Output:

file.compress_type: Returns the type of the compressed component in the archive.

For example:

from zipfile import ZipFile
file_list = ZipFile('output.zip')
info = file_list.infolist()
for file in info:
print(file.compress_type)

Output:

file.create_system: Returns the system which created the archive.

For example:

from zipfile import ZipFile
file_list = ZipFile('output.zip')
info = file_list.infolist()
for file in info:
print(file.create_system)

Output:

File.volume: Returns the volume number of the component file header in the archive.

For example:

from zipfile import ZipFile
file_list = ZipFile('output.zip')
info = file_list.infolist()
for file in info:
print(file.volume)

Output:

ZIP Archives

You can also create an archive containing multiple archives as shown below:

from zipfile import ZipFile
with ZipFile('output.zip', 'w') as zip:
zip.write('pytest.zip')
zip.write('Text.zip')

Output:

Appending to Archives

After creating an archive, it is still possible to add files to the same archive using append mode:

#import ZipFile module
from zipfile import ZipFile
def append_archive():
#assign the archive to file variable
file = 'output.zip'
#open the file in append mode
with ZipFile(file, 'a') as file:
#add files to archive
file.write('Text.txt')
#open the file in read mode
with ZipFile(file, 'r') as file:
#check if appended file is added to archive
print(file.namelist())
append_archive()

Output:

ZipFile extractall to directory

After Archiving the files, if you want to access them you need to extract the files. Following is the code snippet to extract all the files from the zip file:

#import ZipFile module
from zipfile import ZipFile
def extract():
# assigning filename to a variable
file = 'output.zip'
# opening the archive file in read mode
with ZipFile(file, 'r') as file:
# extracting the files using extractall() method
file.extractall()
#if the file is encrypted then you can use pwd argument
file.extractall(pwd=<provide password>)
extract()

Following is the code snippet to extract a specific file from the zip file:

from zipfile import ZipFile
def extract():
# assigning filename to a variable
file = 'output.zip'
# opening the archive file in read mode
with ZipFile(file, 'r') as file:
# extracting a file using extract() method
file.extract('Text.txt')
#if the file is encrypted then you can use pwd argument
file.extract('Text.txt', pwd=<provide password>
extract()

Conclusion:

In this article, we learned about compressing a file using Python scripts and how to implement the compression through ZipFile module. This module is a standard and effective zipping or compressing module and has high efficiency compared to other modules.

This module is useful if you design an application where you want to share a large file; it will not only take longer time but also it is a easy to implement developer’s process. In such case compression technique becomes a boon as app users can quickly, easily, and safely compress it leveraging password protecting methods.


×