Python Zipfile – Benefits, Modules, Objects in Zipfiles in Python

Python course with 57 real-time projects - Learn Python

In our previous tutorial, we studied Image Processing with Scipy and NumPy in Python and in this article, we will learn about Python Zipfile.

Moreover, we will see how we can extract Zipfile in Python. In addition, we will cover how can we write Python Zipfile, and getting information about them using Python.

At last, we will see some methods of the Python Zipfiles module provides and exceptions.

So, let’s begin Python Zipfile Tutorial.

Python Zipfile - Benefits, Modules, Objects in Zipfiles in Python

Python Zipfile – Benefits, Modules, Objects in Zipfiles in Python

What is Python Zipfile?

Python Zipfile is an ideal way to group similar files and compress large files to reduce their size. The compression is lossless.

This means that using the compression algorithm, we can operate on the compressed data to perfectly reconstruct the original data.

So, in Python Zipfile is an archive file format and a compression standard; it is a single file that holds compressed files.

Advantages of Python Zipfiles

Bunching files into zips offer the following advantages:

1. It reduces storage requirements
Since ZIP files use compression, they can hold much more for the same amount of storage

2. It improves transfer speed over standard connections
Since it is just one file holding less storage, it transfers faster

Now, let’s learn about the module zipfile.

Python Zipfile Module

The Python zipfile module has tools to create, read, write, append, and list ZIP files. At the time of writing.

It lets us handle ZIP files that use ZIP64 extensions and decrypt encrypted files in ZIP archives.

It cannot handle multi-disk ZIP files or create encrypted files.

Python Zipfile Module has the following members:

1. exception zipfile.BadZipFile

For a bad ZIP file, it raises this exception.

2. exception zipfile.BadZipfile

This is an alias for the previous exception in the list. It is to make it compatible with older Python versions. This is deprecated since version 3.2.

3. exception zipfile.LargeZipFile

When a Python ZIPfile needs ZIP64 functionality, but it hasn’t been enabled, Python throws this exception.

4. class zipfile.ZipFile

This is the class for reading and writing ZIP files in Python.

5. class zipfile.PyZipFile

This class lets us create ZIP archives holding Python libraries.

6. class zipfile.ZipInfo(filename=’NoName’, date_time=(1980, 1, 1, 0, 0, 0))

With this class, we can represent information about an archive member.

The getinfo() and infolist() methods of Python ZipFile objects return instances of this class.

7. zipfile.is_zipfile(filename)

This considers the magic number of a ZIP file. If it is a valid ZIP file, it returns True; otherwise, False. This works on files and file-like objects.

8. zipfile.ZIP_STORED

This is the numeric constant for uncompressed archive members.

9. zipfile.ZIP_DEFLATED

This is the numeric constant for the usual ZIP compression method. It needs the zlib module.

10. zipfile.ZIP_BZIP2

This is the numeric constant for the BZIP2 compression method. It needs the bz2 module.

11. zipfile.ZIP_LZMA

This is the numeric constant for the LZMA compression method. It needs the lzma module.

Python ZipFile Objects

Python zipfile class this type:

1. class zipfile.ZipFile(file, mode=’r’, compression=ZIP_STORED, allowZip64=True)

This method opens a Python ZIPfile. Here, file may be a file-like object or a string path to a file. We have the following modes:

‘r’- To read an existing file

‘w’- To truncate and write a new file

‘a’- To append to an existing file

Using the compression argument, we can select the compression method to use when writing the archive.

allowZip64 is True by default. This creates ZIP files that use ZIP64 extensions for zipfiles larger than GiB.

ZipFile has the following functions:

1. ZipFile.close()

This closes the archive file. If we do not call this before exiting the program, Python doesn’t write the records intended to.

2. ZipFile.getinfo(name)

This returns a ZipInfo object holding information about the archive member name.

3. ZipFile.infolist()

This returns a list holding a ZipInfo object for each archive member.

4. ZipFile.namelist()

This returns a list of archive members by name.

5., mode=’r’, pwd=None)

This function extracts a member from the archive as a file-like object (CipExtFile).

The mode can be ‘r’, ‘U’, or ‘rU’. pwd is the password for an encrypted file. name is a filename in the archive or a ZipInfo object.

Since it is also a context manager, we can use it with the ‘with’ statement:

with ZipFile(‘’) as myzip:

with‘eggs.txt’) as myfile:


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

This extracts a member from the archive to the current working directory. member may be a filename or a ZipInfo object, path is a different directory to extract to, and pwd is the password for an encrypted file.

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

This extracts all members from the archive to the current working directory. The arguments mean the same as above.

