Python3 file: lzma unpack.7z

I would like to unzip the .7z file. According to this question, I can use lzma 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 that this is not happening. Also try something like

 import lzma #... with lzma.open('myFile.7z') as f: file_content = f.read() print(file_content) 

gave _lzma.LZMAError: Input format not supported by decoder . How to check the format? And I am very surprised because I thought that the 7zip and .7z format is open source, and python should support everything.

I saw a lot of answers when people simply called the 7zip executable using a subprocess, but I don't want to do this. I am looking for a simple python3 solution.

+11
source share
1 answer

LZMA and 7z are two completely different animals.

Simply put, LZMA is a lossless compression algorithm. This means that if you pass some data to LZMA, it will be compressed and provide you with output. It does not make sense in files, folders or how to store them.

7z is an archive file format , which means that 7z is a complete package. You have several files and folders, submit it to 7z, it will gently compress them and save it in one file (archive). Note that 7z uses LZMA and a set of other algorithms to compress and store files in its 7z archive file.

Here's what Wikipedia has to say about two:

7z is a compressed archive file format that supports several different compression, encryption, and data preprocessing algorithms.

The Lempel-Ziv-Markov chain algorithm ( LZMA ) is an algorithm used to compress data without loss. It was developed from 1996 or 1998 3 and was first used in the 7z 7-Zip archiver format.

In short, you cannot use lzma to create or extract 7z files. As far as I know, there is no way to extract the 7z file using python except: See the update below.

 import os os.system( '7z x archive.7z -oPath/to/Name' ) 

Update: May 2019

Since there is some interest in extracting 7z files in python, I thought the update was ok. Starting in 2019 (maybe even earlier), libarchive bindings for python support 7z format. An example of extracting files from the 7z archive is given in the link above.

+16
source share

All Articles