• Stars
    star
    444
  • Rank 97,624 (Top 2 %)
  • Language
    Python
  • License
    GNU Lesser Genera...
  • Created over 5 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

7zip in python3 with ZStandard, PPMd, LZMA2, LZMA1, Delta, BCJ, BZip2, and Deflate compressions, and AES encryption.

logo py7zr -- a 7z library on python

https://readthedocs.org/projects/py7zr/badge/?version=latest https://img.shields.io/pypi/dd/py7zr https://img.shields.io/conda/vn/conda-forge/py7zr https://dev.azure.com/miurahr/github/_apis/build/status/miurahr.py7zr?branchName=master https://coveralls.io/repos/github/miurahr/py7zr/badge.svg?branch=master https://img.shields.io/pypi/l/py7zr py7zr

py7zr is a library and utility to support 7zip archive compression, decompression, encryption and decryption written by Python programming language.

Security Notice

Version 0.20.0, 0.19.0, 0.18.10 or before has a vulnerability for path traversal attack. Details are on "CVE-2022-44900: path traversal vulnerability in py7zr" disclose article .

Affected versions are vulnerable to Directory Traversal due to insufficient checks in the 'py7zr.py' and 'helpers.py' files

You are recommend to update immediately to version 0.20.2 or later, 0.19.2 or 0.18.12

I realy appreciate Mr. Matteo Cosentino for notification and coorporation on security improvement.

Compression algorithms

py7zr supports algorithms and filters which lzma module and liblzma support, and supports BZip2 and Deflate that are implemented in python core libraries, It also supports ZStandard, Brotli and PPMd with third party libraries.

py7zr is also able to encrypt and decrypt data using 3rd party encryption library.

Supported algorithms

  • compress
    • LZMA2
    • LZMA
    • Bzip2
    • Deflate
    • Copy
    • ZStandard
    • Brotli
    • PPMd
    • Enhanced Deflate (Experimental)
  • crypt
    • 7zAES
  • Filters
    • Delta
    • BCJ(X86,ARMT,ARM,PPC,SPARC,IA64)

Note

  • A feature handling symbolic link is basically compatible with p7zip implementation, but not work with original 7-zip because the original does not implement the feature.
  • py7zr try checking symbolic links strictly and raise ValueError when bad link is requested, but it does not guarantee to block all the bad cases.
  • ZStandard and Brotli is not default methods of 7-zip, so these archives are considered not to be compatible with original 7-zip on windows/p7zip on linux/mac.
  • Enhanced Deflate is also known as DEFLATE64 TM that is a registered trademark of PKWARE, Inc.
  • Enhanced Deflate is tested only on CPython. It is disabled on PyPy.

Not supported algorithms

Install

You can install py7zr as usual other libraries using pip.

$ pip install py7zr

OR, alternatively using conda:

$ conda install -c conda-forge py7zr

Documents

User manuals

Developer guide

CLI Usage

You can run command script py7zr like as follows;

  • 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

SevenZipFile Class Usage

py7zr is a library which can use in your python application.

Decompression/Decryption

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

import py7zr

archive = py7zr.SevenZipFile('sample.7z', mode='r')
archive.extractall(path="/tmp")
archive.close()

You can also use 'with' block because py7zr provide context manager(v0.6 and later).

import py7zr

with py7zr.SevenZipFile('sample.7z', mode='r') as z:
    z.extractall()

with py7zr.SevenZipFile('target.7z', 'w') as z:
    z.writeall('./base_dir')

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.

import py7zr
import re

filter_pattern = re.compile(r'<your/target/file_and_directories/regex/expression>')
with py7zr.SevenZipFile('archive.7z', 'r') as archive:
    allfiles = archive.getnames()
    selective_files = [f for f in allfiles if filter_pattern.match(f)]
    archive.extract(targets=selective_files)

py7zr support an extraction of password protected archive.(v0.6 and later)

import py7zr

with py7zr.SevenZipFile('encrypted.7z', mode='r', password='secret') as z:
    z.extractall()

Compression/Encryption

Here is a code snippet how to produce archive.

import py7zr

with py7zr.SevenZipFile('target.7z', 'w') as archive:
    archive.writeall('/path/to/base_dir', 'base')

To create encrypted archive, please pass a password.

import py7zr

with py7zr.SevenZipFile('target.7z', 'w', password='secret') as archive:
    archive.writeall('/path/to/base_dir', 'base')

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

import py7zr

