stouputils.archive.repair.repair module#

class RecoveredArchive(
scanner: ZipScanner,
zip_file: ZipFile,
seen_names: set[str] = <factory>,
entry_index: int = 0,
)[source]#

Bases: object

Archive being rebuilt, which decides what each recovered entry is named.

METADATA_NAME: ClassVar[str] = 'pack.mcmeta'[source]#

Entry worth a second recovery pass, since a pack is worthless without it.

SUPPORTED_METHODS: ClassVar[tuple[int, ...]] = (0, 8)[source]#

Compression methods this module can decode, stored and deflated.

scanner: ZipScanner[source]#

View over the damaged archive.

zip_file: ZipFile[source]#

Archive being written.

seen_names: set[str][source]#

Names already written, so a duplicate never silently replaces an entry.

entry_index: int = 0[source]#

Counter naming the entries whose own name did not survive.

unique_name(name: str) str[source]#

Make a name that no written entry uses yet.

Parameters:

name – Name the entry would like to take

Returns:

Same name, or a suffixed variant when it is already taken

Examples

>>> import io
>>> with ZipFile(io.BytesIO(), "w") as zip_file:
...     archive = RecoveredArchive(scanner=ZipScanner(b""), zip_file=zip_file, seen_names={"a.png"})
...     archive.unique_name("a.png")
'a_recovered.png'
write(name: str, content: bytes) None[source]#

Write one recovered entry.

Parameters:
  • name – Name to write the entry under

  • content – Decompressed content

write_central_entry(
entry: CentralEntry,
) None[source]#

Recover one entry from its central directory metadata, doing nothing when it cannot be read.

Parameters:

entry – Entry announced by the central directory

recover_metadata() None[source]#

Scan the local headers for the metadata entry, used when the central directory is too damaged.

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 – Path of the zip file to repair

  • destination – Destination of the new file

Returns:

Always returns True unless any strong error

Examples:

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