seven_zip

pyhelpers.store.seven_zip(path_to_zip_file, out_dir=None, mode='aoa', verbose=False, seven_zip_exe=None, **kwargs)[source]

Extract data from a compressed file by using 7-Zip.

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

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

  • mode (str) – defaults to 'aoa'

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

  • seven_zip_exe (str | None) – absolute path to ‘7z.exe’, defaults to None; when seven_zip_exe=None, use the default installation path, e.g. (on Windows) “C:\Program Files\7-Zip\7z.exe

  • kwargs – [optional] parameters of subprocess.run

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 20.00 alpha (x64) : Copyright (c) 1999-2020 Igor Pavlov : 2020-02-06

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.

>>> 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")
>>> seven_zip(path_to_zip_file=zip_file_pathname, out_dir=output_dir, 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, out_dir=output_dir)

>>> 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([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.