my_filters = [{"id": py7zr.FILTER_ZSTD}]
another_filters = [{"id": py7zr.FILTER_ARM}, {"id": py7zr.FILTER_LZMA2, "preset": 7}]
with py7zr.SevenZipFile('target.7z', 'w', filters=my_filters) as archive:
    archive.writeall('/path/to/base_dir', 'base')

shutil helper

py7zr also support shutil interface.

from py7zr import pack_7zarchive, unpack_7zarchive
import shutil

# register file format at first.
shutil.register_archive_format('7zip', pack_7zarchive, description='7zip archive')
shutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)

# extraction
shutil.unpack_archive('test.7z', '/tmp')

# compression
shutil.make_archive('target', '7zip', 'src')

Requirements

py7zr uses a python3 standard lzma module for extraction and compression. The standard lzma module uses liblzma that support core compression algorithm of 7zip.

Minimum required version is Python 3.7.

py7zr tested on Linux, macOS, Windows and Ubuntu aarch64.

It hopefully works on M1 Mac too.

Recommended versions are:

  • CPython 3.7.5, CPython 3.8.0 and later.
  • PyPy3.7-7.3.3 and later.

Following fixes are included in these versions, and it is not fixed on python3.6.

  • BPO-21872: LZMA library sometimes fails to decompress a file
  • PyPy3-3090: lzma.LZMADecomporessor.decompress does not respect max_length
  • PyPy3-3242: '_lzma_cffi' has no function named 'lzma_stream_encoder'

Following improvements are included in CPython 3.10

  • BPO-41486: Faster bz2/lzma/zlib via new output buffering

Dependencies

There are several dependencies to support algorithms and CLI expressions.

Package Purpose
PyCryptodomex 7zAES encryption
PyZstd ZStandard compression
PyPPMd PPMd compression
Brotli Brotli compression (CPython)
BrotliCFFI Brotli compression (PyPy)
inflate64 Enhanced deflate compression
pybcj BCJ filters
multivolumefile Multi-volume archive read/write
texttable CLI formatter

Performance

