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.
Python code
37 linesimport 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
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
- Use `shutil.make_archive` and `shutil.unpack_archive` for simpler one-liner backups.
- 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
More from Files & data
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a Python Script That Detects and Deletes Empty Files Across Folders easy
- Compare Two Folder Structures and Find Differences in Python easy
- Convert CSV Files to JSON in Python easy
- Convert Image to ASCII Art in Python medium
- Create a Personal Knowledge Base That Searches Notes Instantly in Python easy
Keep learning
Related tutorials and quizzes for this topic.