orthros / dart-epub

Epub Reader and Writer for Dart
MIT License
218 stars 226 forks source link

Epub book read-write then read again => "EPUB parsing error: root file not found in archive." #81

Open acorn371 opened 3 years ago

acorn371 commented 3 years ago

I was using epub package just to see how it works. I read an epub book with EpubBook epubBook = await EpubReader.readBook(bytes);

then i saved it without any changes then I read it again

var written = EpubWriter.writeBook(epubBook); var newBook = await EpubReader.readBook(written);

I got "EPUB parsing error: root file not found in archive." exception. I investigated for a while and I found:

I think the following quick patch of EpubWriter can help and, perhaps, it's possible to apply an even better one.

static Archive _createArchive(EpubBook book) {
    var arch = Archive();

    final StringBuffer buffer = StringBuffer('<?xml version="1.0"?><container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><rootfiles><rootfile full-path=')
      ..write('"')
      ..write(book.Schema.ContentDirectoryPath)
      ..write(book.Schema.ContentDirectoryPath.isEmpty ? 'content.opf' : '/content.opf'  )
      ..write('"')
      ..write(' media-type="application/oebps-package+xml"/></rootfiles></container>');
    final String _container_file = buffer.toString();

    // Add simple metadata
    arch.addFile(ArchiveFile.noCompress(
        "metadata", 20, convert.utf8.encode("application/epub+zip")));

    // Add Container file
    arch.addFile(ArchiveFile("META-INF/container.xml", _container_file.length,
        convert.utf8.encode(_container_file)));
...