diff --git a/unrar/rarfile.py b/unrar/rarfile.py index 2cd132b..bdcfc3a 100644 --- a/unrar/rarfile.py +++ b/unrar/rarfile.py @@ -34,9 +34,17 @@ def b(x): return x else: def b(x): - if x is not None: - # encode using DOS OEM standard - return x.encode('cp437') + if isinstance(x, str): + try: + if platform.system() == 'Windows': + return x.encode('cp' + str(ctypes.windll.kernel32.GetACP())) + else: + return x.encode('utf-8') + except UnicodeEncodeError: + return x.encode('cp437') + else: + # assume None or bytes + return x class BadRarFile(Exception): @@ -128,6 +136,10 @@ def __init__(self, filename, mode='r', pwd=None): handle = self._open(archive) # assert(archive.OpenResult == constants.SUCCESS) + # On windows, unRAR use system codepage to convert to wide char + # On linux, assume utf-8 and convert to wide char + # Users also can pass bytes object directly if require + # TODO: handle password in callback self.pwd = pwd if self.pwd is not None: unrarlib.RARSetPassword(handle, b(self.pwd))