πŸ“¦ archive#

This module provides functions for creating and managing archives.

  • repair_zip_file() - Try to repair a corrupted zip file by ignoring some of the errors

  • make_archive() - Create a zip archive from a source directory with consistent file timestamps.

  • archive_cli() - Main entry point for command line usage

import zipfile
from pathlib import Path
import stouputils as stp

# Create a folder with 50 small text files
for index in range(50):
    stp.super_open(f"project/notes/note_{index}.txt", "w").write(f"Note {index}. " * 500)

# Zip the folder
stp.make_archive("project", "project.zip")

# Keep only the first two thirds of the zip, like an interrupted download
content = Path("project.zip").read_bytes()
two_thirds = len(content) * 2 // 3
Path("partial.zip").write_bytes(content[:two_thirds])

# Python's zipfile refuses to open it
try:
    zipfile.ZipFile("partial.zip")
except zipfile.BadZipFile as error:
    stp.warning("zipfile cannot open partial.zip:", error)

# stouputils still recovers the files it can read
stp.repair_zip_file("partial.zip", "repaired.zip")
recovered_files = zipfile.ZipFile("repaired.zip").namelist()
stp.info("Recovered", len(recovered_files), "of 50 files")
Terminal output of the example, the truncated archive failing to open then being repaired

cli#

archive_cli() → None[source]#

Main entry point for command line usage.

# Repair a corrupted zip file
python -m stouputils.archive repair /path/to/corrupted.zip /path/to/repaired.zip

# Create a zip archive
python -m stouputils.archive make /path/to/source /path/to/destination.zip

# Create a zip archive with ignore patterns
python -m stouputils.archive make /path/to/source /path/to/destination.zip --ignore "*.pyc,__pycache__"

creation#

make_archive(
source: str,
destinations: list[str] | str | None = None,
override_time: tuple[int, int, int, int, int, int] | None = None,
create_dir: bool = False,
ignore_patterns: str | None = None,
) → bool[source]#

Create a zip archive from a source directory with consistent file timestamps. (Meaning deterministic zip file each time)

Creates a zip archive from the source directory and copies it to one or more destinations. The archive will have consistent file timestamps across runs if override_time is specified. Uses maximum compression level (9) with ZIP_DEFLATED algorithm.

Parameters:
  • source – The source folder to archive

  • destinations – The destination folder(s) or file(s) to copy the archive to

  • override_time – The constant time to use for the archive (e.g. (2024, 1, 1, 0, 0, 0) for 2024-01-01 00:00:00)

  • create_dir – Whether to create the destination directory if it doesn’t exist

  • ignore_patterns – Glob pattern(s) of the files to ignore, one pattern or several separated by commas (e.g. β€œ.pyc” or β€œ.pyc,__pycache__,*.log”)

Returns:

Always returns True unless any strong error

> make_archive("/path/to/source", "/path/to/destination.zip")
> make_archive("/path/to/source", ["/path/to/destination.zip", "/path/to/destination2.zip"])
> make_archive("src", "hello_from_year_2085.zip", override_time=(2085,1,1,0,0,0))
> make_archive("src", "output.zip", ignore_patterns="*.pyc")
> make_archive("src", "output.zip", ignore_patterns="__pycache__")
> make_archive("src", "output.zip", ignore_patterns="*.pyc,__pycache__,*.log")
zip_folder(
source: str,
destination: str,
ignore_patterns: list[str],
override_time: tuple[int, int, int, int, int, int] | None,
) → None[source]#

Write every file under source into a new zip at maximum compression, at its path relative to source.

Parameters:
  • ignore_patterns – Glob patterns of the files and folders left out, matched as in matches_any().

  • override_time – Timestamp given to every entry, which makes the archive the same byte for byte, None to keep the file dates.

matches_any(path: str, patterns: list[str]) → bool[source]#

Whether the path, or its last component, matches one of the glob patterns.

>>> matches_any("src/cache/data.pyc", ["*.pyc"]), matches_any("src/__pycache__", ["__pycache__"]), matches_any("a.py", [])
(True, True, False)