zipfile module in Python


The ZIP file format is the most famous and commonly used file archive format and compression standard in use thus python provides us with the zipfile module to deal with the zip file format.Using this module we can perform tasks like reading,writing,listing and extracting a zip file.

Some important point regarding zipfile module:

  • It does not handle multi-disk zip files.
  • It supports the ZIP64 extensions.
  • It supports decryption of encrypted zip files.
  • Encryption of zip files in not supported.

ZipFile Objects

In order to work with a zip file we first need to open a existing zip file or create a new one.The ZipFile() function allows us to open a zip file in different modes and returns a ZipFile object which we can use to carry out various task on the zip file.

ZipFile()

This function is used to open zip file in read,write or append mode or to create a new zip file.It provides us with a way to read ,write and append to a zip file.It creates a zipfile object of the specified file.

Syntax

ZipFile(file,,mode="read/write/append/create",
        compression="compression method",allowZip64=yes)

Parameters

  • file: This argument should be a filename or the path to a zip file or a file object or a path object.

  • mode:This argument defines the mode in which the zip file needs to be accessed.
    There are four modes:

  1. 'r':This mode is used to read an existing file.
  2. 'w':This mode is used to write to a file.
  3. 'a':This mode is used to append to a file.
  4. 'x':This mode is used to exclusively create a new file and write to it.
  • compression:This argument stores the compression method for the zip file.it can have the following values:
  1. ZIP_STORED
  2. ZIP_DEFLATED
  3. ZIP_BZIP2
  4. ZIP_LZMA
  • allowZip64:This argument stores a boolean value that is either true or false.If the zipfile is larger than 4 GiB then it requires ZIP64 extension an in this case allowZip64 is set to true otherwise it is d=set to false.

Return Type:

This function return a zipfile object with the details of the zip file i.e.,it's name and thew mode in which it is opened/created.

Example:

import zipfile as zf
zd=zf.ZipFile("Opengenus.zip",'r')
print("Return type:",zd)
print("List of contents in zipfile:",zd.namelist())
zd.close();

Output:

Return type: <zipfile.ZipFile filename='Opengenus.zip' mode='r'>
List of contents in zipfile: ['OpenGenus/Cosmos/', 'OpenGenus/Discuss/', 'OpenGenus/IQ/', 'OpenGenus/Quark/', 'OpenGenus/Search Engine/']

Example using 'with' statement:

import zipfile as zf
with zf.ZipFile("Opengenus.zip",'r') as zd:
    print("Return type:",zd)
    print("List of contents in zipfile:",zd.namelist())

Output:

Return type: <zipfile.ZipFile filename='Opengenus.zip' mode='r'>
List of contents in zipfile: ['OpenGenus/Cosmos/', 'OpenGenus/Discuss/', 'OpenGenus/IQ/', 'OpenGenus/Quark/', 'OpenGenus/Search Engine/']

ZipFile.close():

This function is used to close a zipfile object after all the required task have been completed.It is necessary to close in case we write something to the zip file so that the zip file can be updated.

Syntax:

ZipFile.close();

Parameters:None

Return Type:None

Example:

import zipfile as zf
zd=zf.ZipFile("Opengenus.zip",'r')
print("List of contents in zipfile:",zd.namelist())
zd.close();

Output:

List of contents in zipfile: ['OpenGenus/Cosmos/', 'OpenGenus/Discuss/', 'OpenGenus/IQ/', 'OpenGenus/Quark/', 'OpenGenus/Search Engine/']

ZipFile.getinfo():

This function is used to get the information of any member of the zip file by providing it's name as a parameter.

Syntax:

ZipFile.getinfo(*name*)

Parameters:

  • name:This parameter should contains the name of the member or the path to the member inside the zip file.

Return type:Returns a ZipInfo object with the information of the members name given by the user.

Example:

import zipfile as zf
zd=zf.ZipFile("Opengenus.zip",'r')
print("Information of Cosmos:",zd.getinfo('OpenGenus/Cosmos/IntroCosmos.txt'))
zd.close();

Output:

Information of Cosmos: <ZipInfo filename='OpenGenus/Cosmos/IntroCosmos.txt' external_attr=0x20 file_size=19>

ZipFile.infolist():

This function is used to get a list of the information of all the present members of the zip file.

Syntax:

ZipFile.infolist()

Parameters:None

Return type:Returns a list of ZipInfo object of each member of the zip file and the information of all the members.

Example:

import zipfile as zf
zd=zf.ZipFile("Opengenus.zip",'r')
print("List of information of all members:")
for item in zd.infolist():
    print(item)
zd.close()

Output:

List of information of all members:
<ZipInfo filename='OpenGenus/Cosmos/' external_attr=0x10>
<ZipInfo filename='OpenGenus/Discuss/' external_attr=0x10>
<ZipInfo filename='OpenGenus/IQ/' external_attr=0x10>
<ZipInfo filename='OpenGenus/Quark/' external_attr=0x10>
<ZipInfo filename='OpenGenus/Search Engine/' external_attr=0x10>
<ZipInfo filename='OpenGenus/Cosmos/IntroCosmos.txt' external_attr=0x20 file_size=19>

ZipFile.namelist():

This function is used to get the list of names of all the members of the zip file.

Syntax:

ZipFile.namelist()

Parameters:None

Return type:Returns a list of names of each member of the zip file.

Example:

import zipfile as zf
zd=zf.ZipFile("Opengenus.zip",'r')
print("List of names of all members:")
for name in zd.namelist():
    print(name)
zd.close()

Output:

List of names of all members:
OpenGenus/Cosmos/
OpenGenus/Discuss/
OpenGenus/IQ/
OpenGenus/Quark/
OpenGenus/Search Engine/
OpenGenus/Cosmos/IntroCosmos.txt

