unzip

pyhelpers.store.unzip(path_to_zip_file, out_dir=None, verbose=False, **kwargs)[source]

Extract data from a zipped (compressed) file.

Parameters:
  • path_to_zip_file (str | os.PathLike) – path where a Zip file is saved

  • out_dir (str | None) – path to a directory where the extracted data is saved, defaults to None

  • verbose (bool | int) – whether to print relevant information in console, defaults to False

  • kwargs – [optional] parameters of zipfile.ZipFile.extractall

Examples:

>>> from pyhelpers.store import unzip
>>> from pyhelpers.dirs import cd, delete_dir

>>> zip_file_path = cd("tests\data", "zipped.zip")

>>> unzip(path_to_zip_file=zip_file_path, verbose=True)
Extracting "tests\data\zipped.zip" to "tests\data\zipped\" ... Done.
>>> out_file_pathname = cd("tests\data\zipped", "zipped.txt")
>>> with open(out_file_pathname) as f:
...     print(f.read())
test

>>> output_dir = cd("tests\data\zipped_alt")
>>> unzip(path_to_zip_file=zip_file_path, out_dir=output_dir, verbose=True)
Extracting "tests\data\zipped.zip" to "tests\data\zipped_alt\" ... Done.
>>> out_file_pathname = cd("tests\data\zipped_alt", "zipped.txt")
>>> with open(out_file_pathname) as f:
...     print(f.read())
test

>>> # Delete the directories "tests\data\zipped\" and "tests\data\zipped_alt\"
>>> delete_dir([cd("tests\data\zipped"), output_dir], verbose=True)
To delete the following directories:
    "tests\data\zipped\" (Not empty)
    "tests\data\zipped_alt\" (Not empty)
? [No]|Yes: yes
Deleting "tests\data\zipped\" ... Done.
Deleting "tests\data\zipped_alt\" ... Done.