zipfile

The Python zipfile module provides tools to create, read, write, and extract ZIP archives. With just a few lines of code, you can manipulate ZIP files to back up data, share files, or save disk space.

Here’s a quick example:

Python
>>> import zipfile

>>> with zipfile.ZipFile("example.zip", mode="w") as zip_file:
...     zip_file.write("example.txt")
...

Key Features

  • Creates ZIP archives
  • Reads and writes compressed files
  • Supports ZIP64 format for large files
  • Allows for extracting specific files from a ZIP archive
  • Provides a context manager for managing ZIP files
  • Supports multiple compression methods
  • Handles reading password-protected ZIP files
  • Allows adding files with custom archive names
  • Integrates with file-like objects or streams
  • Supports reading and writing metadata, including comments and timestamps
  • Enables appending files to existing archives

Frequently Used Classes and Functions

Object Type Description
zipfile.ZipFile Class Provides a high-level interface to ZIP archives
zipfile.is_zipfile() Function Tests whether a file is a ZIP archive
zipfile.ZipInfo Class Represents information about a member of a ZIP archive
zipfile.PyZipFile Class Extends ZipFile for working with Python libraries
zipfile.ZipExtFile Class Provides a file-like object for reading an archive member
zipfile.ZipFile.write() Method Adds a file to the ZIP archive
zipfile.ZipFile.extractall() Method Extracts all members from the archive to the current directory or a specified path
zipfile.ZipFile.namelist() Method Returns a list of names of the archive contents

Examples

Reads the contents of a ZIP file:

Python
>>> import zipfile

>>> with zipfile.ZipFile("archive.zip", mode="r") as archive:
...     archive.namelist()
['file.txt']

Extracts all files from a ZIP archive:

Python
>>> with zipfile.ZipFile("archive.zip", mode="r") as archive:
...     archive.extractall("extracted_files")
...

Common Use Cases

  • Backing up directories and files to ZIP files
  • Inspecting the contents of third-party archives before extraction
  • Distributing Python projects as compressed archives
  • Manipulating ZIP files without extracting them to disk
  • Bundling datasets for reproducible experiments
  • Securely transmitting files by compressing and encrypting them

Real-World Example

Suppose you want to create a ZIP archive of all Python files in a project/ directory. This example demonstrates how to do that efficiently:

Python
>>> import pathlib, zipfile
>>> project_dir = pathlib.Path("project")
>>> archive_path = project_dir.with_suffix(".zip")

>>> with zipfile.ZipFile(archive_path, mode="w") as zip_file:
...     for file in project_dir.rglob("*.py"):
...         zip_file.write(file, file.relative_to(project_dir))
...

>>> archive_path.exists()
True

In this example, you create a ZIP archive containing all Python files in a directory, illustrating how the zipfile module can automate the archiving process efficiently.

Tutorial

Python's zipfile: Manipulate Your ZIP Files Efficiently

In this guided tutorial, you'll learn how to manipulate ZIP files using Python's zipfile module from the standard library. Through hands-on examples, you'll learn how to read, write, compress, and extract files from your ZIP files quickly.

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated July 30, 2025