From edb087abde1dcc5fecbb0ee6581cb4dbe9e409ce Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Fri, 6 Oct 2023 20:09:05 -0700 Subject: [PATCH] Handle errors when reading zip comments fixes #548 --- comicapi/archivers/zip.py | 11 ++++++++++- setup.cfg | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/comicapi/archivers/zip.py b/comicapi/archivers/zip.py index 35d59b2..5ec3ba2 100644 --- a/comicapi/archivers/zip.py +++ b/comicapi/archivers/zip.py @@ -9,6 +9,8 @@ import tempfile import zipfile from typing import cast +import chardet + from comicapi.archivers import Archiver logger = logging.getLogger(__name__) @@ -23,7 +25,14 @@ class ZipArchiver(Archiver): def get_comment(self) -> str: with zipfile.ZipFile(self.path, "r") as zf: - comment = zf.comment.decode("utf-8") + encoding = chardet.detect(zf.comment, True) + if encoding["confidence"] > 60: + try: + comment = zf.comment.decode(encoding["encoding"]) + except UnicodeDecodeError: + comment = zf.comment.decode("utf-8", errors="replace") + else: + comment = zf.comment.decode("utf-8", errors="replace") return comment def set_comment(self, comment: str) -> bool: diff --git a/setup.cfg b/setup.cfg index 371ea37..6f2d110 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,6 +36,7 @@ packages = find: install_requires = appdirs==1.4.4 beautifulsoup4>=4.1 + chardet importlib-metadata>=3.3.0 natsort>=8.1.0 pathvalidate