gws.lib.zipx

Zipfile wrappers.

Source code: gws.lib.zipx

Package Contents

exception gws.lib.zipx.Error

Bases: gws.Error

Generic GWS error.

gws.lib.zipx.unzip_bytes(source: bytes, target_dir: str, flat: bool = False) int

Unpack a zip archive in memory into a directory.

Parameters:
  • source – Path to the zip archive.

  • target_dir – Path to the target directory.

  • flat – If True omit path and consider only base name of files in the zip archive, else complete paths are considered of files in the zip archive. Default is False.

Returns:

The number of unzipped files.

gws.lib.zipx.unzip_bytes_to_dict(source: bytes, flat: bool = False) dict[str, bytes]

Unpack a zip archive in memory into a dict.

Parameters:
  • source – Path to zip archive.

  • flat – If True then the result contains the base names of the unzipped files, else it contains the whole path. Default is False.

Returns:

A dictionary whose keys are the file paths or base names and values are the file contents.

gws.lib.zipx.unzip_path(path: str, target_dir: str, flat: bool = False) int

Unpack a zip archive into a directory.

Parameters:
  • path – Path to the zip archive.

  • target_dir – Path to the target directory.

  • flat – If True omit path and consider only base name of files in the zip archive, else complete paths are considered of files in the zip archive. Default is False.

Returns:

The number of unzipped files.

gws.lib.zipx.unzip_path_to_dict(path: str, flat: bool = False) dict[str, bytes]

Unpack a zip archive into a dict.

Parameters:
  • path – Path to the zip archive.

  • flat – If True then the result contains the base names of the unzipped files, else it contains the whole path. Default is False.

Returns:

A dictionary whose keys are the file paths or base names and values are the file contents.

gws.lib.zipx.zip_to_bytes(sources: list[str | dict], base_dir: str = '', flat: bool = False) bytes

Create a zip archive in memory.

Parameters:
  • sources – A list of paths or dicts to zip. If a dict is given, its keys are file names in the archive and its values are the file contents.

  • base_dir – If given, this path is stripped from the beginning of the file paths in the archive.

  • flat – If True only base names are being kept in archive.

Returns:

The zipped content as bytes.

gws.lib.zipx.zip_to_path(path: str, sources: list[str | dict], base_dir: str = '', flat: bool = False) int

Create a zip archive in a file.

Parameters:
  • path – Path to the archive.

  • sources – A list of paths or dicts to zip. If a dict is given, its keys are file names in the archive and its values are the file contents.

  • base_dir – If given, this path is stripped from the beginning of the file paths in the archive.

  • flat – If True only base names are being kept in archive.

Returns:

The amount of files in the archive.