Python zipfile - Python ZIP With Examples

Python zipfile module helps us in working with zip files. Today we will learn how to read zip archive details, create and extract zip files using zipfile module.

In case you don’t know, there is a built-in zip() function in Python. You can read all about it at AppDividend Python zip() Tutorial.

Python zipfile

Python zipfile module is important for even production-grade application. This is due to the reason that on servers, files uploaded through web applications are often zipped and then saved to save costly server space. Let’s get started with the zipfile module examples. This python module is also similar to python tarfile module.

Please note that for demonstration purposes, we have a ZIP file called Archive.zip with some text files and this ZIP is present in the directory where we run the programs.

Reading a ZIP file

We will start with listing files present inside a ZIP Archive. Here is a sample program:

Let’s see the output for this program:

zipfile_list_files

We were able to list files present in the Archive and some meta-data for files as well. Please note that the process is really fast as we didn’t have to unzip the file before we could read it.

Create a ZIP file

Next, we will start by looking at how a ZIP file can be made (this is how we made it as well). To create a new Archive, we will make an instance of ZipFile with a mode of w. Note that if a file with the same name exists, it will be truncated completely. So, make sure that your file name is unique.

Let’s look at the code snippet to create a zip file using the zipfile module:

Let’s see the output for this program:

zipfile_make_zip

Checking for a Valid ZIP file

We can also test if a mentioned file is a valid ZIP Archive. Here is a sample program:

Let’s see the output for this program:
zipfile_make_zip
This is an important test to be performed while handling ZIP Archives.

Unzipping a ZIP Archive

Let’s look at a code snippet:

Let’s see the output for this program:
zipfile_unzip
Note that a new directory isn’t made, rather, files are put in same directory here. Mention a directory if you want to put files at a particular location.

Adding a file to ZIP with different name

It is possible to add member files into an archive with a different name. Here is a sample program to show how this can be done:

Let’s see the output for this program:

zipfile_unzip

Conclusion

In this Python zipfile tutorial, we saw how we can create ZIP archives and read them. Use this module to produce Zipped files and process them as required.

Reference: API Doc

By admin

Leave a Reply