Manipulating Existing ZIP Files
Manipulating Existing ZIP Files With Python’s
zipfile provides convenient classes and functions that allow you to create, read, write, extract, and list the contents of your ZIP files.
Here are some additional features that
zipfile supports: ZIP files greater than four gigabytes; data decryption; several compression algorithms, such as Deflate, Bzip2, and LZMA; and information integrity checks with CRC32.
Be aware that
zipfile does have some limitations. For example, the current data decryption feature can be fairly slow because it uses pure Python code. The module can’t handle the creation of encrypted ZIP files.
And finally, the use of multi-disk ZIP files isn’t supported either. Despite these limitations,
zipfile is a great and useful tool, and you’ll learn all about its capabilities in this course. In the
zipfile module, you’ll find the
ZipFile implements the context manager protocol so you can use the class in a
with statement. This feature allows you to quickly open and work with a ZIP file without worrying about closing the file after you finish your work.
01:37 Before writing any code, make sure you have a copy of the files and archives that you’ll be using by downloading the course materials. You can then unzip them using your operating system’s built-in ZIP-handling method shown here by double-clicking in macOS, whereas right-clicking on the archive and choosing Extract All will work in Windows.
You should see a listing similar to the one seen on-screen. Once you have these files and directories present in your working folder, you are ready to open up a Python interactive session and to start working with ZIP files. To warm up, you’ll start by reading the ZIP file called
sample.zip. To do that, you can use the code seen on-screen.
The first argument to the initializer of
ZipFile can be a string representing the path to the ZIP file that you need to open. This argument can accept file-like and path-like objects too. On-screen, you use a string-based path.
The second argument to
ZipFile is a single-letter string representing the mode that you’ll use to open the file. As you saw earlier,
ZipFile can accept four possible modes depending on your needs.
Its output has a user-friendly tabular format with three informative columns:
Size. If you want to make sure that you are targeting a valid ZIP file before you try to open it, then you can wrap
ZipFile in a
except statement and catch any
BadZipFile exception, as seen on-screen.
ZipFile is smart enough to create a new archive when you use the class in write mode and the target archive doesn’t exist. However, the class doesn’t create new directories in the path to the target ZIP file if those directories don’t already exist.
Become a Member to join the conversation.