hfutils.archive

Overview:

Archive pack and unpack management.

Supported Formats:

Format

Extension Name

7z

.7z

bztar

.tar.bz2, .tbz2

gztar

.tar.gz, .tgz

rar

.rar

tar

.tar

xztar

.tar.xz, .txz

zip

.zip

Note

If you require support for 7z and RAR formats, simply install hfutils using the following code:

pip install hfutils[7z]
pip install hfutils[rar]

Warning

The creation of archive files in the RAR format is not supported, as we utilize the rarfile library, which does not offer functionality for creating RAR files.

register_archive_type

hfutils.archive.register_archive_type(name: str, exts: List[str], fn_pack: Callable, fn_unpack: Callable)[source]

Register a custom archive type with associated file extensions and packing/unpacking functions.

Parameters:
  • name (str) – The name of the archive type.

  • exts (List[str]) – A list of file extensions associated with the archive type.

  • fn_pack (Callable) – The packing function that takes a directory and an archive filename as input and creates an archive.

  • fn_unpack (Callable) – The unpacking function that takes an archive filename and a directory as input and extracts the archive.

archive_pack

hfutils.archive.archive_pack(type_name: str, directory: str, archive_file: str, silent: bool = False, clear: bool = False)[source]

Pack a directory into an archive file using the specified archive type.

Parameters:
  • type_name (str) – The name of the archive type.

  • directory (str) – The directory to pack.

  • archive_file (str) – The filename of the resulting archive.

  • silent (bool) – If True, suppress warnings during the packing process.

  • clear (bool) – If True, remove existing files when packing.

Returns:

The path to the created archive file.

Return type:

str

archive_unpack

hfutils.archive.archive_unpack(archive_file: str, directory: str, silent: bool = False, password: str | None = None)[source]

Unpack an archive file into a directory using the specified archive type.

Parameters:
  • archive_file (str) – The filename of the archive.

  • directory (str) – The directory to unpack the contents into.

  • silent (bool) – If True, suppress warnings during the unpacking process.

  • password (str, optional) – The password to extract the archive file.

Returns:

The path to the unpacked directory.

Return type:

str

get_archive_type

hfutils.archive.get_archive_type(archive_file: str) str[source]

Determine the archive type based on the file extension.

Parameters:

archive_file (str) – The filename of the archive.

Returns:

The name of the archive type.

Return type:

str

Raises:

ValueError – If the file extension is not associated with any registered archive type.

get_archive_extname

hfutils.archive.get_archive_extname(type_name: str) str[source]

Get the file extension associated with a registered archive type.

Parameters:

type_name (str) – The name of the archive type.

Returns:

The file extension associated with the archive type.

Return type:

str

Raises:

ValueError – If the archive type is not registered.