You can find a compression and decompression benchmark results at [Github issue](#297) and [wiki page](https://github.com/miurahr/py7zr/wiki/Benchmarks)

py7zr works well, but slower than 7-zip and p7zip C/C++ implementation by several reasons. When compression/decompression speed is important, it is recommended to use these alternatives through subprocess.run python interface.

py7zr consumes some memory to decompress and compress data. It requires about 300MiB - 700MiB free memory to work well at least.

Use Cases

  • aqtinstall Another (unofficial) Qt (aqt) CLI Installer on multi-platforms.
  • PreNLP Preprocessing Library for Natural Language Processing
  • mlox a tool for sorting and analyzing Morrowind plugin load order

License

  • Copyright (C) 2019-2022 Hiroshi Miura
  • pylzma Copyright (c) 2004-2015 by Joachim Bauch
  • 7-Zip Copyright (C) 1999-2010 Igor Pavlov
  • LZMA SDK Copyright (C) 1999-2010 Igor Pavlov

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

More Repositories

1

aqtinstall

aqt: Another (unofficial) Qt CLI Installer on multi-platforms
Python
903
star
2

pykakasi

Lightweight converter from Japanese Kana-kanji sentences into Kana-Roman.
Python
382
star
3

unihandecode

unihandecode is a transliteration library to convert all characters/words in Unicode into ASCII alphabet that aware with Language preference priorities
Python
70
star
4

cmake-qt-packaging-example

CMake example to build pacakges for Qt GUI cross platform application in several format.
CMake
37
star
5

omegat-textra-plugin

OmegaT plugin to use TexTra(R) powered by NICT
Java
27
star
6

cmake-optimize-architecture-flag

CMake module to optimize cflags for architecture extensions such as SSE, AVX
CMake
26
star
7

nevernote

New nixnote is cloned on miurahr/nixnote2 ... Nixnote (formaly nevernote) is imcomplete evernote OSS cilent. here is a development branch by Hiroshi and NOT a offical one. please see below.
Java
19
star
8

lua-nginx-osm

OpenStreetMap extension for Nginx Lua module
Lua
16
star
9

bluegnss4osm

The project was suspended. It support Bluetooth GNSS(GPS, GLONASS, Galireo, Beidu, QZSS) reciever on Android for OpenStreetMap mappers.Very aggressive fork of bluegps4droid project(git://git.code.sf.net/p/bluegps4droid/git) )
Java
9
star
10

install-linuxdeploy-action

github action to run linuxdeploy
TypeScript
8
star
11

omegat-round-theme

Round theme plugin for OmegaT
Java
8
star
12

cmake4gdal

CMake scripts for gdal developers, who can use modern IDEs such as CLion, VS, VSCode and XCode
CMake
8
star
13

pyppmd

pyppmd provides classes and functions for compressing and decompressing text data, using PPM (Prediction by partial matching) compression algorithm variation H and I.2. It provide an API similar to Python's zlib/bz2/lzma modules.
C
8
star
14

multivolume

multi volume file wrapper
Python
7
star
15

tmpotter

TMPotter - source and translation text aligner/TMX converter for Computer Aided Translation
Java
6
star
16

ppmd-cffi

ppmd-cffi provides classes and functions for compressing and decompressing text data, using PPM (Prediction by partial matching) compression algorithm variation H and I.2 by CFFI python/C binding.
C
5
star
17

omegat-stat

JavaScript
4
star
18

lightspot

Abandoned: Logitech spotlight smart bluetooth preesnter driver for Linux/*BSD
C
4
star
19

omegat-plugin-epwing

EPWING dictionray plugin for the open-source translation tool OmegaT.
3
star
20

sane-backends

epjitsu driver improvement to support additional parameters, to fix padding problems and to support new S1100 device.
C
3
star
21

Ushahidi_plugin_kml

Ushahidi's KML plugin
PHP
3
star
22

vgabios

C
3
star
23

pdic-toolkit

PDIC-Toolkit: Perl library for converting PDIC files
Perl
3
star
24

librasterlite2

(not maintained) mirror of librasterlite2 and topic branches to implement cmake build
C
2
star
25

qemu-kvm

development branch of qemu-kvm from git.kernel.org
C
2
star
26

omegat-for-cat-beginners

OmegaT tutorial for beginner
2
star
27

OpenBoard-AppImage

AppImage build and release of OpenBoard-org/OpenBoard
2
star
28

qgolftracker

qGolfTracker development branch to utilize OpenStreetMap: original http://gitorious.org/qgolftracker
C++
2
star
29

ModemManager

ModemManager hack for Japanese Modems
C
2
star
30

java-odapiv2-client

Java client library for Oxford Dictionaries API v2
Java
1
star
31

kde-qt-patch

1
star
32

map_features_converter

Java
1
star
33

keyring-export

Java
1
star
34

diff-java

Clone and improvements
Java
1
star
35

docker-hubot

hubot on docker
Shell
1
star
36

packer-rbenv

Packer recipe for rbenv base template
Shell
1
star
37

da-cha-drupal

PHP
1
star
38

seabios

fork of SeaBIOS project http://seabios.org
C
1
star
39

vagrant-calibre-ubuntu-package

Vagrantfile to compile Calibre 2.0 on Ubuntu Precise 12.04LTS on AWS
1
star
40

translate-django-ja

OmegaT translation project for django projects in Japanese
1
star
41

pybcj

BCJ(Branch-Call-Jump) filter for python
1
star
42

gnuhealth-vagrant-aws

Vagrant recipe for GNU Health working on AWS
Ruby
1
star
43

nginx-resty-ppa

Private Nginx debian package repositories - distributed as PPA at https://launchpad.net/~miurahr/+archive/openresty
C
1
star
44

omegat-adaptive-theme

DarkLaf theme plugin for OmegaT
Java
1
star
45

jlaw-tmx

Generation an OmegaT TMX file from Japanese Law Translation data from http://www.japaneselawtranslation.go.jp/
Ruby
1
star
46

docker-rbenv-base

Shell
1
star
47

gdal_docs_ja

GDAL documentation JA translation team project with OmegaT
1
star
48

omegat-onlinedictionary

Online dictionary query API for OmegaT CAT tool
Java
1
star
49

fgdconv

基盤地図情報(FGD: Fundermental Geographic Data) を変換するコンバータ このリポジトリではgit LFSを使っていますので、テストに必要なバイナリファイルをクローンしない場合は GIT_LFS_SKIP_SMUDGE=1 git cloneとしてください。
Python
1
star
50

bench-hash-performance

Code example of hashlib, memoryview, and ctypes
Python
1
star
51

libreoffice-build-ubuntu-vagrant

Provide libreoffice building environment using Vagrant
Shell
1
star
52

picast

https://codeberg.org/miurahr/picast
1
star
53

pdic-fpw

PDIC形式の辞書をFreePWINGを利用してJIS X 4081形式に変換するツール (fork)
Perl
1
star
54

cmake-define-find-package

Utility script to define Find* functionality without adding Find*.cmake in your project
CMake
1
star