Skip to content

Instantly share code, notes, and snippets.

@jbillo
Created March 13, 2014 01:23
Show Gist options
  • Save jbillo/9520211 to your computer and use it in GitHub Desktop.
Save jbillo/9520211 to your computer and use it in GitHub Desktop.
import py7zlib, os
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()
szfile = SevenZFile('MPlayer-p4-svn-34401.7z')
szfile.extractall('/tmp/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment