dmf.io.decompress#
- dmf.io.decompress(input_file: str | Path, output_dir: str | Path = './', compression: str | None = None, password: str | None = None, **kwargs) Path[source]#
Decompress a compressed file.
This function decompresses a file based on its extension or the specified compression format. Supported formats include gzip, bzip2, xz, zip, 7z, and various tar-based formats.
Supported Formats#
gzip (.gz, .gzip)
bzip2 (.bz2, .bzip2)
xz (.xz)
zip (.zip)
7z (.7z)
tar (.tar)
tar.gz (.tar.gz, .tgz)
tar.bz2 (.tar.bz2)
tar.xz (.tar.xz)
- param input_file:
The compressed input file path.
- type input_file:
Union[str, Path]
- param output_dir:
The directory where files should be extracted. Defaults to the current directory (“./”).
- type output_dir:
Union[str, Path], optional
- param compression:
The compression format. If not provided, it will be inferred from the file extension.
- type compression:
Optional[str], optional
- param password:
Password for the archive, supported only for ZIP and 7z formats.
- type password:
Optional[str], optional
- param kwargs:
Additional keyword arguments to pass to the decompression function.
- type kwargs:
dict
- returns:
The path to the directory containing the decompressed files.
- rtype:
Path
- raises ValueError:
If the input file is invalid or if the compression format is unsupported.
- raises NotImplementedError:
If password protection is used with an unsupported compression format.
- raises ImportError:
If a required library for a specific compression format is not installed.
Examples
Example 1: Decompressing a gzip file
decompress("example.gz", output_dir="output")
Example 2: Decompressing a zip file
decompress("example.zip", output_dir="output")
Example 3: Decompressing a 7z file with a password
decompress("example.7z", output_dir="output", password="mypassword")
Notes
The function automatically detects the compression format based on the file extension.
The output directory will be created if it does not exist.
For unsupported formats or missing libraries, appropriate errors are raised.