13

I want to use PyLZMA to extract a file from an archive (e.g. test.7z) and extract it to the same directory.

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?

Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
Philipp Bammes
  • 525
  • 1
  • 6
  • 25
  • 4
    Could you show some examples of what you have tried and how it failed? – Levon May 22 '12 at 11:54
  • 1
    It seems this library is indeed completely undocumented, except some docstrings of the kind `class Base(object): """base oject"""`... – Fred Foo May 22 '12 at 11:58
  • 1
    People here usually frown upon "give me the code" questions, try to show some effort, show us what you found, tried and what you're missing and you'll get better support. – KurzedMetal May 22 '12 at 12:16
  • What I found: https://github.com/fancycode/pylzma/blob/master/doc/usage.txt the examples here: http://nullege.com/codes/search/pylzma.decompress i tried `f = Archive7z(open('test.7z', 'rb')) f.list()` which displays the archive's content but I dont't know how to tell python to extract it. (I'll keep searching for a solution on my own) – Philipp Bammes May 22 '12 at 12:48
  • When you have no idea where to start, I recommend you downloading the package sources and check if there is a "test' directory. It is often simple code that will help you :) –  Dec 22 '15 at 21:54

2 Answers2

15

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()
Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Brian B
  • 1,410
  • 1
  • 16
  • 30
  • 2
    Warning: Currently Archive7z will uncompress the entire contents into memory to read the files... so in my case a 10 MB file became 1.2 GB in memory. – liquidpele Feb 04 '20 at 20:19
3

Here are two code snippets i found here http://www.linuxplanet.org/blogs/?cat=3845

# Compress the input file (as a stream) to a file (as a stream)
i = open(source_file, 'rb')
o = open(compressed_file, 'wb')
i.seek(0)
s = pylzma.compressfile(i)
while True:
    tmp = s.read(1)
    if not tmp: break
    o.write(tmp)
o.close()
i.close()

# Decomrpess the file (as a stream) to a file (as a stream)
i = open(compressed_file, 'rb')
o = open(decompressed_file, 'wb')
s = pylzma.decompressobj()
while True:
    tmp = i.read(1)
    if not tmp: break
    o.write(s.decompress(tmp))
o.close()
i.close()
Fnord
  • 5,365
  • 4
  • 31
  • 48
  • 3
    Similar snippets are already in github.com/fancycode/pylzma/blob/master/doc/usage.txt ; also, `s.flush()` should be noted for the decompression case. (yet, somehow, googling leads here first, heh) – HoverHell Jul 31 '12 at 10:07
  • Since the OP wants to extract a file from an archive, I don't think this answers the question and find it astonishing that it was up-voted at all. – martineau Nov 22 '13 at 02:16
  • 2
    usage.txt --> usage.md @ https://github.com/fancycode/pylzma/blob/master/doc/USAGE.md – philshem Aug 14 '14 at 20:54
  • linuxplanet.org is an ex-parrot – rossmcm Mar 02 '21 at 03:54