python解压7z文件,Python3:lzma解压缩.7z文件

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.

解决方案

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' )

Update: May 2019

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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值