Skip to content

Instantly share code, notes, and snippets.

@MMohan1
Last active December 26, 2017 10:51
Show Gist options
  • Save MMohan1/238c7edaa360ad5bf15b3a836ee98383 to your computer and use it in GitHub Desktop.
Save MMohan1/238c7edaa360ad5bf15b3a836ee98383 to your computer and use it in GitHub Desktop.
recursively Extract 7z files
import py7zlib
import os
import re
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()
def extract_nested_7z(zippedFile, toFolder):
""" Extract a 7z file including any nested 7z files
Delete the 7z file(s) after extraction
"""
if not os.path.exists(zippedFile):
return
SevenZFile(zippedFile).extractall(toFolder)
os.remove(zippedFile)
for root, dirs, files in os.walk(toFolder):
for filename in files:
if re.search(r'\.7z$', filename):
fileSpec = os.path.join(root, filename)
extract_nested_7z(fileSpec, root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment