Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
acorn371 opened this issue Nov 11, 2020 · 0 comments

Comments

@acorn371
Copy link

acorn371 commented Nov 11, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant