首页 文章

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

提问于
浏览
6

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

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

我 Build 的方法几乎可行 . 它构建了一个可以被大多数电子阅读器读取的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();

1 回答

  • 7

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

    根据您的示例代码,'s not clear whether you'重新指定 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();
    }
    

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

相关问题