Last active
December 13, 2024 02:46
-
-
Save MMohan1/238c7edaa360ad5bf15b3a836ee98383 to your computer and use it in GitHub Desktop.
recursively Extract 7z files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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