hbp_nrp_commons.zip_util module

This module provides helper functions to handle zip

hbp_nrp_commons.zip_util.create_from_filelist(file_list: Iterable[Union[str, os.PathLike]], dest_zip_file: Union[str, os.PathLike], preserve_path: bool = True) → None[source]

Create a zip from file_list.

Parameters
  • file_list – the list of files to be compressed

  • dest_zip_file – file name and path to the zip archive

  • preserve_path – the file paths will be preseverd in the archive.

hbp_nrp_commons.zip_util.create_from_path(path: Union[str, os.PathLike], dest_zip_file: Union[str, os.PathLike]) → None[source]

Create a zip from a path

Parameters
  • path – path to be compressed

  • dest_zip_file – file name and path to the zip archive

hbp_nrp_commons.zip_util.extract_all(zip_target: Union[str, os.PathLike, IO[bytes]], extract_to: Union[str, os.PathLike], overwrite: bool = False, preserve_path: bool = True) → None[source]

Extract a zip in the given location

Parameters
  • zip_target – absolute path of the zip file or BytesIO object

  • extract_to – absolute path to the folder where to unzip

  • overwrite – boolean to indicate whether to overwrite the existing contents

  • preserve_path – preserve the archived file paths, flatten them otherwise

Returns

hbp_nrp_commons.zip_util.get_rootname(zip_abs_path: Union[str, os.PathLike, IO[bytes]]) → Optional[str][source]

Gets the root folder name inside a zip Note: for some zip, namelist doesn’t contain the folders as entries Possibly dependent upon how the zip was bundled, hence the extra check

Returns

root folder name inside a zip or None if not found