使用java.util.zip构造有效的epub

obj*_*sea 7 java zip epub

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

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

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

mimetype contains wrong type (application/epub+zip expected)
Run Code Online (Sandbox Code Playgroud)

这是原始测试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();
Run Code Online (Sandbox Code Playgroud)

Ali*_*ael 8

根据维基百科对开放容器格式的描述,该mimetype文件应该是ZIP文件中的第一个条目,它应该是未压缩的.

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

以下似乎让我过去了"mimetype包含错误类型"错误:

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();
}
Run Code Online (Sandbox Code Playgroud)

确认:移除setMethod(),setSize(),setCompressedSize()setCrc()线产生从"MIME类型包含错误类型"错误epubcheck.