seven_zip

pyhelpers.store.seven_zip(path_to_zip_file, output_dir=None, mode='aoa', ret_output_dir=False, verbose=False, raise_error=False, seven_zip_exe=None)[source]

Extracts data from a compressed file using 7-Zip.

Parameters:
  • path_to_zip_file (str | os.PathLike) – The path where the compressed file is saved.

  • output_dir (str | None) – The directory where the extracted data will be saved; defaults to None.

  • mode (str) – The extraction mode; defaults to 'aoa'.

  • ret_output_dir (bool) – Whether to return the path to output directory; defaults to False.

  • verbose (bool | int) – Whether to print relevant information to the console; defaults to False.

  • raise_error (bool) – Whether to raise the provided exception; if raise_error=False (default), the error will be suppressed.

  • seven_zip_exe (str | None) – The path to the executable “7z.exe”; If seven_zip_exe=None (default), the default installation path will be used, e.g. “C:\Program Files\7-Zip\7z.exe” (on Windows).

Examples:

>>> from pyhelpers.store import seven_zip
>>> from pyhelpers.dirs import cd, delete_dir
>>> zip_file_pathname = cd("tests", "data", "zipped.zip")
>>> seven_zip(path_to_zip_file=zip_file_pathname, verbose=True)

7-Zip 24.09 (x64) : Copyright (c) 1999-2024 Igor Pavlov : 2024-11-29

Scanning the drive for archives:
1 file, 158 bytes (1 KiB)

Extracting archive: .\tests\data\zipped.zip
--
Path = .\tests\data\zipped.zip
Type = zip
Physical Size = 158

Everything is Ok

Size:       4
Compressed: 158

Done.
>>> output_dir_1 = cd("tests", "data", "zipped")
>>> out_file_pathname = cd(output_dir_1, "zipped.txt")
>>> with open(out_file_pathname) as f:
...     print(f.read())
test
>>> output_dir_2 = cd("tests", "data", "zipped_alt")
>>> seven_zip(path_to_zip_file=zip_file_pathname, output_dir=output_dir_2, verbose=False)
>>> out_file_pathname = cd("tests", "data", "zipped_alt", "zipped.txt")
>>> with open(out_file_pathname) as f:
...     print(f.read())
test
>>> # Extract a .7z file
>>> zip_file_path = cd("tests", "data", "zipped.7z")
>>> seven_zip(path_to_zip_file=zip_file_path, output_dir=output_dir_2)
>>> out_file_pathname = cd("tests", "data", "zipped", "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([output_dir_1, output_dir_2], 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.