Python zipfile Compression Methods Guide

Python's built-in zipfile module provides native support for archiving files with various compression algorithms, primarily uncompressed storage, DEFLATE, BZIP2, and LZMA. This guide explains which compression methods are supported, the standard library dependencies required to use them, and how to implement each algorithm in your Python code.

Supported Compression Methods

The zipfile module defines compression types using integer constants. The four primary methods supported are:

  1. zipfile.ZIP_STORED (Default)

    • Algorithm: None.
    • Description: Files are copied directly into the archive without any compression.
    • Requirements: Always available; requires no external or optional modules.
    • Best used for: Formats that are already compressed (such as JPEG, MP3, or MP4) or when archive creation speed is the primary concern.
  2. zipfile.ZIP_DEFLATED

    • Algorithm: DEFLATE.
    • Description: The traditional and most widely compatible ZIP compression algorithm.
    • Requirements: Requires the zlib module from the Python standard library.
    • Best used for: General-purpose archiving where wide cross-platform compatibility across different unzipping tools is required.
  3. zipfile.ZIP_BZIP2

    • Algorithm: BZIP2.
    • Description: Uses the Burrows-Wheeler transform. It typically achieves higher compression ratios than DEFLATE, especially on plain text, but with slower processing times.
    • Requirements: Requires the bz2 module from the standard library.
    • Best used for: Scenarios needing better compression than DEFLATE where LZMA is too resource-intensive or unavailable.
  4. zipfile.ZIP_LZMA

    • Algorithm: LZMA (Lempel-Ziv-Markov chain algorithm).
    • Description: Offers the highest compression ratio among the supported formats, similar to 7-Zip archives, but requires significantly more memory and CPU time to compress.
    • Requirements: Requires the lzma module from the standard library.
    • Best used for: Large files and distribution packages where minimizing download size is prioritized over compression speed.

Implementation Example

To apply a specific algorithm, pass the corresponding constant to the compression argument when initializing a zipfile.ZipFile object:

import zipfile

# 1. Uncompressed (Stored)
with zipfile.ZipFile('stored_archive.zip', 'w', compression=zipfile.ZIP_STORED) as zipf:
    zipf.write('example.txt')

# 2. Standard DEFLATE compression
with zipfile.ZipFile('deflate_archive.zip', 'w', compression=zipfile.ZIP_DEFLATED) as zipf:
    zipf.write('example.txt')

# 3. BZIP2 compression
with zipfile.ZipFile('bzip2_archive.zip', 'w', compression=zipfile.ZIP_BZIP2) as zipf:
    zipf.write('example.txt')

# 4. LZMA compression
with zipfile.ZipFile('lzma_archive.zip', 'w', compression=zipfile.ZIP_LZMA) as zipf:
    zipf.write('example.txt')

Compression Level Support

For ZIP_DEFLATED and ZIP_BZIP2, Python allows you to adjust the trade-off between speed and size using the compresslevel parameter:

# Applying maximum DEFLATE compression
with zipfile.ZipFile('max_deflate.zip', 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=9) as zipf:
    zipf.write('example.txt')

Handling Missing Modules

If a requested compression method relies on an underlying C library that was omitted when Python was compiled (such as missing zlib, bz2, or lzma development headers), Python raises a RuntimeError:

try:
    with zipfile.ZipFile('archive.zip', 'w', compression=zipfile.ZIP_LZMA) as zipf:
        zipf.write('example.txt')
except RuntimeError as e:
    print(f"Compression method not available on this Python installation: {e}")