103 lines
2.7 KiB
Python
103 lines
2.7 KiB
Python
import os
|
|
api = mfp.require("api")
|
|
prochelper = mfp.require("prochelper")
|
|
filehelper = mfp.require("filehelper")
|
|
|
|
def split7zLine(line):
|
|
arg = 0
|
|
args = []
|
|
argchars = ""
|
|
for c in line:
|
|
if arg == 5:
|
|
argchars = argchars + c
|
|
continue
|
|
if c in [" ","\t"]:
|
|
if argchars == "": continue
|
|
if arg == 0: # Date
|
|
if argchars.count("-") != 2: # Not a date, assume time is missing as well. Add both.
|
|
args.append("0000-00-00")
|
|
args.append("00:00:00")
|
|
arg += 2
|
|
|
|
args.append(argchars)
|
|
|
|
if arg == 2: # Attr
|
|
if argchars[0] == "D": # Is directory, insert 0 (compressed) size
|
|
args.append("0")
|
|
args.append("0")
|
|
arg += 2
|
|
|
|
argchars = ""
|
|
arg += 1
|
|
else:
|
|
argchars = argchars + c
|
|
|
|
if argchars != "":
|
|
args.append(argchars)
|
|
|
|
return args
|
|
|
|
def action_list(archiveFile,flags):
|
|
status = 0
|
|
for line in prochelper.pcallLines(["7z","l",archiveFile]):
|
|
if status == 0:
|
|
if line.lstrip(" \t").startswith("Date"):
|
|
status = 1
|
|
continue
|
|
|
|
if status == 1:
|
|
status = 2
|
|
continue
|
|
|
|
if status == 2:
|
|
if line.startswith("-"): return
|
|
lineSplit = split7zLine(line)
|
|
|
|
apiFile = api.getDefaultFile()
|
|
apiFile["isdir"] = lineSplit[2].startswith("D")
|
|
apiFile["date"] = list(map(int,lineSplit[0].split("-")))
|
|
apiFile["time"] = list(map(int,lineSplit[1].split(":")))
|
|
apiFile["size"] = int(lineSplit[3])
|
|
apiFile["sizeCompressed"] = int(lineSplit[4])
|
|
apiFile["name"] = lineSplit[5]
|
|
|
|
apiFile["ownerGroup"] = -1
|
|
apiFile["ownerUser"] = -1
|
|
apiFile["permissions"] = "---------"
|
|
|
|
yield apiFile
|
|
|
|
def action_extract(archiveFile,flags):
|
|
import shutil
|
|
if flags["if"] == "":
|
|
prochelper.pcall(["7z","x",archiveFile,"-y","-aoa","-o" + flags["of"]])
|
|
return
|
|
|
|
try:
|
|
tempdir = mfp.p(flags["of"],"7z." +str(os.getpid())+ ".tmp")
|
|
outdir = mfp.p(tempdir,mfp.pUp(flags["if"].replace("\\",os.path.sep).replace("/",os.path.sep)))
|
|
os.mkdir(tempdir)
|
|
prochelper.pcall(["7z","x",archiveFile,"-y","-aoa","-o" + tempdir,flags["if"]])
|
|
|
|
if os.path.isdir(outdir):
|
|
for root,dirs,files in os.walk(outdir):
|
|
for file in dirs:
|
|
ffile = mfp.p(root,file)
|
|
nfile = mfp.p(flags["of"],ffile.replace(outdir + os.path.sep,"",1))
|
|
if not os.path.isdir(nfile): os.mkdir(nfile)
|
|
|
|
for file in files:
|
|
ffile = mfp.p(root,file)
|
|
nfile = mfp.p(flags["of"],ffile.replace(outdir + os.path.sep,"",1))
|
|
if os.path.isfile(nfile): os.remove(nfile)
|
|
os.rename(ffile,nfile)
|
|
else:
|
|
nfile = mfp.p(flags["of"],outdir.replace(mfp.pUp(outdir) + os.path.sep,"",1))
|
|
if os.path.isfile(nfile): os.remove(nfile)
|
|
os.rename(outdir,nfile)
|
|
|
|
filehelper.rmtree(tempdir)
|
|
except:
|
|
filehelper.rmtree(tempdir)
|
|
raise
|