archive

Support for reading and writing tar and zip archives.

Module Contents

Classes

E3ZipInfo

ZipInfo subclass for cross-platform compatibility.

E3ZipFile

Override default ZipFile with attributes preservation.

Functions

is_known_archive_format(→ bool)

Check if a given path is a supported archive format.

check_type(→ TAR_GZ | TAR_BZ2 | TAR_XZ | TAR | ZIP)

Return the archive extension.

unpack_archive(→ None)

Unpack an archive file (.tgz, .tar.gz, .tar or .zip).

create_archive(→ None)

Create an archive file (.tgz, .tar.gz, .tar or .zip).

Attributes

UnpackAutoRemoveDirType

logger

TAR_READ_MODES

archive.UnpackAutoRemoveDirType
archive.logger
class archive.E3ZipInfo(filename='NoName', date_time=(1980, 1, 1, 0, 0, 0))

Bases: zipfile.ZipInfo

ZipInfo subclass for cross-platform compatibility.

classmethod from_file(*args: Any, **kwargs: Any) E3ZipInfo

Create a ZipInfo from a file with adjusted executable permissions.

Sets execute permissions (0o555) in external_attr for the file.

class archive.E3ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True, metadata_encoding=None)

Bases: zipfile.ZipFile

Override default ZipFile with attributes preservation.

_extract_member(member: str | zipfile.ZipInfo, path: str | os.PathLike[str] | None, pwd: bytes | None) str

Extract a member from the archive with attribute preservation.

Parameters:
  • member – member name or ZipInfo object

  • path – directory to extract to

  • pwd – password for encrypted files

exception archive.ArchiveError(message: str | list[str], origin: str | None = None)

Bases: e3.error.E3Error

Exception raised for archive operations errors.

archive.is_known_archive_format(filename: str | pathlib.Path) bool

Check if a given path is a supported archive format.

Parameters:

filename – path

Returns:

True if the path corresponding to a supported archive format

archive.check_type(filename: str, force_extension: str | None = None) TAR_GZ | TAR_BZ2 | TAR_XZ | TAR | ZIP

Return the archive extension.

Internal function used by create_archive and unpack_archive.

Parameters:
  • filename – the name of the archive to extract the extension

  • force_extension – specify the archive extension if not in the filename

Returns:

the file extension

archive.TAR_READ_MODES: dict[str, Literal[r:, r:bz2, r:gz, r:xz]]
archive.unpack_archive(filename: str | pathlib.Path, dest: str | pathlib.Path, fileobj: IO[bytes] | None = None, selected_files: collections.abc.Sequence[str] | None = None, remove_root_dir: RemoveRootDirType = False, unpack_cmd: collections.abc.Callable[Ellipsis, None] | None = None, force_extension: str | None = None, delete: bool = False, ignore: list[str] | None = None, preserve_timestamps: bool = True, tmp_dir_root: str | pathlib.Path | None = None) None

Unpack an archive file (.tgz, .tar.gz, .tar or .zip).

Parameters:
  • filename – archive to unpack

  • dest – destination directory (should exist)

  • fileobj – if specified, the archive is read from this file object instead of opening a file. The file object must be opened in binary mode. In this case filename is the name of the archive contained in the file object.

  • selected_files – list of files to unpack (partial extraction). If None all files are unpacked

  • remove_root_dir – if True then the root dir of the archive is suppressed. if set to ‘auto’ then the root dir of the archive is suppressed only if it is possible. If not do not raise an exception in that case and fallback on the other method.

  • unpack_cmd – command to run to unpack the archive, if None use default methods or raise ArchiveError if archive format is not supported. If unpack_cmd is not None, then remove_root_dir is ignored. The unpack_cmd must raise ArchiveError in case of failure.

  • force_extension – specify the archive extension if not in the filename. If filename has no extension and force_extension is None unpack_archive will fail.

  • delete – if True and remove_root_dir is also True, remove files from dest if they do not exist in the archive

  • ignore – a list of files/folders to keep when synchronizing with the final destination directory.

  • preserve_timestamps – if False and remove_root_dir is True, and the target directory exists, ensure that updated files get their timestamp updated to current time.

  • tmp_dir_root – If not None the temporary directory used to extract the archive will be created in tmp_dir_root directory. If None the temporary directory is created in the destination directory. This argument only has an effect when remove_root_dir is True.

Raises:

ArchiveError – in case of error

cygpath (win32) utilities might be needed when using remove_root_dir option

archive.create_archive(filename: str, from_dir: str | pathlib.Path, dest: str | pathlib.Path | None = None, fileobj: IO[bytes] | None = None, force_extension: str | None = None, from_dir_rename: str | None = None, no_root_dir: bool = False) None

Create an archive file (.tgz, .tar.gz, .tar or .zip).

The Python implementations (tarfile and zipfile) are used on all platforms. Spawning tar, gzip or zip on Linux could be faster, however it has the disadvantage of not using the same implementation across platforms, hence the choice to only use the Python implementations.

Parameters:
  • filename – archive to create

  • from_dir – directory to pack (full path)

  • dest – destination directory (should exist). If not specified, the archive is written to the file object passed with fileobj.

  • fileobj – if specified, the archive is written to this file object instead of opening a file. The file object must be opened in binary mode. In this case filename is the name of the archive contained in the file object.

  • force_extension – specify the archive extension if not in the filename. If filename has no extension and force_extension is None create_archive will fail.

  • from_dir_rename – name of root directory in the archive.

  • no_root_dir – create archive without the root dir (zip only)

Raises:
  • ValueError – neither dest nor fileobj is provided

  • ArchiveError – if an error occurs