Skip to content

callbacks

ZhouSicheng-2011 edited this page Jul 31, 2026 · 1 revision

Here is the callback definitions for bit7z-python, showing the Python signatures (function names correspond to the C++ typedef aliases). All callbacks are std::function under the hood – just pass any Python callable.


βœ… Bound Callbacks (available)

FileCallback

def file_callback(path: str) -> None:
    """Called when the current file being processed changes."""

Use: Notifies about each file as extraction/compression progresses.

Example:

def on_file(path: str) -> None:
    print(f"Processing: {path}")
extractor.set_file_callback(on_file)

PasswordCallback

def password_callback() -> str:
    """Return the password to use for the archive."""

Use: Called when a password is required (e.g., for encrypted archives).

Example:

def get_password() -> str:
    pwd = input("Password: ")  # Notice: it's just a example, this is not a safe way! 
    return pwd
compressor.set_password_callback(get_password)

ProgressCallback

def progress_callback(processed: int) -> bool:
    """
    Called with the current processed size in bytes.
    Return True to continue, False to abort the operation.
    """

Use: Report progress; can abort by returning False.

Note

It can be combined with TotalCallback to get finished ratio.

Example:

def on_progress(processed: int) -> bool:
    print(f"Processed: {processed} bytes")
    return True
extractor.set_progress_callback(on_progress)

RatioCallback

def ratio_callback(in_size: int, out_size: int) -> None:
    """Called with current input size and output size."""

Use: Monitor compression ratio (input vs. output).

Example:

def on_ratio(in_size: int, out_size: int) -> None:
    ratio = out_size / in_size if in_size else 0
    print(f"Ratio: {ratio:.2%}")
compressor.set_ratio_callback(on_ratio)

TotalCallback

def total_callback(total: int) -> None:
    """Called once the total size of the operation is known."""

Use: Initialize progress bars.

Example:

def on_total(total: int) -> None:
    print(f"Total size: {total} bytes")
extractor.set_total_callback(on_total)

RenameCallback

def rename_callback(item: bit7z_python.BitArchiveItem) -> str:
    """
    Return the new path for the extracted item.
    Return the original path to keep it unchanged.
    Return an empty string to skip this item.
    """

Use: Dynamically rename or skip items during extraction.

Example:

def rename(item: bit7z_python.BitArchiveItem) -> str:
    if item.is_dir():
        return ""  # skip folders
    return "new_" + item.path()
extractor.extract(in_archive, out_dir, rename)

FilterCallback

def filter_callback(item: bit7z_python.BitArchiveItem) -> bit7z_python.FilterResult:
    """
    Return ProcessItem to process the item,
    SkipItem to skip it,
    AbortOperation to abort the whole operation.
    """

Use: Decide per item whether to process, skip, or abort.

Example:

def filter(item: bit7z_python.BitArchiveItem) -> bit7z_python.FilterResult:
    if item.name().endswith(".tmp"):
        return bit7z_python.FilterResult.SkipItem
    return bit7z_python.FilterResult.ProcessItem
extractor.extract_if(in_archive, out_dir, filter)

🚫 Unbound Callbacks (not yet usable)

BufferCallback 🚫

# Not bound (involves reference to buffer_t&)
# def buffer_callback(index: int, path: str) -> bytes:
#     """Return a mutable buffer to extract the item into."""

Status: Not available in current version.


RawDataCallback 🚫

# Not bound (involves raw byte pointer)
# def raw_data_callback(data: bytes, size: int) -> bool:
#     """Called with raw extracted data chunks."""

Status: Not available in current version.


LegacyRenameCallback 🚫

# Deprecated (since v4.1)
# def legacy_rename_callback(index: int, path: str) -> str:
#     """Old form of RenameCallback (index + path)."""

Status: Deprecated; use RenameCallback instead.


Combined Example

import bit7z_python as b7z

lib = b7z.Bit7zLibrary()
ext = b7z.BitFileExtractor(lib, b7z.FORMAT_AUTO)

def on_total(total: int) -> None:
    print(f"Total: {total}")

def on_progress(processed: int) -> bool:
    print(f"Progress: {processed}")
    return True

def on_file(path: str) -> None:
    print(f"File: {path}")

ext.set_total_callback(on_total)
ext.set_progress_callback(on_progress)
ext.set_file_callback(on_file)

ext.extract("archive.7z", "./output/")

All bound callbacks accept the signatures above; unbound ones will be added in future releases.

Clone this wiki locally