- Notifications
You must be signed in to change notification settings - Fork74
7zip in python3 with ZStandard, PPMd, LZMA2, LZMA1, Delta, BCJ, BZip2, and Deflate compressions, and AES encryption.
License
miurahr/py7zr
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
py7zr is a library and utility to support 7zip archive compression, decompression,encryption and decryption written by Python programming language.
There is incompatible change between v1.0.0-rc1 to v1.0.0-rc2.I abandon methodsread
andreadall
.When you want to extract the archive to memory, you need to useextract
method withfactory
argument. Thefactory
takes a factory class, as of the GoF design pattern,that provides object implementsPy7zIO
interface as a call back instance.SevenZipFile
extract and write uncompressed data into yourPy7zIO
object.
You are welcome to join discussions on project forum/builtin-board athttps://github.com/miurahr/py7zr/discussions
You can see announcements of new releases, questions and answers, andnew feature ideas. When you doubt for usage of py7zr library with unclearmanuals, please feel easy to raise question on forum.
py7zr
supports algorithms and filters whichlzma module andliblzma 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.
- 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 doesnot guarantee to block all the bad cases.- ZStandard and Brotli is not default methods of 7-zip, so these archives are considerednot 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 ofPKWARE, Inc.
- Enhanced Deflate is tested only on CPython. It is disabled on PyPy.
- BCJ2 (Standardlzma module does not provide).
You can install py7zr as usual other libraries using pip.
$ pip install py7zr
OR, alternatively using conda:
$ conda install -c conda-forge py7zr
- User Guide for latest version.
- API Guide for latest version.
- Manual for stable version.
- Contribution guidelines for this project.
- Contribution guidelines(html) for this project.
- Code of conduct for this project.
- Code of conduct(html) for this project.
- 7z file specification that py7zr stand on.
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
py7zr is a library which can use in your python application.
Here is a code snippet how to decompress some file in your application.
importpy7zrarchive=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).
importpy7zrwithpy7zr.SevenZipFile('sample.7z',mode='r')asz:z.extractall()withpy7zr.SevenZipFile('target.7z','w')asz: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.
importpy7zrimportrefilter_pattern=re.compile(r'<your/target/file_and_directories/regex/expression>')withpy7zr.SevenZipFile('archive.7z','r')asarchive:allfiles=archive.getnames()selective_files= [fforfinallfilesiffilter_pattern.match(f)]archive.extract(targets=selective_files)
py7zr support an extraction of password protected archive.(v0.6 and later)
importpy7zrwithpy7zr.SevenZipFile('encrypted.7z',mode='r',password='secret')asz:z.extractall()
Here is a code snippet how to produce archive.
importpy7zrwithpy7zr.SevenZipFile('target.7z','w')asarchive:archive.writeall('/path/to/base_dir','base')
To create encrypted archive, please pass a password.
importpy7zrwithpy7zr.SevenZipFile('target.7z','w',password='secret')asarchive:archive.writeall('/path/to/base_dir','base')
To create archive with algorithms such as zstandard, you can call with custom filter.
importpy7zrmy_filters= [{"id":py7zr.FILTER_ZSTD}]another_filters= [{"id":py7zr.FILTER_ARM}, {"id":py7zr.FILTER_LZMA2,"preset":7}]withpy7zr.SevenZipFile('target.7z','w',filters=my_filters)asarchive:archive.writeall('/path/to/base_dir','base')
py7zr also support shutil interface.
frompy7zrimportpack_7zarchive,unpack_7zarchiveimportshutil# register file format at first.shutil.register_archive_format('7zip',pack_7zarchive,description='7zip archive')shutil.register_unpack_format('7zip', ['.7z'],unpack_7zarchive)# extractionshutil.unpack_archive('test.7z','/tmp')# compressionshutil.make_archive('target','7zip','src')
py7zr uses a python3 standardlzma module for extraction and compression.The standard lzma module usesliblzma that support core compression algorithm of 7zip.
Minimum required version is Python 3.9.
py7zr
tested on Linux, macOS, Windows and Ubuntu aarch64.
It hopefully works on M1 Mac too.
Recommended versions are:
- CPython 3.9.0 and later.
- PyPy3.9-7.3.8 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
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 |
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 than7-zip
andp7zip
C/C++ implementation by several reasons.When compression/decompressionspeed is important, it is recommended to use thesealternatives throughsubprocess.run
python interface.
py7zr consumes some memory to decompress and compress data. It requires about 300MiB - 700MiB free memory to work well at least.
- PyTorch Open-source deep learning framework.
- 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
Please find aSecurity Policy of this project.
Version 0.20.0, 0.19.0, 0.18.10 or before has avulnerability 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
I really appreciate Mr. Matteo Cosentino for notification and corporation on security improvement.
- Copyright (C) 2019-2024 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/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULesser General Public License for more details.
You should have received a copy of the GNU Lesser General PublicLicense along with this library; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
About
7zip in python3 with ZStandard, PPMd, LZMA2, LZMA1, Delta, BCJ, BZip2, and Deflate compressions, and AES encryption.