【问题标题】:Using java.util.zip to construct valid epub使用 java.util.zip 构造有效的 epub
【发布时间】:2011-08-15 08:04:51
【问题描述】:

我构建了一种方法,可以递归地将文件夹的内容添加到文件扩展名为“epub”的 zip 文档中,这基本上就是 epub,除了一件事:

存档中的第一个文档必须命名为“mimetype”,类型必须指定为application/epub+zip,并且必须以字节偏移量38开头。有没有办法将mimetype添加到带有偏移量的存档中38?

我构建的方法几乎可行。它构建了一个可以被大多数电子阅读器阅读的 epub,但它不验证。 EpubCheck 给出了这个错误:

mimetype contains wrong type (application/epub+zip expected)

这是一个在原始测试 epub 中不存在的问题,但在重构的 epub 中出现。我仔细检查了解压缩/重新压缩的 mimetype 文件的内容是否正确。

方法太多,这里就不贴了。但这是我用来将 mimetype 文件添加到存档的:

out = new ZipOutputStream(new FileOutputStream(outFilename));

FileInputStream in = new FileInputStream(mimeTypePath);
out.putNextEntry(new ZipEntry("mimetype"));

int len;
while ((len = in.read(buf)) > 0) {
   out.write(buf, 0, len);
}

out.closeEntry();
in.close();
out.close();

【问题讨论】:

  • 有什么特别的原因为什么您要重新发明轮子而不是使用现有的库,例如siegmann.nl/epublib?即使您最终没有使用它,阅读源代码也会告诉您它是如何正确完成的。
  • 您能否简要介绍创建epub zip的过程或建议一些教程?
  • @GAMA,您的问题可能更适合stackoverflow 上的新主题。如果您发布问题,您很可能会获得所需的信息。根据您的尝试,我可能有您需要的答案。但它需要在一个新的主题中。

标签: java zip epub


【解决方案1】:

根据维基百科对Open Container Format 的描述,mimetype 文件应该是 ZIP 文件中的第一个条目,并且应该是未压缩的。

仅根据您的示例代码,尚不清楚您是否指定mimetype 文件应为STORED(未压缩)。

以下内容似乎让我摆脱了“mimetype contains wrong type”错误:

private void writeMimeType(ZipOutputStream zip) throws IOException {
    byte[] content = "application/epub+zip".getBytes("UTF-8");
    ZipEntry entry = new ZipEntry("mimetype");
    entry.setMethod(ZipEntry.STORED);
    entry.setSize(20);
    entry.setCompressedSize(20);
    entry.setCrc(0x2CAB616F); // pre-computed
    zip.putNextEntry(entry);
    zip.write(content);
    zip.closeEntry();
}

已确认:删除 setMethod()setSize()setCompressedSize()setCrc() 行会从 epubcheck 产生“mimetype contains wrong type”错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 2016-11-20
    相关资源
    最近更新 更多