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