1

我使用 .NET 4.5 System.IO.Compression.ZipArchive 编写了一些生成 ePub 存档的代码。我需要它成为可移植类库 (PCL) 的一部分,因此使用 .NET fx 的子集。

我对包含魔术“application/epub+zip”的 mimetype 文件有疑问。阅读规范后,我首先添加该文件,不进行压缩。

尽管有这种小心,但生成的 ePub 档案不符合规范。规范要求 mimetype 文件的内容应从位置 38 开始。我的从位置 47 开始。

ZipArchive 本身没有任何参数,ZipArchiveEntry 只能通过压缩模式进行参数化。我有点困惑,因为我认为 Zip 文件有一个种类,我不明白是什么影响了这种特定的行为。

作为参考,以下是示例 ePub 的开头部分(正在工作):

在此处输入图像描述

这是我的:

在此处输入图像描述

4

3 回答 3

2

您没有将压缩方法设置为“无压缩”。数据的 9-10 字节用于压缩方法,对于工作文件,它应该是 00,但在您的情况下,它们设置为 8 - 'deflate'。压缩级别不是压缩方式,设置为0依然使用deflate。您应该尝试其他库,例如 SecureBlackbox 或 DotNetZip。

于 2013-10-28T22:54:22.673 回答
1

正如 Nickolay 指出的那样,正在使用“deflate”方法而不是“store”。找到解决这个问题的方法对我来说真的很痛苦,对于发现这个主题的其他人来说,我使用 Jaime Olivares 的 ZipStorer 类来添加使用“store”的 mimetype。

https://github.com/jaime-olivares/zipstorer

将此代码添加到 C# 项目(它不是 DLL)很容易,并且使用“store”而不是“deflate”很容易添加文件。这是我的代码:

Dictionary<string, string> FilesToZip = new Dictionary<string, string>()
{
    { ConfigPath + @"mimetype",                 @"mimetype"},
    { ConfigPath + @"container.xml",            @"META-INF/container.xml" },
    { OutputFolder + Name.Output_OPF_Name,      @"OEBPS/" + Name.Output_OPF_Name},
    { OutputFolder + Name.Output_XHTML_Name,    @"OEBPS/" + Name.Output_XHTML_Name},
    { ConfigPath + @"style.css",                @"OEBPS/style.css"},
    { OutputFolder + Name.Output_NCX_Name,      @"OEBPS/" + Name.Output_NCX_Name}
};

using (ZipStorer EPUB = ZipStorer.Create(OutputFolder + "book.epub", ""))
{
    bool First = true;
    foreach (KeyValuePair<string, string> File in FilesToZip)
    {
        if (First) { EPUB.AddFile(ZipStorer.Compression.Store, File.Key, File.Value, ""); First = false; }
        else EPUB.AddFile(ZipStorer.Compression.Deflate, File.Key, File.Value, "");
    }
}

此代码创建一个完全有效的 EPUB 文件。但是,如果您不需要担心验证,似乎大多数电子阅读器都会接受带有“deflate”mimetype 的 EPUB。因此,我之前使用 .NET 的 ZipArchive 的代码生成了在 Adob​​e Digital Editions 和 PocketBook 中工作的 EPUB。例如:

/*using (ZipArchive EPUB = ZipFile.Open(OutputFolder + Name.Output_EPUB_Name, ZipArchiveMode.Create))
{
    foreach (KeyValuePair<string, string> AddFile in AddFiles)
    {
        if (AddFile.Key.Contains("mimetype"))
        {
            EPUB.CreateEntryFromFile(AddFile.Key, AddFile.Value, CompressionLevel.NoCompression);
        }
        else EPUB.CreateEntryFromFile(AddFile.Key, AddFile.Value, CompressionLevel.Optimal);
    }
}*/
于 2019-06-08T22:01:40.723 回答
0

我确实遵循了 Nickolay 的建议,我使用 DotNetZip 创建了一个仅包含 mimetype 文件的存档,并将该文件用作其他 epub 的起点。

这种方法允许我在尊重 ePub 规范的同时使用 ZipArchive 及其异步接口。

于 2013-10-29T16:47:23.490 回答