77import hashlib
88import math
99import os
10+ import tarfile
1011import xml .etree .ElementTree as ET
1112import zipfile
1213from copy import deepcopy
1314from io import BytesIO
1415from pathlib import Path
15- from typing import TYPE_CHECKING , cast
16+ from typing import TYPE_CHECKING , Literal , cast
1617from warnings import catch_warnings
1718from xml .etree .ElementTree import Element
1819
9293 logger .exception ('[ThumbRenderer] Could not import the "pillow_jxl" module' )
9394
9495
96+ class _TarFile (tarfile .TarFile ):
97+ """Wrapper around tarfile.TarFile to mimic zipfile.ZipFile's API."""
98+
99+ def __init__ (self , filepath : Path , mode : Literal ["r" ]) -> None :
100+ super ().__init__ (filepath , mode )
101+
102+ def namelist (self ) -> list [str ]:
103+ return self .getnames ()
104+
105+ def read (self , name : str ) -> bytes :
106+ return self .extractfile (name ).read ()
107+
108+
109+ type _Archive_T = type [zipfile .ZipFile ] | type [rarfile .RarFile ] | type [_TarFile ]
110+ type _Archive = zipfile .ZipFile | rarfile .RarFile | _TarFile
111+
112+
95113class ThumbRenderer (QObject ):
96114 """A class for rendering image and file thumbnails."""
97115
@@ -871,9 +889,11 @@ def _epub_cover(filepath: Path, ext: str) -> Image.Image | None:
871889 """
872890 im : Image .Image | None = None
873891 try :
874- archiver : type [ zipfile . ZipFile ] | type [ rarfile . RarFile ] = zipfile .ZipFile
892+ archiver : _Archive_T = zipfile .ZipFile
875893 if ext == ".cbr" :
876894 archiver = rarfile .RarFile
895+ elif ext == ".cbt" :
896+ archiver = _TarFile
877897
878898 with archiver (filepath , "r" ) as archive :
879899 if "ComicInfo.xml" in archive .namelist ():
@@ -899,12 +919,12 @@ def _epub_cover(filepath: Path, ext: str) -> Image.Image | None:
899919
900920 @staticmethod
901921 def __cover_from_comic_info (
902- archive : zipfile . ZipFile | rarfile . RarFile , comic_info : Element , cover_type : str
922+ archive : _Archive , comic_info : Element , cover_type : str
903923 ) -> Image .Image | None :
904924 """Extract the cover specified in ComicInfo.xml.
905925
906926 Args:
907- archive (zipfile.ZipFile | rarfile.RarFile ): The current ePub file.
927+ archive (_Archive ): The current ePub file.
908928 comic_info (Element): The parsed ComicInfo.xml.
909929 cover_type (str): The type of cover to load.
910930
0 commit comments