使用Zip库创建Epub文件

cla*_*ble 9 c# zip epub

大家好,

我试图压缩我使用c#制作的Epub文件

我尝试过的事情

  • Dot Net Zip http://dotnetzip.codeplex.com/
  • - DotNetZip有效但epubcheck失败了生成的文件(**见下面的编辑)
  • ZipStorer zipstorer.codeplex.com
  • - 创建一个传递验证的epub文件,但该文件不会在Adobe Digital Editions中打开
  • 7拉链
  • - 我没有尝试使用c#,但是当我使用那里的界面压缩文件时,它告诉我mimetype文件名的长度为9,它应该是8

在所有情况下,mimetype文件是添加到存档的第一个文件,不会被压缩

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

如果有人用这些库中的一个成功压缩了一个epub文件,请让我知道如何或者是否有人使用任何其他开源的压缩API成功压缩epub文件.


编辑

DotNetZip有效,见下面接受的答案.

Che*_*eso 12

如果需要控制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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请在此处查看ZipOutputStream的文档.


小智 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();
    }
}
Run Code Online (Sandbox Code Playgroud)