7Zip Compression/Decompression Using Python 3 and py7zr

In this tutorial, you will learn how to use Python 3 and py7zr to perform 7Zip Compression/Decompression.

How to Install py7zr?

pip install py7zr

How to use py7zr in CLI?

You can run the below-mentioned py7zr commands according to your specific requirements.

List archive contents

py7zr l test.7z

Extract archive

py7zr x test.7z

Extract archive with password

py7zr x -P test.7z
password?: ****

Create and compress to archive

py7zr c target.7z test_dir

Create multi-volume archive

py7zr c -v 500k target.7z test_dir

Test archive

py7zr t test.7z

Append files to archive

py7zr a test.7z test_dir

Show information

py7zr i

Show version

py7zr --version

Decompression/Decryption

Here is a code snippet on how to decompress some files in your application.

<span class="kn">import</span> <span class="nn">py7zr</span>

<span class="n">archive</span> <span class="o">=</span> <span class="n">py7zr</span><span class="o">.</span><span class="n">SevenZipFile</span><span class="p">(</span><span class="s1">'sample.7z'</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">'r'</span><span class="p">)</span>
<span class="n">archive</span><span class="o">.</span><span class="n">extractall</span><span class="p">(</span><span class="n">path</span><span class="o">=</span><span class="s2">"/tmp"</span><span class="p">)</span>
<span class="n">archive</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

You can also use the ‘with’ block because py7zr provides a context manager (with version 0.6 and later).

<span class="kn">import</span> <span class="nn">py7zr</span>

<span class="k">with</span> <span class="n">py7zr</span><span class="o">.</span><span class="n">SevenZipFile</span><span class="p">(</span><span class="s1">'sample.7z'</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">'r'</span><span class="p">)</span> <span class="k">as</span> <span class="n">z</span><span class="p">:</span>
    <span class="n">z</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>

<span class="k">with</span> <span class="n">py7zr</span><span class="o">.</span><span class="n">SevenZipFile</span><span class="p">(</span><span class="s1">'target.7z'</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">z</span><span class="p">:</span>
    <span class="n">z</span><span class="o">.</span><span class="n">writeall</span><span class="p">(</span><span class="s1">'./base_dir'</span><span class="p">)</span>

py7zr also supports extraction of single or selected files by ‘extract(targets=[‘file path’])’.

Note:- If you specify only a file but not a parent directory, it will fail.

<span class="kn">import</span> <span class="nn">py7zr</span>
<span class="kn">import</span> <span class="nn">re</span>

<span class="n">filter_pattern</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">'<your/target/file_and_directories/regex/expression>'</span><span class="p">)</span>
<span class="k">with</span> <span class="n">SevenZipFile</span><span class="p">(</span><span class="s1">'archive.7z'</span><span class="p">,</span> <span class="s1">'r'</span><span class="p">)</span> <span class="k">as</span> <span class="n">archive</span><span class="p">:</span>
    <span class="n">allfiles</span> <span class="o">=</span> <span class="n">archive</span><span class="o">.</span><span class="n">getnames</span><span class="p">()</span>
    <span class="n">selective_files</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span> <span class="k">if</span> <span class="n">filter_pattern</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">allfiles</span><span class="p">]</span>
    <span class="n">archive</span><span class="o">.</span><span class="n">extract</span><span class="p">(</span><span class="n">targets</span><span class="o">=</span><span class="n">selective_files</span><span class="p">)</span>

py7zr supports the extraction of password-protected archives. (with version 0.6 and later)

<span class="kn">import</span> <span class="nn">py7zr</span>

<span class="k">with</span> <span class="n">py7zr</span><span class="o">.</span><span class="n">SevenZipFile</span><span class="p">(</span><span class="s1">'encrypted.7z'</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">'r'</span><span class="p">,</span> <span class="n">password</span><span class="o">=</span><span class="s1">'secret'</span><span class="p">)</span> <span class="k">as</span> <span class="n">z</span><span class="p">:</span>
    <span class="n">z</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>

Compression/Encryption

Here is a code snippet on how to produce an archive.

<span class="kn">import</span> <span class="nn">py7zr</span>

<span class="k">with</span> <span class="n">py7zr</span><span class="o">.</span><span class="n">SevenZipFile</span><span class="p">(</span><span class="s1">'target.7z'</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">archive</span><span class="p">:</span>
    <span class="n">archive</span><span class="o">.</span><span class="n">writeall</span><span class="p">(</span><span class="s1">'/path/to/base_dir'</span><span class="p">,</span> <span class="s1">'base'</span><span class="p">)</span>

To create an encrypted archive, please enter a password.

<span class="kn">import</span> <span class="nn">py7zr</span>

<span class="k">with</span> <span class="n">py7zr</span><span class="o">.</span><span class="n">SevenZipFile</span><span class="p">(</span><span class="s1">'target.7z'</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">,</span> <span class="n">password</span><span class="o">=</span><span class="s1">'secret'</span><span class="p">)</span> <span class="k">as</span> <span class="n">archive</span><span class="p">:</span>
    <span class="n">archive</span><span class="o">.</span><span class="n">writeall</span><span class="p">(</span><span class="s1">'/path/to/base_dir'</span><span class="p">,</span> <span class="s1">'base'</span><span class="p">)</span>

To create an archive with algorithms such as zstandard, you can call a custom filter.

<span class="kn">import</span> <span class="nn">py7zr</span>

<span class="n">my_filters</span> <span class="o">=</span> <span class="p">[{</span><span class="s2">"id"</span><span class="p">:</span> <span class="n">py7zr</span><span class="o">.</span><span class="n">FILTER_ZSTD</span><span class="p">}]</span>
<span class="n">another_filters</span> <span class="o">=</span> <span class="p">[{</span><span class="s2">"id"</span><span class="p">:</span> <span class="n">py7zr</span><span class="o">.</span><span class="n">FILTER_ARM</span><span class="p">},</span> <span class="p">{</span><span class="s2">"id"</span><span class="p">:</span> <span class="n">py7zr</span><span class="o">.</span><span class="n">FILTER_LZMA2</span><span class="p">,</span> <span class="s2">"preset"</span><span class="p">:</span> <span class="mi">7</span><span class="p">}]</span>
<span class="k">with</span> <span class="n">py7zr</span><span class="o">.</span><span class="n">SevenZipFile</span><span class="p">(</span><span class="s1">'target.7z'</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">,</span> <span class="n">filters</span><span class="o">=</span><span class="n">my_filter</span><span class="p">)</span> <span class="k">as</span> <span class="n">archive</span><span class="p">:</span>
    <span class="n">archive</span><span class="o">.</span><span class="n">writeall</span><span class="p">(</span><span class="s1">'/path/to/base_dir'</span><span class="p">,</span> <span class="s1">'base'</span><span class="p">)</span>

shutil helper

py7zr also supports the shutil interface.

<span class="kn">from</span> <span class="nn">py7zr</span> <span class="kn">import</span> <span class="n">pack_7zarchvie</span><span class="p">,</span> <span class="n">unpack_7zarchive</span>
<span class="kn">import</span> <span class="nn">shutil</span>

<span class="c1"># register file format at first.</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">register_archive_format</span><span class="p">(</span><span class="s1">'7zip'</span><span class="p">,</span> <span class="n">pack_7zarchive</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="s1">'7zip archive'</span><span class="p">)</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">register_unpack_format</span><span class="p">(</span><span class="s1">'7zip'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'.7z'</span><span class="p">],</span> <span class="n">unpack_7zarchive</span><span class="p">)</span>

<span class="c1"># extraction</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">unpack_archive</span><span class="p">(</span><span class="s1">'test.7z'</span><span class="p">,</span> <span class="s1">'/tmp'</span><span class="p">)</span>

<span class="c1"># compression</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">make_archive</span><span class="p">(</span><span class="s1">'target'</span><span class="p">,</span> <span class="s1">'7zip'</span><span class="p">,</span> <span class="s1">'src'</span><span class="p">)</span>

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.