Python zipfile module - Comprehensive Examples

Python zipfile module

Compressed files are everywhere, you also need to compress the files before sending them on the internet to reduce the bandwidth. You might also want to compress files, so those files take less space on your desk. Do not you think python should come and tell you how to compress and decompress these files?

Making zip files with Python

Python zipfile module is a built-in Python Module that can give you a high level of abstraction to work with zip-compressed files. You can compress the files to zip format and you can do the reverse to extract them if they are already compressed. though there are certain limitations while extracting already zipfile and we will talk about them in a second.

# example code for the zipping 
from zipfile import ZipFile
import zipfile

with ZipFile('', 'w') as zipObj:
   # Add  file to the zip

How to compress the file to zip Format with Python

Python built-in module zipfile, can help us to compress any single or multiple files into zip format. The zip format is one of the popular compressing formats, which is widely used. There are many ways we can use the zipfile module to do the compression with different configurations.

To learn about the zip compression with python in detail, please check out my previous article, it will help you get into the bits and bytes of the zip compression with python.

How to get information of a zip file with Python

We can use the getinfo() method of the ZipFile() object to get the information of the zip file. remember this is the zip file that we have created and is not any zip file that we have read with another python code. We can first convert any zip file to the ZipFile object and then call the getinfo() method, otherwise, it will give us an error.

The other thing about the getinfo() method is that, if the zipfile archive does not contain any member yet, then it will still give you an error.

from zipfile import ZipFile
import zipfile

zipObj = ZipFile('', 'w',compression=zipfile.ZIP_DEFLATED)

# Output

# ZipInfo filename='Roughwork/pretify.html' 
#compress_type=deflate filemode='-rw-rw-rw-' 
#file_size=403 compress_size=249>

Opening a Zip Files without extracting with python

With method you can get any specific file without extracting the whole zip file. We first have to create the instance of the ZipFile() and then call the open() method on this instance and we will get the required file and all the information within that file.

For example, in the following code, we have opened a zip file that contains a text file and there are two lines in that text file. If we want to read that file we can do it as follows.

from zipfile import ZipFile
import zipfile

with ZipFile('') as myzip:
    with'Roughwork/textfile.text') as myfile:

Extracting a Zip File with Python

The ZipFile() class provides two methods to extract the file in a zip file. The extract() method is used to extract a single specified file out of a zip file and the extractall() method is used to extract all the zip files. Below is the code that shows you how to use it. You can also look at how to extract a zip file with Python, in my previous article.

from zipfile import ZipFile
import zipfile

myzip = ZipFile('') 

Get the list of all content of a Zip File with Python

The Python zipfile Module also provides a method to see all the contents of the zip file. The method that i am calling below is the printdir() method will show you the data of the zip file. It will not just output the file data but also the metadata about the file.

from zipfile import ZipFile
import zipfile

myzip = ZipFile('') 

Creating a Zip file of All Python files

in the zipfile module, there is also a cool class PyZipFile() which gives you the ability to search for all the python files in the directory specified and then create a zip file of those python files. this is kind of a cool track that you can do while you are working on a large set of files.

from zipfile import PyZipFile, ZipFile
import zipfile

with PyZipFile('','w') as myzip:

Resources to look for More about Zip File And Python

Post a Comment

* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !