Skip to content

[Python] Use zipfile to compress or decompress file

Last Updated on 2021-01-21 by Clay

Python is the most popular programming language!

Introduction

Everyone is familiar with the compressed file. These compressed file means that we compress one or more files and folders into a file, which is also call archiving.

In addition to archiving, according to the different compression format algorithms, some can even compress the file size to help us better store and transmit these files.

But what if we need to restore the compressed file back to the original file? At this time, we need to decompress according to different compression formats.

Today I want to record how to use Python module zipfile to compress or decompress some fiile as ZIP format.


Introduction to zip archive

The following quotes a section of the introduction on WIKI:

ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. The ZIP file format permits a number of compression algorithms, though DEFLATE is the most common. This format was originally created in 1989 and was first implemented in PKWARE, Inc.'s PKZIP utility,[2] as a replacement for the previous ARC compression format by Thom Henderson. The ZIP format was then quickly supported by many software utilities other than PKZIP. Microsoft has included built-in ZIP support (under the name "compressed folders") in versions of Microsoft Windows since 1998. Apple has included built-in ZIP support in Mac OS X 10.3 (via BOMArchiveHelper, now Archive Utility) and later. Most free operating systems have built in support for ZIP in similar manners to Windows and Mac OS X.
ZIP files generally use the file extensions .zip or .ZIP and the MIME media type application/zip.[1] ZIP is used as a base file format by many programs, usually under a different name. When navigating a file system via a user interface, graphical icons representing ZIP files often appear as a document or other object prominently featuring a zipper.

–– From WIKI (https://en.wikipedia.org/wiki/ZIP_(file_format))

By the way, if you are on a Linux system, you can use the following commands to compress and decompress the zip format.

Compress: zip -r File.zip File
Encryption compress: zip -rP {dddd} test.zip words.txt
Decompress: unzip File.zip

How to use zipfile module

The zipfile is implemented in pure Python and is not composed of C, so the performance may not be excellent. It is not recommended to be used to crack the password of the zip file. However, this module realizes creation, reading, writing, listing… all kinds of operations on the zip format, which is quite complete.

Let's take a look at how to operate the zipfile module!


Using zipfile to compress

Suppose I have a folder named test (in fact, there is no need to assume, I do have it).

As you can see

Then, I can use the following Python code to compress.

# coding: utf-8
import os
import zipfile


# zipfile example
def zip_dir(path):
    zf = zipfile.ZipFile('{}.zip'.format(path), 'w', zipfile.ZIP_DEFLATED)
   
    for root, dirs, files in os.walk(path):
        for file_name in files:
            zf.write(os.path.join(root, file_name))


if __name__ == '__main__':
    path = 'test'
    zip_dir(path)


Output:

As you can see, the test.zip compressed file we compressed appears under the current directory.


Using zipfile module to list files in compressed files

So, if you want to find a large number of files in a zip file, you can refer to the following sample code:

# coding: utf-8
import os
import zipfile


# zipfile example
def zip_list(file_path):
    zf = zipfile.ZipFile(file_path, 'r')
    print(zf.namelist())


if __name__ == '__main__':
    file_path = 'test.zip'
    zip_list(file_path)


Output:

['test/test_01.txt', 'test/test_02.txt']


Using zipfile to decompress

If you want to decompress the folder, we can first read the compressed file with zipfile and decompress it with the extractall() function.

# coding: utf-8
import os
import zipfile


# zipfile example
def zip_list(file_path):
    zf = zipfile.ZipFile(file_path, 'r')
    zf.extractall()


if __name__ == '__main__':
    file_path = 'test.zip'
    zip_list(file_path)


In this way, we will see the unzipped folder.


References


Read More

1 thought on “[Python] Use zipfile to compress or decompress file”

  1. Pingback: Compress and Decompress Files with Python zipfile and gzip Modules – vegibit

Leave a ReplyCancel reply

Exit mobile version