Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

Compress and Extract ZIP Files Programmatically in Python

Create a ZIP archive with in-memory files and extract its contents to a directory using Python's stdlib zipfile and pathlib modules.

Easy Python 3.9+ Jun 27, 2026 Files & data 1 views 0 copies

Python code

37 lines
Python 3.9+
import zipfile
from pathlib import Path
import tempfile
import os

def create_sample_zip(zip_path: str, files: dict) -> None:
    """Create a ZIP file containing the given files (name -> content mapping)."""
    with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
        for filename, content in files.items():
            zf.writestr(filename, content)

def extract_zip(zip_path: str, extract_dir: str) -> list:
    """Extract all files from a ZIP archive and return list of extracted paths."""
    with zipfile.ZipFile(zip_path, 'r') as zf:
        zf.extractall(extract_dir)
    return [str(p) for p in Path(extract_dir).rglob('*') if p.is_file()]

if __name__ == "__main__":
    # Create a temporary directory for demonstration
    with tempfile.TemporaryDirectory() as tmpdir:
        zip_path = os.path.join(tmpdir, "sample.zip")
        
        # Create sample ZIP with two text files
        files_to_zip = {
            "hello.txt": "Hello, world!",
            "data/notes.txt": "This is a nested file."
        }
        create_sample_zip(zip_path, files_to_zip)
        
        # Extract to a subdirectory
        extract_path = os.path.join(tmpdir, "extracted")
        extracted_files = extract_zip(zip_path, extract_path)
        
        # Verify contents
        for file_path in sorted(extracted_files):
            with open(file_path, 'r') as f:
                print(f"{os.path.relpath(file_path, extract_path)}: {f.read()}")

Output

stdout
data/notes.txt: This is a nested file.
hello.txt: Hello, world!

How it works

The zipfile.ZipFile class provides a high-level interface for reading and writing ZIP archives. Using zipfile.ZIP_DEFLATED enables compression, reducing file size. writestr allows adding files directly from memory without intermediate disk writes. extractall recursively unpacks the archive while preserving directory structure. The example demonstrates round-trip compression and extraction, verifying file contents to confirm integrity.

Common mistakes

  • Forgetting to use 'with' statement causes resource leaks and incomplete writes.
  • Using ZipFile in write mode ('w') on an existing file overwrites it without warning.
  • Assuming ZIP_DEFLATED is available on all systems (rare but possible with some Python builds).

Variations

  1. Use `shutil.make_archive` and `shutil.unpack_archive` for simpler one-liner backups.
  2. Use `zipfile.Path` (Python 3.8+) for a pathlib-compatible traversal of ZIP contents without extracting.

Real-world use cases

  • Packaging log files and reports into a single compressed archive for email attachments or cloud uploads.
  • Unpacking downloaded datasets or configuration bundles in a CI/CD pipeline.
  • Creating in-memory ZIP archives for serving dynamic downloads in a web framework.

Sponsored

Sponsored Reserved space — layout preview until AdSense is connected

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Files & data

Related tutorials and quizzes for this topic.