I would like to unpack a .7z file. According to this question I can use the lzma package to do this.
I was expecting something like
import lzma
#...
with lzma.open('myFile.7z') as f:
f.extractall('.')
To extract the file into the current directory but it seems something like this does not exist. Furthermore trying something like
import lzma
#...
with lzma.open('myFile.7z') as f:
file_content = f.read()
print(file_content)
did yield _lzma.LZMAError: Input format not supported by decoder
. How can I check the format? And I am quite surprised because I thought both 7zip and the .7z format are open source and python should support everything.
I saw a lot of answers where people were just calling the 7zip executable with a subprocess but this is not want I want to do. I am looking for a plain python3 solution.
Press Ctrl + A to select all of the files and folders in the 7z file. Click the button that says “1-click Unzip”. Select “Unzip to PC or Cloud,” then select a destination folder. The default option is to create a new folder with the name of the 7z file, which should suffice.
New in version 3.3. Source code: Lib/lzma.py. This module provides classes and convenience functions for compressing and decompressing data using the LZMA compression algorithm.
Both zip and 7z are lossless compression formats. 7z is newer and give you a better compression ratio but it's not as widely supported as . zip.
LZMA and 7z are two very different beasts.
In the simplest of terms LZMA is a lossless compression algorithm. This means that, you feed LZMA some data, it will compress and give you the output. It has no sense of files, folders or how to store them.
7z on the other hand is an archive file format, and this means that 7z is a complete package. You have a few files and folders, feed it to 7z, it will neatly compress them, and store them in a single file (archive). Please note that, 7z uses LZMA and a cocktail of other algorithms to compress and store files in its 7z archive file.
Here is what wikipedia has got to say about the two:
7z is a compressed archive file format that supports several different data compression, encryption and pre-processing algorithms.
The Lempel–Ziv–Markov chain algorithm (LZMA) is an algorithm used to perform lossless data compression. It has been under development either since 1996 or 19983 and was first used in the 7z format of the 7-Zip archiver.
So in short, you cannot use lzma to create or extract 7z files. As far as I know, there is no way to extract a 7z file using python other than: See update below.
import os
os.system( '7z x archive.7z -oPath/to/Name' )
Since there some interest about extracting 7z
files in python, I thought an update is in order. As of 2019 (perhaps even earlier), libarchive bindings for python do support 7z
format. An example for extracting files from 7z archive is given in above link.
You can try using a python library instead, py7zr, which supports 7zip archive compression, decompression, encryption, decryption. https://github.com/miurahr/py7zr
import py7zr
with py7zr.SevenZipFile('sample.7z', mode='r') as z:
z.extractall()
Credits for this solution go to Matt Dnv in this answer:
I was not able to make the given answers work. Most solutions for unpacking a 7z file in python used 7zip for which I was not able to find a reliable portable version that I could curl.
Hence here is a solution to unpack a .7z
file in a python 3.6 environment in Anaconda prompt 4.8.2. First I had to install 2 packages:
pip install pyunpack
pip install patool
Next I was able to unpack a 7zip filed named test.7z
which was located in the same directory as this example.py
script, to a folder named: output
(also located in the same directory as this example.py
script). The code to unpack the test.7z
file can be:
from pyunpack import Archive
Archive('test.7z').extractall('./output')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With