Python zipfile – Python ZIP

Filed Under: Python
Python Zipfile Module

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. Sometime back we look at python zip function.

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:


import zipfile

zip_archive = zipfile.ZipFile("Archive.zip", "r")

# list file information
for file_info in zip_archive.infolist():
    print(file_info.filename, file_info.date_time, file_info.file_size)

Let’s see the output for this program:
python zipfile infolist

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:


import zipfile

archive = zipfile.ZipFile('Archive.zip', mode='w')
try:
    archive.write('hello.txt')
    archive.write('second.txt')
    print('Files added.')
finally:
    print('Reading files now.')
    archive.close()

zip_archive = zipfile.ZipFile("Archive.zip", "r")

# list file information
for file_info in zip_archive.infolist():
    print(file_info.filename, file_info.date_time, file_info.file_size)

Let’s see the output for this program:
zipfile create zip file

Checking for a Valid ZIP file

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


import zipfile

test_files = ['check_if_zipfile.py', 'Archive.zip']

for file in test_files:
    print('ZIP status for {0}: {1}'.format(file, zipfile.is_zipfile(file)))

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

Unzipping a ZIP Archive

Let’s look at a code snippet:


import zipfile

print('Extracting ZIP.')
archive = zipfile.ZipFile('Archive.zip', 'r')

# Extract to current directory
archive.extractall('.')
print('ZIP Extracted.')

archive.close()

Let’s see the output for this program:
python zipfile extractall
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:


import zipfile

print('Creating Archive.zip.')
archive = zipfile.ZipFile('Archive.zip', mode='w')
try:
    archive.write('hello.txt', arcname='some_hello.txt')
    archive.write('second.txt', arcname='another.txt')
finally:
    archive.close()

print('ZIP created with different name.')

Let’s see the output for this program:
python zipfile write with different file name

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

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages