Using Other Libraries to Manage ZIP Files
00:00
Using Other Libraries to Manage ZIP Files. There are a few other tools in the Python standard library that you can use to archive, compress, and decompress your files at a lower level. Python’s zipfile uses some of these internally, mainly for compression purposes.
00:16
On-screen is a summary of these tools. Unlike zipfile, some of these modules allow you to compress and decompress data from memory and data streams other than regular files and archives.
00:28
In the Python standard library, you’ll also find tarfile, which supports the TAR archiving format. There’s also a module called gzip, which provides an interface to compress and decompress data, similar to how the new GNU Gzip program does it.
00:45
For example, you can use gzip to create a compressed file containing some text.
01:04
Once you run this code, you’ll have a hello.txt.gz archive containing a compressed version of hello.txt in your current directory. Inside hello.txt, you’ll find the text Hello, World!.
01:21
A quick and high-level way to create a ZIP file without using zipfile is to use shutil. This module allows you to perform several high-level operations on files and collections of files. When it comes to archiving operations, you have make_archive(), which can create archives such as ZIP or TAR files.
01:46
This code creates a compressed file called shutil_sample.zip in your working directory. This ZIP file will contain all the files in the input directory, source_dir/.
01:59
The make_archive() function is convenient when you need a quick and high-level way to create your ZIP files in Python. In the next section of the course, you’ll take a look back at what you’ve covered.
Become a Member to join the conversation.