ZipFile.printdir()

This function is used to directly print the details of the members of the zip file in the same order as they would appear in the file system.

Syntax:

ZipFile.namelist()

Parameters:None

Return type:Returns a list of names of each member of the zip file.

Example:

import zipfile as zf
zd=zf.ZipFile("Opengenus.zip",'r')
zd.printdir()
zd.close()

Output:

File Name                                             Modified             Size
OpenGenus/Cosmos/                              2020-03-18 11:25:28            0
OpenGenus/Discuss/                             2020-03-18 11:26:20            0
OpenGenus/IQ/                                  2020-03-18 11:25:58            0
OpenGenus/Quark/                               2020-03-18 11:25:36            0
OpenGenus/Search Engine/                       2020-03-18 11:26:42            0
OpenGenus/Cosmos/IntroCosmos.txt               2020-03-18 13:31:08           19

ZipFile.extract():

This function is used to extract a member from the zipfile.This function is used when we need to extract only a single member at a time.The extracted member is not removed from the zip file.

Syntax:

ZipFile.extract(*member, path=None, pwd=None*)

Parameters:

  • member:This parameter should be the name or the path to the member that will be extracted.
  • path:This parameter contains the path to the directory where the member will be extracted.If it is not specified the member is extracted in the present working directory.
  • pwd:This parameter contains the password to decrypt the zip file in case it is encrypted.

Return type:It returns a path to the extracted file.

Example:

import zipfile as zf
zd=zf.ZipFile("Opengenus.zip",'r')
print("List of members in the zip file.")
zd.printdir()
print("\nThe extracted file is at:",zd.extract('OpenGenus/Cosmos/'))
zd.close()

Output:

List of members in the zip file.
File Name                                             Modified             Size
OpenGenus/Cosmos/                              2020-03-18 11:25:28            0
OpenGenus/Discuss/                             2020-03-18 11:26:20            0
OpenGenus/IQ/                                  2020-03-18 11:25:58            0
OpenGenus/Quark/                               2020-03-18 11:25:36            0
OpenGenus/Search Engine/                       2020-03-18 11:26:42            0
OpenGenus/Cosmos/IntroCosmos.txt               2020-03-18 13:31:08           19

The extracted file is at: H:\OpenGenus\Cosmos

ZipFile.extractall():

This function is used to extract all the members from the zipfile.This function is used when we need to extract all the member at once.The extracted members are not removed from the zip file.

Syntax:

ZipFile.extractall(*path=None,members,pwd=None*)

Parameters:

  • path:This parameter contains the path to the directory where the member will be extracted.If it is not specified the member is extracted in the present working directory.
  • members:This parameter should be a list of names or the paths to the members that will be extracted.
  • pwd:This parameter contains the password to decrypt the zip file in case it is encrypted.

Return type:None

Example:

import zipfile as zf
import os
zd=zf.ZipFile("Opengenus.zip",'r')
print("List of items in the directory before extraction")    
for item in os.listdir(path='.'):
    print(item)
print("\n\n\n\n")    
zd.extractall()
print("List of items in the directory after extraction")    
for item in os.listdir(path='.'):
    print(item)
zd.close()

Output:

List of items in the directory before extraction
Competitive Coding
Drivers
Internship
OpenGenus.zip
Projects


List of items in the directory after extraction
Competitive Coding
Drivers
Internship
OpenGenus
OpenGenus.zip
Projects

ZipInfo Objects

There might be cases while working with zip files when we need to access the details of a member of the zip file.zipfile module provides us with functionalities like ZipInfo objects where each object can store the information of a single member of the zip file.we can create ZipInfo object by using the functions getinfo() and infolist().We will learn about the various attributes of Zipinfo object in this section.

ZipInfo.filename

this attributes is used to get the name of the member of the zip file through the ZipInfo object.

ZipInfo.is_dir():

This function is used to check if the member is a directory or not.If the member is a directory then it returns true otherwise false.

Syntax:

ZipInfo.is_dir()

Parameters:None

Return type:Boolean(True or False)

Example:

import zipfile as zf
zd=zf.ZipFile("Opengenus.zip",'r')
print("List of information of all members:")
for item in zd.infolist():
    print(item.filename," is directory? ",item.is_dir())
zd.close()

Output

List of information of all members:
OpenGenus/Cosmos/  is directory?  True
OpenGenus/Discuss/  is directory?  True
OpenGenus/IQ/  is directory?  True
OpenGenus/Quark/  is directory?  True
OpenGenus/Search Engine/  is directory?  True
OpenGenus/Cosmos/IntroCosmos.txt  is directory?  False

ZipInfo.compress_type

Type of compression for the archive member.

ZipInfo.comment

Comment for the individual archive member as a bytes object.

ZipInfo.extra

Expansion field data. The PKZIP Application Note contains some comments on the internal structure of the data contained in this bytes object.

ZipInfo.create_system

System which created ZIP archive.

ZipInfo.create_version

PKZIP version which created ZIP archive.

ZipInfo.extract_version

PKZIP version needed to extract archive.

ZipInfo.flag_bits

ZIP flag bits.

ZipInfo.volume

Volume number of file header.

ZipInfo.internal_attr

Internal attributes.

ZipInfo.external_attr

External file attributes.

ZipInfo.header_offset

Byte offset to the file header.

ZipInfo.CRC

CRC-32 of the uncompressed file.

ZipInfo.compress_size

Size of the compressed data.

ZipInfo.file_size

Size of the uncompressed file.

With this article at OpenGenus, you must have a complete idea of zipfile module in Python. Enjoy.