embfile.compression

Functions

extract_file(src_path[, member, dest_dir, …])

Extracts a file compressed with gzip, bz2 or lzma or a member file inside a zip/tar archive.

extract_if_missing(src_path[, member, …])

Extracts a file unless it already exists and returns its path.

open_file(path[, mode, encoding, compression])

Open a file, eventually with (de)compression.

Data

COMPRESSION_TO_EXTENSIONS

Maps each compression format to its associated extensions

EXTENSION_TO_COMPRESSION

Maps a compression extensions to the corresponding compression format name

Reference

embfile.compression.open_file(path, mode='rt', encoding=None, compression=None)[source]

Open a file, eventually with (de)compression.

If compression is not given, it is inferred from the file extension. If the file has not the extension of a supported compression format, the file is opened without compression, unless the argument compression is given.

embfile.compression.extract_file(src_path, member=None, dest_dir='.', dest_filename=None, overwrite=False)[source]

Extracts a file compressed with gzip, bz2 or lzma or a member file inside a zip/tar archive. The compression format is inferred from the extension or from the magic number of the file (in the case of zip and tar).

The file is first extracted to a .part file that is renamed when the extraction is completed.

Parameters
  • src_path (Union[str, Path]) – source file path

  • member (Optional[str]) – must be provided if src_path points to an archive that contains multiple files;

  • dest_dir (Union[str, Path]) – destination directory; by default, it’s the current working directory

  • dest_filename (Optional[str]) – destination filename; by default, it’s equal to member (if provided)

  • overwrite (bool) – overwrite existing file at dest_path if it already exists

Return type

Path

Returns

Path – the path to the extracted file

embfile.compression.extract_if_missing(src_path, member=None, dest_dir='.', dest_filename=None)[source]

Extracts a file unless it already exists and returns its path.

Note: during extraction, a .part file is used, so there’s no risk of using a partially extracted file.

Parameters
Return type

Path

Returns

The path of the decompressed file is returned.

embfile.compression.EXTENSION_TO_COMPRESSION = {'.bz2': 'bz2', '.gz': 'gz', '.gzip': 'gz', '.lzma': 'xz', '.xz': 'xz', '.zip': 'zip'}

Maps a compression extensions to the corresponding compression format name

embfile.compression.COMPRESSION_TO_EXTENSIONS = {'bz2': ['.bz2'], 'gz': ['.gz', '.gzip'], 'xz': ['.xz', '.lzma'], 'zip': ['.zip']}

Maps each compression format to its associated extensions