seven_zip

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

Extract data from a compressed file using 7-Zip.

This function extracts contents from supported compressed archives using the 7-Zip executable. If no output directory is specified, an extraction directory named after the archive is generated automatically.

Parameters:
  • zip_file_path (str | pathlib.Path | os.PathLike) – Path where the compressed file is saved.

  • output_dir (str | pathlib.Path | None) – Directory where extracted data will be saved. Defaults to None.

  • mode (str) – Extraction mode flag passed to 7-Zip. Defaults to 'aoa'.

  • return_output_dir (bool) – Whether to return the path to the 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 an exception on failure. Defaults to False.

  • seven_zip_exe (str | None) – Path to the 7-Zip executable (e.g. “7z.exe”). Defaults to None.

  • seven_zip_exe – 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_path = cd("tests", "data", "dat.zip")
>>> seven_zip(zip_file_path, verbose=True)
7-Zip 26.01 (x64) : Copyright (c) 1999-2026 Igor Pavlov : 2026-04-27
Scanning the drive for archives:
1 file, 158 bytes (1 KiB)
Extracting archive: .\tests\data\dat.zip
--
Path = .\tests\data\dat.zip
Type = zip
Physical Size = 158
Everything is Ok
Size:       4
Compressed: 158
Done.

>>> output_dir_1 = cd("tests/data", "dat")
>>> out_file_path = cd(output_dir_1, "zipped.txt")
>>> with open(out_file_path) as f:
...     print(f.read())
test

>>> output_dir_2 = cd("tests", "data", "dat_alt")
>>> seven_zip(zip_file_path, output_dir=output_dir_2, verbose=True)
>>> out_file_path = cd("tests/data/dat_alt", "zipped.txt")
>>> with open(out_file_path) as f:
...     print(f.read())
test

>>> # Extract a .7z file
>>> zip_file_path = cd("tests", "data", "dat.7z")
>>> seven_zip(zip_file_path, output_dir=output_dir_2)
>>> out_file_path = cd("tests/data/dat", "zipped.txt")
>>> with open(out_file_path) as f:
...     print(f.read())
test

>>> # Delete the directories "tests/data/dat/" and "tests/data/dat_alt/"
>>> delete_dir([output_dir_1, output_dir_2], verbose=True)
Confirm deletion of the following directories:
  "tests/data/dat/" (Not empty)
  "tests/data/dat_alt/" (Not empty)
? [No]|Yes: yes
Deleting:
  "tests/data/dat/" ... Done.
  "tests/data/dat_alt/" ... Done.