Compressing and Extracting Member Files
Compressing and Extracting Member Files. If your files are taking up too much space, then you might consider compressing them. Python’s
zipfile supports a few popular compression methods. However, the module doesn’t compress your files by default.
If you want to make your files smaller, then you need to explicitly supply a compression method to
ZipFile. Typically, you’ll use the term stored to refer to member files written into a ZIP file without compression.
compression method is the third argument to the initializer of
ZipFile. If you want to compress your files when you write them to a ZIP archive, then you can set this argument to one of the constants seen on-screen.
These are the compression methods you can currently use with
ZipFile. A different method will raise a
NotImplementedError. There are no additional compression methods available to
zipfile as of Python 3.11. As an additional requirement, If you choose one of these methods, then the compression module that supports it must be available in your Python installation. Otherwise, you’ll get a
RuntimeError exception, and your code will break.
Another relevant argument of
ZipFile when it comes to compressing your files is
compresslevel. This argument controls which compression level you use. With the Deflate method,
compresslevel can take integer numbers from zero through nine.
01:43 Note that many binary files, such as PNG, JPG, and MP3, already use some kind of compression. As a result, adding them to a ZIP file may not make the data any smaller because it’s already compressed.
Here you pass
compresslevel to get maximum compression. To provide this argument, you use a keyword argument. You need to do this because
compresslevel isn’t the fourth positional argument in the
The initializer of
ZipFile takes a fourth argument called
allowZip64. It’s a Boolean argument that tells
ZipFile to create ZIP files with the
.zip64 extension for files larger than four gigabytes.
After running this code, you’ll have a
comp_dir.zip file in your current directory. If you compare the size of that file with the size of
directory.zip, which contains the same member files, but without compression, then you’ll notice significant size reduction.
03:31 Creating ZIP files sequentially can be another common requirement in your day-to-day programming. For example, you may need to create an initial ZIP file with or without content and then append new member files as soon as they become available. In this situation, you need to open and close the target ZIP file multiple times.
To solve this problem, you can use
ZipFile in append mode (
"a"), as you already have done. This mode allows you to safely append new member files to a ZIP archive without truncating its current content.
append_member() is a function that appends a file (
member) to the input ZIP archive (
zip_file). To perform this action, the function opens and closes the target archive every time you call it.
Using a function to perform this task allows you to reuse the code as many times as you need. The
get_file_from_stream() function is a generator function simulating a stream of files to process.
One of the most common operations you’ll ever perform on ZIP files is to extract their content to a given directory in your file system. You already learned the basics of using
.extract_all() to extract one or all of the files from an archive. As an additional example, go back to the
sample.zip file. At this point, the archive contains four files of different types.
with statement opens
sample.zip for reading, and the
for loop iterates over each file in the archive using
namelist(), while the conditional statement checks if the filename ends with the
.md extension. If it does, then you extract the file at hand to a target directory,
Become a Member to join the conversation.