Python3:lzma解压缩.7z文件 [英] Python3: lzma unpack .7z file

查看:476
本文介绍了Python3:lzma解压缩.7z文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解压缩.7z文件。根据这个问题,我可以使用
lzma 包即可。

I would like to unpack a .7z file. According to this question I can use the lzma package to do this.

我期待的是类似

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:解码器不支持输入格式。如何检查格式?而且我感到很惊讶,因为我认为7zip和.7z格式都是开源的,而python应该支持所有内容。

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.

我看到了很多人们只是在打电话的答案具有子进程的7zip可执行文件,但这不是我想要的。我正在寻找简单的python3解决方案。

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和7z是两种非常不同的野兽。

LZMA and 7z are two very different beasts.

最简单的说法是LZMA是无损压缩算法。这意味着,您向LZMA提供一些数据,它将进行压缩并提供输出。它没有文件,文件夹或如何存储它们的感觉。

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是存档文件格式,这意味着7z是一个完整的软件包。您有一些文件和文件夹,将其提供给7z,它将整齐地压缩它们,并将其存储在单个文件中(存档)。请注意,7z使用LZMA和其他算法组合来压缩文件并将其存储在其7z存档文件中。

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.

这是Wikipedia关于这两者的内容:

Here is what wikipedia has got to say about the two:


7z 是一种压缩的存档文件格式,支持几种不同的数据压缩,加密和预处理算法。

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

Lempel-Ziv-Markov链算法( LZMA )是一种用于执行无损数据压缩的算法。自1996年或1998年以来,它一直在开发中。 3 ,它最初以7z格式用于7-Zip存档器。

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.

因此,简而言之,您不能使用lzma创建或提取7z文件。据我所知,除了使用python之外,没有其他方法可以提取7z文件:请参见下面的更新。

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



更新:2019年5月



由于有人对在python中提取 7z 文件感兴趣,所以我认为需要进行更新。从2019年开始(也许甚至更早),用于Python的 libarchive绑定确实支持 7z 格式。上面的链接中提供了一个从7z存档中提取文件的示例。

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.

这篇关于Python3:lzma解压缩.7z文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