【问题标题】:How to create ePub with System.IO.Compression.ZipArchive?如何使用 System.IO.Compression.ZipArchive 创建 ePub?
【发布时间】:2013-10-28 21:27:36
【问题描述】:

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

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

尽管如此谨慎,但生成的 ePub 存档仍不符合规范。规范要求 mimetype 文件的内容应从位置 38 开始。我的从位置 47 开始。

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

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

这是我的:

【问题讨论】:

    标签: .net zip epub ziparchive


    【解决方案1】:

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

    【讨论】:

    • 啊!感谢您的回答!
    【解决方案2】:

    正如 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);
        }
    }*/
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2015-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多