stouputils.archive module#

This module provides functions for creating and managing archives.

  • make_archive: Create a zip archive from a source directory with consistent file timestamps.

  • repair_zip_file: Try to repair a corrupted zip file by ignoring some of the errors

stouputils archive examples
make_archive(source: str, destinations: list[str] | str | None = None, override_time: None | tuple[int, int, int, int, int, int] = 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 (str) – The source folder to archive

  • destinations (list[str]|str) – The destination folder(s) or file(s) to copy the archive to

  • override_time (None | tuple[int, ...]) – 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 (bool) – Whether to create the destination directory if it doesn’t exist

  • ignore_patterns (str | None) – Glob pattern(s) to ignore files. Can be a single pattern or comma-separated patterns (e.g. “.pyc” or “.pyc,__pycache__,*.log”)

Returns:

Always returns True unless any strong error

Return type:

bool

Examples:

> 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")
repair_zip_file(file_path: str, destination: str) bool[source]#

Try to repair a corrupted zip file by ignoring some of the errors

This function manually parses the ZIP file structure to extract files even when the ZIP file is corrupted. It reads the central directory entries and attempts to decompress each file individually.

Parameters:
  • file_path (str) – Path of the zip file to repair

  • destination (str) – Destination of the new file

Returns:

Always returns True unless any strong error

Return type:

bool

Examples:

> repair_zip_file("/path/to/source.zip", "/path/to/destination.zip")