8. ZipFile.printdir()

This prints a table of contents for the archive to sys.stdout.

9. ZipFile.setpassword(pwd)

This sets pwd as the default password to extract encrypted files.

10., pwd=None)

This returns the bytes of name in the archive, where name is the name of a file in the archive, or of a ZipFile object.

11. ZipFile.testzip()

This checks the CRCs and file headers for all files in the archive, and returns the name of the first bad file. If there is none, it returns None.

12. ZipFile.write(filename, arcname=None, compress_type=None)

This writes the file filename to the archive, calling it arcname.

13. ZipFile.writestr(zinfo_or_arcname, bytes[, compress_type])

This writes the string bytes to the archive

We also have some data attirbutes:

  •  ZipFile.debug

This denotes the level of debug output to use. 0 means no output (default) and 3 means the most output.

  • ZipFile.comment

This is the comment text linked with a Python ZIPfile.

Extracting ZIP Files in Python

Now, let’s try it hands-on. Let’s try extracting a Python Zipfile.

>>> from zipfile import ZipFile
>>> import os
>>> os.chdir("C:\\Users\\lifei\\Desktop")
>>> file=""
>>> with ZipFile(file,'r') as zip:          #ZipFile constructor; READ mode; ZipFile object named as zip
        zip.printdir()                      #To print contents of the archive
print("Extracting files")
        zip.extractall()                    #Extract contents of the ZIP to the current working directory
print("Finished extracting")


File Name                 Modified               Size

Demo/1.txt            2018-06-15 17:40:06          0

Demo/2.txt            2018-06-15 17:40:12          0

Demo/3.txt            2018-06-15 17:40:16          0

Extracting files

Finished extracting

As you can see, this extracts all files in the ZIP It creates a folder labeled ‘Demo’ on the Desktop.

We explain the code through comments. You can also extract just a single file using the method extract():

>>> with ZipFile(file,'r') as zip:



This creates a folder on the Desktop labeled ‘Demo’. But this time, it only contains one file- ‘2.txt’.

How to Write Python ZIP File?

We use the write() method to write to a ZIP. Here’s the code we use:

>>> from zipfile import ZipFile
>>> import os
>>> os.chdir('C:\\Users\\lifei\\Desktop')
>>> def get_paths(directory):
        for root, directories, files in os.walk(directory):
           for filename in files:
     return paths
>>> directory='./Demo'
>>> paths=get_paths(directory)
>>> print("Zipping these files:")
Zipping these files:
>>> for file in paths:
>>> with ZipFile('','w') as zip:
for file in paths:
>>> print("Zip successful")


Zip successful

Now let’s see how this works:

We create a function with uses the method os.walk(). In every iteration, it appends the files in that directory to the list paths.

Then, we get a list of the file paths bypassing the Demo directory’s path to the function get_paths().

Then, we create a ZipFile object in WRITE mode. Finally, we use the write() method to write all these files to the ZIP.

Getting Information about ZIP Files in Python

To find out more about a Python zipfile, we use the method infolist(). Let’s see how:

>>> from zipfile import ZipFile
>>> import datetime
>>> file=""
>>> with ZipFile(file,'r') as zip:
       for info in zip.infolist():
         print('\tZIP version:\t'+str(info.create_version))
         print('\tCompressed:\t'+str(info.compress_size)+' bytes')
         print('\tUncompressed:\t'+str(info.file_size)+' bytes')


Modified:            2018-06-15 17:56:32
System:              0(0=Windows,3=Unix)
ZIP version:          20
Compressed:        0 bytes
Uncompressed:    0 bytes
Modified:                  2018-06-15 17:57:18
System:                    0(0=Windows,3=Unix)
ZIP version:              20
Compressed:            29 bytes
Uncompressed:       40 bytes
Modified:           2018-06-15 17:56:42
System:             0(0=Windows,3=Unix)
ZIP version:        20
Compressed:      0 bytes
Uncompressed:   0 bytes

Here, we use the method infolist() to create an instance of the ZipInfo class that holds all information about the Python zipfile.

It lets us access information like file names, a system where the file was created, file modification data, ZIP version, size of files, and so.

So, this was all about Python Zipfile Tutorial. Hope you like our explanation.

Python Interview Questions on Zipfile

  1. How to use a Zipfile module in Python?
  2. How to extract a Zipfile in Python?
  3. Explain how to Zip a folder in Python?
  4. How to unzip a file in Python?
  5. How to zip multiple files in Python?


Hence, like we’ve always said, there are so many things you can do with Python. Using the Python zipfile module, we can even handle ZIP files.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *