Python Zipping and Unzipping Files

We know that some softwares like 'winzip' provide zipping and unzipping of file data. In zipping the file contents, following two things could happen:

  • The file contents are compressed and hence the size will be reduced.
  • The format of data will be changed making it unreadable.

While zipping a file content, a zipping algorithm (logic) is used in such a way that the algorithm first finds out which bit pattern is most often repeated in the original file and replaces that bit pattern with a 0. Then the algorithm searches for the next bit pattern which is most often repeated in the input file. In its place, a 1 is substituted. The third repeated bit pattern will be replaced by 10, the fourth by 11, the fifth by 100, and so on. In this way, the original bit patterns are replaced by lesser number of bits. This file with lesser number of bits is called 'zipped file' or 'compressed file'.

To get back the original data from the zipped file, we can follow a reverse algorithm, which substitutes the original bit pattern wherever particular bits are found.

python zipping and unzipping original file to compressed file
Zipping and unzipping a file

In Python, the module zipfile contains ZipFile class that helps us to zip or unzip a file contents. For example, to zip the files, we should first pass the zip file name in write mode with an attribute ZIP_DEFLATED to the ZipFile class object as:

f = ZipFile('', 'w', ZIP_DEFLATED)

Here, 'f' is the ZipFile class object to which file name is passed. This is the zip file that is created finally. The next step is to add the filenames that are to be zipped, using write() method as:



Python program to compress the contents of files

from zipfile import * 
f = ZipFile('', 'w', ZIP_DEFLATED) 


print(' file created...') 

A Python program to unzip the contents of the files that are available in a zip file

f#to view contents of zipped files 
from zipfile import * 

#Open the zip file 
z = ZipFile('', 'r') 

#extract all the file names which are in the zip file