core.files.uncompress

core.files.uncompress.get_compression_ext(f: str | Path)[source]

Detect the compression format of a file using the system ‘file’ command.

This function uses the Unix ‘file’ command to determine the compression format of a file based on its content (magic numbers), not just its extension.

Parameters:

fstr or Path

Path to the file to analyze

Returns:

str or None

The detected compression extension (‘.zip’, ‘.tar’, ‘.tar.gz’, ‘.tgz’, ‘.gz’, ‘.bz2’, ‘.Z’) or None if no compression is detected

core.files.uncompress.uncompress(filename: str | Path, target_dir: str | Path, create_out_dir=True, extract_to: Literal['auto', 'subdir', 'target_dir'] = 'auto', verbose=False) Path[source]

Uncompress filename to dirname

Parameters:
  • filename (str or Path) – Path to the file to uncompress

  • target_dir (str or Path) – Directory to uncompress the file to

  • create_out_dir (bool, optional) – If True, create the output directory if it does not exist. Default is True.

  • extract_to ({'auto', 'subdir', 'target_dir'}, default 'auto') –

    ‘auto’: if the root of the archive contains a single file or directory, uncompress

    it to target_dir. Otherwise uncompress it to <target_dir>/<archivefolder>, where archivefolder is a directory named after filename but without the extension.

    ’subdir’: Uncompress the content of the archive to <target_dir>/<archivefolder>, where

    archivefolder is a directory named after filename but without the extension.

    ’target_dir’: uncompress the content of the archive to <target_dir> regardless of the

    content of the archive.

  • verbose (bool, optional) – If True, print verbose output. Default is False.

Returns:

Path to the uncompressed file or directory

Return type:

Path

core.files.uncompress.uncompress_decorator(target_name_func=None, verbose=True)[source]

A decorator that uncompresses the result of function f

Signature of f is assumed to be as follows:

f(identifier: str, dirname: Path, *args, **kwargs)

The file returned by f is uncompressed to dirname

Parameters:

target_name_funccallable, optional

A function that takes an identifier and returns the target name/path for the uncompressed file. The identifier can be a URL or any string. If None, defaults to using Path(identifier).stem.

verbosebool

Whether to display verbose output during uncompression

core.files.uncompress.uncompress_single(filename, output_path, verbose=False)[source]

Decompress a single compressed file (not an archive).

Parameters:

filenamePath

Path to the compressed file

output_pathPath

Path where the decompressed file should be written

verbosebool

Whether to display verbose output

Returns:

Path

Path to the decompressed file