8

我构建了一个方法,它可以递归地将文件夹的内容添加到文件扩展名为“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();
4

1 回答 1

10

根据 Wikipedia 对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()会产生“mimetype contains wrong type”错误epubcheck

于 2011-08-15T08:55:55.620 回答