rclrar: also support py-unrar

This commit is contained in:
Jean-Francois Dockes 2019-02-17 17:12:50 +01:00
parent a1da4ecbca
commit afe4d92183

View File

@ -22,10 +22,23 @@ from __future__ import print_function
import sys import sys
import rclexecm import rclexecm
import os
# We can use two different unrar python modules. Either python-rarfile
# which is a wrapper over the the unrar command line, or python-unrar
# which is a ctypes wrapper of the unrar lib. Python-rarfile is the
# one commonly packaged on linux. Their interfaces is similar. Note
# that python-unrar uses forward slashes in internal file paths while
# python-rarfile uses backslashes (ipaths are opaque anyway).
using_unrar = False
try:
from unrar import rarfile
using_unrar = True
except Exception as ex:
try: try:
from rarfile import RarFile from rarfile import RarFile
except: except:
print("RECFILTERROR HELPERNOTFOUND python3:rarfile") print("RECFILTERROR HELPERNOTFOUND python3:rarfile/python3:unrar")
sys.exit(1); sys.exit(1);
# Requires RarFile python module. Try "sudo pip install rarfile" or # Requires RarFile python module. Try "sudo pip install rarfile" or
@ -49,10 +62,14 @@ class RarExtractor:
try: try:
rarinfo = self.rar.getinfo(ipath) rarinfo = self.rar.getinfo(ipath)
if using_unrar:
# dll.hpp RHDF_DIRECTORY: 0x20
isdir = ((rarinfo.flag_bits & 0x20) != 0)
else:
isdir = rarinfo.isdir() isdir = rarinfo.isdir()
except Exception as err: except Exception as err:
self.em.rclog("extractone: rar.getinfo failed: [%s]" % err) self.em.rclog("extractone: rar.getinfo failed: [%s]" % err)
return (True, docdata, ipath, false) return (True, docdata, ipath, False)
if not isdir: if not isdir:
try: try:
@ -80,6 +97,14 @@ class RarExtractor:
def openfile(self, params): def openfile(self, params):
self.currentindex = -1 self.currentindex = -1
try: try:
if using_unrar:
# There might be a way to avoid the decoding which is
# wrong on Unix, but I'd have to dig further in the
# lib than I wish to. This is used on Windows anyway,
# where all Recoll paths are utf-8
fn = params["filename:"].decode("UTF-8")
self.rar = rarfile.RarFile(fn, 'rb')
else:
# The previous versions passed the file name to # The previous versions passed the file name to
# RarFile. But the py3 version of this wants an str as # RarFile. But the py3 version of this wants an str as
# input, which is wrong of course, as filenames are # input, which is wrong of course, as filenames are