The zipfile Module
ZIP is one of the most popular file formats used for archiving and compression. It has been in use since the days of MSDOS and PC and has been used by famous PKZIP application.
Python's standard library provides zipfile module with classes that facilitate the tools for creating, extracting, reading and writing to ZIP archives.
ZipFile() function
This function returns a ZipFile object from a file parameter which can be a string or file object as created by built-in open() function. The function needs a mode parameter whose default value is 'r' although it can take 'w' or 'a' value for opening the archive in read, write or append mode respectively.
The archive by default is uncompressed. To specify the type of compression algorithm to be used, one of the constants has to be assigned to compression parameter.
zipfile.ZIP_STORED |
for an uncompressed archive member. |
zipfile.ZIP_DEFLATED |
for the usual ZIP compression method. This requires the zlib module. |
zipfile.ZIP_BZIP2 |
for the BZIP2 compression method. This requires the bz2 module. |
zipfile.ZIP_LZMA |
for the LZMA compression method. This requires the lzma module. |
The ZipFile object uses following methods โ
write() method
This method adds the given file to the ZipFile object.
import zipfile
newzip=zipfile.ZipFile('newzip.zip','w')
newzip.write('zen.txt')
newzip.close()
This creates newzip.zip file in th current directory. Additional file can be added to already existing archive by opening it in append mode ('a' as the mode).
import zipfile
newzip=zipfile.ZipFile('newzip.zip','a')
newzip.write('json.txt')
newzip.close()
read() method
This method reads data from a particular file in the archive.
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
data = newzip.read('json.txt')
print (data)
newzip.close()
Output
b'["Rakesh", {"marks": [50, 60, 70]}]'
printdir() method
This method lists all file in given archive.
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
newzip.printdir()
newzip.close()
Output
File Name Modified Size zen.txt 2023-03-30 21:55:48 132 json.txt 2023-04-03 22:01:56 35
extract() method
This method extracts a specified file from archive by default to current directory or to one given as second parameter to it.
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
newzip.extract('json.txt', 'newdir')
newzip.close()
extractall() method
This method extracts all files in the archive to current directory by default. Specify alternate directory if required as parameter.
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
newzip.extractall('newdir')
newzip.close()
getinfo() method
This method returns ZipInfo object corresponding to the given file. The ZipInfo object contains different metadata information of the file.
Following code obtains ZipInfo object of 'zen.txt' from the archive and retrieves filename, size and date-time information from it.
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
info = newzip.getinfo('zen.txt')
print (info.filename, info.file_size, info.date_time)
newzip.close()
Output
zen.txt 132 (2023, 3, 30, 21, 55, 48)
infolist() method
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
info = newzip.infolist()
print (info)
newzip.close()
Output
[<ZipInfo filename='zen.txt' filemode='-rw-rw-rw-' file_size=132>, <ZipInfo filename='json.txt' filemode='-rw-rw-rw-' file_size=35>]
namelist() method
This method of ZipFile object returns a list of all files in the archive.
import zipfile
newzip=zipfile.ZipFile('newzip.zip','r')
info = newzip.namelist()
print (info)
newzip.close()
Output
['zen.txt', 'json.txt']
setpassword() method
This method sets password parameter which must be provided at the time of extracting the archive.
python_data_compression.htm