10

大家好,

我正在尝试压缩我使用 c# 制作的 Epub 文件

我尝试过的事情

  • 点网邮编 http://dotnetzip.codeplex.com/
  • - DotNetZip 有效,但 epubcheck 无法生成结果文件(**参见下面的编辑)
  • ZipStorer zipstorer.codeplex.com
  • - 创建一个通过验证但无法在 Adob​​e Digital Editions 中打开的 epub 文件
  • 7 拉链
  • - 我没有使用 c# 尝试过,但是当我使用那里的接口压缩文件时,它告诉我 mimetype 文件名的长度为 9,它应该是 8

在所有情况下,mimetype 文件都是添加到存档中的第一个文件,并且未压缩

我正在使用的 Epub 验证器是 epubcheck http://code.google.com/p/epubcheck/

如果有人使用这些库之一成功压缩了 epub 文件,请告诉我如何或是否有人使用任何其他也可以工作的开源压缩 api 成功压缩了 epub 文件。


编辑

DotNetZip 有效,请参阅下面接受的答案。

4

3 回答 3

13

如果需要控制 ZIP 文件中条目的顺序,可以使用 DotNetZip 和 ZipOutputStream。

你说你试过 DotNetZip,它(epub 验证器)给你一个错误,抱怨 mime 类型的东西。这可能是因为您在 DotNetZip 中使用了 ZipFile 类型。如果您使用 ZipOutputStream,您可以控制 zip 条目的顺序,这对于 epub 显然很重要(我不知道格式,只是猜测)。


编辑

我刚刚检查过,维基百科上的 epub 页面描述了您需要如何格式化 .epub 文件。它表示 mimetype 文件必须包含特定文本,必须未压缩和未加密,并且必须作为 ZIP 存档中的第一个文件出现。

使用 ZipOutputStream,您可以通过在该特定 ZipEntry 上设置 CompressionLevel = None 来执行此操作 - 该值不是默认值。

这是一些示例代码:

private void Zipup()
{
    string _outputFileName = "Fargle.epub";
    using (FileStream fs = File.Open(_outputFileName, FileMode.Create, FileAccess.ReadWrite ))
    {
        using (var output= new ZipOutputStream(fs))
        {
            var e = output.PutNextEntry("mimetype");
            e.CompressionLevel = CompressionLevel.None;

            byte[] buffer= System.Text.Encoding.ASCII.GetBytes("application/epub+zip");
            output.Write(buffer,0,buffer.Length);

            output.PutNextEntry("META-INF/container.xml");
            WriteExistingFile(output, "META-INF/container.xml");
            output.PutNextEntry("OPS/");  // another directory
            output.PutNextEntry("OPS/whatever.xhtml");
            WriteExistingFile(output, "OPS/whatever.xhtml");
            // ...
        }
    }
}

private void WriteExistingFile(Stream output, string filename)
{
    using (FileStream fs = File.Open(fileName, FileMode.Read))
    {
        int n = -1;
        byte[] buffer = new byte[2048];
        while ((n = fs.Read(buffer,0,buffer.Length)) > 0)
        {
            output.Write(buffer,0,n);
        }
    }
}

请参阅此处的 ZipOutputStream 文档。

于 2011-05-05T13:57:40.320 回答
7

为什么不让生活更轻松?

private void IonicZip()
{
    string sourcePath = "C:\\pulications\\";
    string fileName = "filename.epub";

    // Creating ZIP file and writing mimetype
    using (ZipOutputStream zs = new ZipOutputStream(sourcePath + fileName))
    {
        var o = zs.PutNextEntry("mimetype");
        o.CompressionLevel = CompressionLevel.None;

        byte[] mimetype = System.Text.Encoding.ASCII.GetBytes("application/epub+zip");
        zs.Write(mimetype, 0, mimetype.Length);
    }

    // Adding META-INF and OEPBS folders including files     
    using (ZipFile zip = new ZipFile(sourcePath + fileName))
    {
        zip.AddDirectory(sourcePath  + "META-INF", "META-INF");
        zip.AddDirectory(sourcePath  + "OEBPS", "OEBPS");
        zip.Save();
    }
}
于 2012-09-06T11:49:56.300 回答
1

对于像我这样正在寻找其他方法的人来说,我想补充一点,Jaime Olivares 的 ZipStorer 类是一个很好的选择。您可以将代码直接复制到您的项目中,并且很容易在“deflate”和“store”之间进行选择。

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

这是我创建 EPUB 的代码:

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。

于 2019-06-09T17:23:58.220 回答