rcl7z: use py7zr if available, rather than pylzma, which does not work on some archives

This commit is contained in:
Jean-Francois Dockes 2020-12-25 12:30:18 +01:00
parent 3479e7cd85
commit 53edd7b213

View File

@ -4,6 +4,10 @@
# Thanks to Recoll user Martin Ziegler # Thanks to Recoll user Martin Ziegler
# This is a modified version of rclzip, with some help from rcltar # This is a modified version of rclzip, with some help from rcltar
#
# Normally using py7zr https://github.com/miurahr/py7zr
#
# Else, but it does not work on all archives, may use:
# Python pylzma library required. See http://www.joachim-bauch.de/projects/pylzma/ # Python pylzma library required. See http://www.joachim-bauch.de/projects/pylzma/
import sys import sys
@ -11,10 +15,15 @@ import os
import fnmatch import fnmatch
import rclexecm import rclexecm
usingpy7zr = False
try:
from py7zr import SevenZipFile as Archive7z
usingpy7zr = True
except:
try: try:
from py7zlib import Archive7z from py7zlib import Archive7z
except: except:
print("RECFILTERROR HELPERNOTFOUND python3:pylzma") print("RECFILTERROR HELPERNOTFOUND python3:py7zr or python3:pylzma")
sys.exit(1); sys.exit(1);
try: try:
@ -41,14 +50,18 @@ class SevenZipExtractor:
def extractone(self, ipath): def extractone(self, ipath):
#self.em.rclog("extractone: [%s]" % ipath) #self.em.rclog("extractone: [%s]" % ipath)
docdata = b'' docdata = b''
ok = False
try: try:
if usingpy7zr:
docdata = self.sevenzdic[ipath].read()
else:
docdata = self.sevenzip.getmember(ipath).read() docdata = self.sevenzip.getmember(ipath).read()
ok = True ok = True
except Exception as err: except Exception as err:
self.em.rclog("extractone: failed: [%s]" % err) self.em.rclog("extractone: failed: [%s]" % err)
ok = False
iseof = rclexecm.RclExecM.noteof iseof = rclexecm.RclExecM.noteof
if self.currentindex >= len(self.sevenzip.getnames()) -1: if self.currentindex >= len(self.names) -1:
iseof = rclexecm.RclExecM.eofnext iseof = rclexecm.RclExecM.eofnext
return (ok, docdata, rclexecm.makebytes(ipath), iseof) return (ok, docdata, rclexecm.makebytes(ipath), iseof)
@ -72,6 +85,11 @@ class SevenZipExtractor:
try: try:
self.fp = open(filename, 'rb') self.fp = open(filename, 'rb')
self.sevenzip = Archive7z(self.fp) self.sevenzip = Archive7z(self.fp)
if usingpy7zr:
self.sevenzdic = self.sevenzip.readall()
self.names = [k[0] for k in self.sevenzdic.items()]
else:
self.names = self.sevenzip.getnames()
return True return True
except Exception as err: except Exception as err:
self.em.rclog("openfile: failed: [%s]" % err) self.em.rclog("openfile: failed: [%s]" % err)
@ -94,23 +112,23 @@ class SevenZipExtractor:
# Return "self" doc # Return "self" doc
self.currentindex = 0 self.currentindex = 0
self.em.setmimetype('text/plain') self.em.setmimetype('text/plain')
if len(self.sevenzip.getnames()) == 0: if len(self.names) == 0:
self.closefile() self.closefile()
eof = rclexecm.RclExecM.eofnext eof = rclexecm.RclExecM.eofnext
else: else:
eof = rclexecm.RclExecM.noteof eof = rclexecm.RclExecM.noteof
return (True, "", "", eof) return (True, "", "", eof)
if self.currentindex >= len(self.sevenzip.getnames()): if self.currentindex >= len(self.names):
#self.em.rclog("getnext: EOF hit") #self.em.rclog("getnext: EOF hit")
self.closefile() self.closefile()
return (False, "", "", rclexecm.RclExecM.eofnow) return (False, "", "", rclexecm.RclExecM.eofnow)
else:
entryname = self.sevenzip.getnames()[self.currentindex] entryname = self.names[self.currentindex]
if hasrclconfig and len(self.skiplist) != 0: if hasrclconfig and len(self.skiplist) != 0:
while self.currentindex < len(self.sevenzip.getnames()): while self.currentindex < len(self.names):
entryname = self.sevenzip.getnames()[self.currentindex] entryname = self.names[self.currentindex]
for pat in self.skiplist: for pat in self.skiplist:
if fnmatch.fnmatch(entryname, pat): if fnmatch.fnmatch(entryname, pat):
entryname = None entryname = None