Maintenance

Site is under maintenance — quizzes are still available.

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

How to Compress a Folder in Python While Preserving Directory Structure

A Python function that uses zipfile to recursively compress a folder, maintaining the original directory hierarchy inside the zip archive.

Easy Python 3.9+ Jun 28, 2026 Automation & scripting 2 views 0 copies

Python code

43 lines
Python 3.9+
import os
import zipfile
from pathlib import Path

def compress_folder(source_dir: str, output_zip: str):
    """
    Compress a folder into a zip file, preserving the directory structure.
    
    Args:
        source_dir: Path to the source directory to compress
        output_zip: Path for the output zip file
    """
    source_path = Path(source_dir)
    
    with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
        for file_path in source_path.rglob('*'):
            if file_path.is_file():
                # Calculate relative path to preserve structure
                arcname = str(file_path.relative_to(source_path.parent))
                zipf.write(file_path, arcname)
    
    print(f"Compressed '{source_dir}' -> '{output_zip}'")

if __name__ == "__main__":
    # Example usage
    test_dir = "example_folder"
    os.makedirs(test_dir, exist_ok=True)
    
    # Create test files with subdirectories
    sub_dir = os.path.join(test_dir, "subdir")
    os.makedirs(sub_dir, exist_ok=True)
    
    with open(os.path.join(test_dir, "file1.txt"), "w") as f:
        f.write("Hello from file 1")
    with open(os.path.join(test_dir, "subdir", "file2.txt"), "w") as f:
        f.write("Hello from file 2")
    
    compress_folder(test_dir, "compressed_example.zip")
    
    # Cleanup
    import shutil
    shutil.rmtree(test_dir)
    os.remove("compressed_example.zip")

Output

stdout
Compressed 'example_folder' -> 'compressed_example.zip'

How it works

The function uses pathlib.Path.rglob('*') to walk through all files in the source directory recursively. It computes each file's relative path to the source folder's parent, preserving the full folder tree inside the zip archive. The zipfile.ZipFile context manager with ZIP_DEFLATED enables compression. Using relative_to ensures zip entries match the original folder structure, not absolute paths.

Common mistakes

  • Forgetting to set `zipfile.ZIP_DEFLATED` — files will be stored uncompressed.
  • Using absolute paths in `arcname` — breaks the intended directory structure in the zip.

Variations

  1. Use `shutil.make_archive` for a one-liner with top-level directory inclusion.

Real-world use cases

  • Automating backup of project directories before deployment or migration.
  • Packaging user-uploaded folders into a single zip for download, preserving subfolder layout.
  • Archiving log directories from a server with date-stamped zip names for compliance.

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 Automation & scripting

Related tutorials and quizzes for this topic.