使用 Zip 库创建 Epub 文件 [英] Creating an Epub file with a Zip library

查看:53
本文介绍了使用 Zip 库创建 Epub 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

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

I am trying to zip up an Epub file i have made using c#

我尝试过的东西

  • Dot Net Zip http://dotnetzip.codeplex.com/
  • - DotNetZip 有效,但 epubcheck 使生成的文件失败(**请参阅下面的编辑)
  • ZipStorer zipstorer.codeplex.com
  • - 创建一个通过验证的 epub 文件,但该文件不会在 Adob​​e Digital Editions 中打开
  • 7 zip
  • - 我没有使用 c# 尝试过这个,但是当我使用那里的接口压缩文件时,它告诉我 mimetype 文件名的长度为 9,它应该是 8
  • Dot Net Zip http://dotnetzip.codeplex.com/
  • - DotNetZip works but epubcheck fails the resulting file (**see edit below)
  • ZipStorer zipstorer.codeplex.com
  • - creates an epub file that passes validation but the file won't open in Adobe Digital Editions
  • 7 zip
  • - I have not tried this using c# but when i zip the file using there interface it tells me that the mimetype file name has a length of 9 and it should be 8

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

In all cases the mimetype file is the first file added to the archive and is not compressed

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

The Epub validator that I'am using is epubcheck http://code.google.com/p/epubcheck/

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

if anyone has succesfully zipped an epub file with one of these libraries please let me know how or if anyone has zipped an epub file successfully with any other open source zipping api that would also work.

编辑

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

DotNetZip works, see accepted answer below.

推荐答案

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

If you need to control the order of the entries in the ZIP file, you can use DotNetZip and the ZipOutputStream.

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

You said you tried DotNetZip and it (the epub validator) gave you an error complaining about the mime type thing. This is probably because you used the ZipFile type within DotNetZip. If you use ZipOutputStream, you can control the ordering of the zip entries, which is apparently important for epub (I don't know the format, just surmising).

编辑

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

I just checked, and the epub page on Wikipedia describes how you need to format the .epub file. It says that the mimetype file must contain specific text, must be uncompressed and unencrypted, and must appear as the first file in the ZIP archive.

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

Using ZipOutputStream, you would do this by setting CompressionLevel = None on that particular ZipEntry - that value is not the default.

这是一些示例代码:

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 的文档.

See the documentation for ZipOutputStream here.

这篇关于使用 Zip 库创建 Epub 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