Skip to content

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

@acorn371

Description

@acorn371

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:

  • My epub book contains content.opf in the root of epubArchive, not in OEBPS/content.opf
  • while writing my epub book, _EpubWriter.container_file constant contains OEBPS/content.opf
  • As a result, adding the container file META-INF/container.xml, the OEBPS/content.opf reference was set while content.opf was on the root.
  • Subseguent searches throw an exception when I re-read saved epub book.

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)));
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions