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.
Python code
43 linesimport 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
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
- 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
More from Automation & scripting
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
- Automatically Log CPU, RAM, and Disk Usage Every Minute in Python easy
- Batch Rename Hundreds of Files in Python easy
Keep learning
Related tutorials and quizzes for this topic.