mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
When dealing with ZIP/RAR archives, use the file header rather than the file extension to detrmine the file type, when possible. This fixes the common case of CBZ files being actually cbr files and vice versa
This commit is contained in:
parent
593f3aaf0a
commit
504ef95056
@ -217,14 +217,25 @@ def filename_to_utf8(name):
|
|||||||
return name.decode(codec, 'replace').encode('utf8')
|
return name.decode(codec, 'replace').encode('utf8')
|
||||||
|
|
||||||
def extract(path, dir):
|
def extract(path, dir):
|
||||||
ext = os.path.splitext(path)[1][1:].lower()
|
|
||||||
extractor = None
|
extractor = None
|
||||||
if ext in ['zip', 'cbz', 'epub', 'oebzip']:
|
# First use the file header to identify its type
|
||||||
from calibre.libunzip import extract as zipextract
|
with open(path, 'rb') as f:
|
||||||
extractor = zipextract
|
id_ = f.read(3)
|
||||||
elif ext in ['cbr', 'rar']:
|
if id_ == b'Rar':
|
||||||
from calibre.libunrar import extract as rarextract
|
from calibre.libunrar import extract as rarextract
|
||||||
extractor = rarextract
|
extractor = rarextract
|
||||||
|
elif id_.startswith(b'PK'):
|
||||||
|
from calibre.libunzip import extract as zipextract
|
||||||
|
extractor = zipextract
|
||||||
|
if extractor is None:
|
||||||
|
# Fallback to file extension
|
||||||
|
ext = os.path.splitext(path)[1][1:].lower()
|
||||||
|
if ext in ['zip', 'cbz', 'epub', 'oebzip']:
|
||||||
|
from calibre.libunzip import extract as zipextract
|
||||||
|
extractor = zipextract
|
||||||
|
elif ext in ['cbr', 'rar']:
|
||||||
|
from calibre.libunrar import extract as rarextract
|
||||||
|
extractor = rarextract
|
||||||
if extractor is None:
|
if extractor is None:
|
||||||
raise Exception('Unknown archive type')
|
raise Exception('Unknown archive type')
|
||||||
extractor(path, dir)
|
extractor(path, dir)
|
||||||
|
@ -166,6 +166,14 @@ class ComicMetadataReader(MetadataReaderPlugin):
|
|||||||
description = _('Extract cover from comic files')
|
description = _('Extract cover from comic files')
|
||||||
|
|
||||||
def get_metadata(self, stream, ftype):
|
def get_metadata(self, stream, ftype):
|
||||||
|
if hasattr(stream, 'seek') and hasattr(stream, 'tell'):
|
||||||
|
pos = stream.tell()
|
||||||
|
id_ = stream.read(3)
|
||||||
|
stream.seek(pos)
|
||||||
|
if id_ == b'Rar':
|
||||||
|
ftype = 'cbr'
|
||||||
|
elif id.startswith(b'PK'):
|
||||||
|
ftype = 'cbz'
|
||||||
if ftype == 'cbr':
|
if ftype == 'cbr':
|
||||||
from calibre.libunrar import extract_first_alphabetically as extract_first
|
from calibre.libunrar import extract_first_alphabetically as extract_first
|
||||||
extract_first
|
extract_first
|
||||||
|
Loading…
x
Reference in New Issue
Block a user