如何使用 PyLZMA 的示例 [英] Example of how to use PyLZMA
本文介绍了如何使用 PyLZMA 的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 PyLZMA 从存档(例如 test.7z)中提取文件并提取到同一个目录.
I want to use PyLZMA to extract a file from an archive (e.g. test.7z) and extract it to the same directory.
我是 Python 的新手,不知道如何开始.我做了一些谷歌搜索,发现 一些例子 和 docs,但我不明白它们是如何工作的.
I'm a newbie to Python and have no idea how to start. I've done some googling and found some examples and docs, but I don't understand how they work.
有人可以发布我想要做的基本代码,以便我可以开始工作和理解吗?
Could someone please post the basic code for what I want to do so that I can start to work and understand?
推荐答案
这是一个处理基本功能的 Python 类.我已经将它用于我自己的工作:
Here is a Python class to handle the basic functionality. I have used it for my own work:
import py7zlib
class SevenZFile(object):
@classmethod
def is_7zfile(cls, filepath):
'''
Class method: determine if file path points to a valid 7z archive.
'''
is7z = False
fp = None
try:
fp = open(filepath, 'rb')
archive = py7zlib.Archive7z(fp)
n = len(archive.getnames())
is7z = True
finally:
if fp:
fp.close()
return is7z
def __init__(self, filepath):
fp = open(filepath, 'rb')
self.archive = py7zlib.Archive7z(fp)
def extractall(self, path):
for name in self.archive.getnames():
outfilename = os.path.join(path, name)
outdir = os.path.dirname(outfilename)
if not os.path.exists(outdir):
os.makedirs(outdir)
outfile = open(outfilename, 'wb')
outfile.write(self.archive.getmember(name).read())
outfile.close()
这篇关于如何使用 PyLZMA 的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文