diff --git a/src/calibre/ebooks/pdb/palmdoc/reader.py b/src/calibre/ebooks/pdb/palmdoc/reader.py index a5a58b4d81..ba35a2317e 100644 --- a/src/calibre/ebooks/pdb/palmdoc/reader.py +++ b/src/calibre/ebooks/pdb/palmdoc/reader.py @@ -53,9 +53,12 @@ class Reader(FormatReader): def extract_content(self, output_dir): txt = '' + self.log.info('Decompressing text...') for i in range(1, self.header_record.num_records + 1): + self.log.debug('\tDecompressing text section %i' % i) txt += self.decompress_text(i) + self.log.info('Converting text to OEB...') html = txt_to_markdown(txt) with open(os.path.join(output_dir, 'index.html'), 'wb') as index: index.write(html.encode('utf-8')) diff --git a/src/calibre/ebooks/pdb/palmdoc/writer.py b/src/calibre/ebooks/pdb/palmdoc/writer.py index 705b01daee..784eca5523 100644 --- a/src/calibre/ebooks/pdb/palmdoc/writer.py +++ b/src/calibre/ebooks/pdb/palmdoc/writer.py @@ -30,7 +30,9 @@ class Writer(FormatWriter): header_record = self._header_record(txt_length, len(txt_records)) section_lengths = [len(header_record)] + self.log.info('Compessing data...') for i in range(0, len(txt_records)): + self.log.debug('\tCompressing record %i' % i) txt_records[i] = compress_doc(txt_records[i].encode('utf-8')) section_lengths.append(len(txt_records[i])) diff --git a/src/calibre/ebooks/pdb/ztxt/reader.py b/src/calibre/ebooks/pdb/ztxt/reader.py index 3d96018def..ccc26a3fdc 100644 --- a/src/calibre/ebooks/pdb/ztxt/reader.py +++ b/src/calibre/ebooks/pdb/ztxt/reader.py @@ -14,6 +14,8 @@ from calibre.ebooks.pdb.formatreader import FormatReader from calibre.ebooks.pdb.ztxt import zTXTError from calibre.ebooks.txt.processor import txt_to_markdown, opf_writer +SUPPORTED_VERSION = (1, 40) + class HeaderRecord(object): ''' The first record in the file is always the header record. It holds @@ -27,6 +29,7 @@ class HeaderRecord(object): self.num_records, = struct.unpack('>H', raw[2:4]) self.size, = struct.unpack('>L', raw[4:8]) self.record_size, = struct.unpack('>H', raw[8:10]) + self.flags, = struct.unpack('>B', raw[18:19]) class Reader(FormatReader): @@ -41,6 +44,16 @@ class Reader(FormatReader): self.sections.append(header.section_data(i)) self.header_record = HeaderRecord(self.section_data(0)) + + vmajor = (self.header_record.version & 0x0000FF00) >> 8 + vminor = self.header_record.version & 0x000000FF + if vmajor < 1 or (vmajor == 1 and vminor < 40): + raise zTXTError('Unsupported ztxt version (%i.%i). Only versions newer than %i.%i are supported.' % (vmajor, vminor, SUPPORTED_VERSION[0], SUPPORTED_VERSION[1])) + + if (self.header_record.flags & 0x01) == 0: + raise zTXTError('Only compression method 1 (random access) is supported') + + self.log.debug('Foud ztxt version: %i.%i' % (vmajor, vminor)) # Initalize the decompressor self.uncompressor = zlib.decompressobj() @@ -57,9 +70,12 @@ class Reader(FormatReader): def extract_content(self, output_dir): txt = '' + self.log.info('Decompressing text...') for i in range(1, self.header_record.num_records + 1): + self.log.debug('\tDecompressing text section %i' % i) txt += self.decompress_text(i) + self.log.info('Converting text to OEB...') html = txt_to_markdown(txt) with open(os.path.join(output_dir, 'index.html'), 'wb') as index: index.write(html.encode('utf-8')) diff --git a/src/calibre/ebooks/pdb/ztxt/writer.py b/src/calibre/ebooks/pdb/ztxt/writer.py index 81a0df48af..6e974d1a67 100644 --- a/src/calibre/ebooks/pdb/ztxt/writer.py +++ b/src/calibre/ebooks/pdb/ztxt/writer.py @@ -30,7 +30,9 @@ class Writer(FormatWriter): crc32 = 0 section_lengths = [] compressor = zlib.compressobj(9) + self.log.info('Compressing data...') for i in range(0, len(txt_records)): + self.log.debug('\tCompressing record %i' % i) txt_records[i] = compressor.compress(txt_records[i].encode('utf-8')) txt_records[i] = txt_records[i] + compressor.flush(zlib.Z_FULL_FLUSH) section_lengths.append(len(txt_records[i]))