使用Zip库创建Epub文件
本文关键字:Epub 文件 创建 Zip 使用 | 更新日期: 2023-09-27 17:54:49
大家好,
我正在尝试压缩我使用c#制作的Epub文件
我尝试过的事情
- Dot Net Zip http://dotnetzip.codeplex.com/ - DotNetZip工作,但epubcheck失败的结果文件(**见下面的编辑)
- ZipStorer zipstorer.codeplex.com -创建一个通过验证的epub文件,但该文件在Adobe数字版本中无法打开
- 7 zip -我没有尝试过使用c#,但是当我使用那里的接口压缩文件时,它告诉我mimetype文件名的长度为9,它应该是8
在所有情况下,mimetype文件都是添加到存档中的第一个文件,并且没有被压缩
我使用的Epub验证器是epubcheck http://code.google.com/p/epubcheck/
如果有人成功地用这些库之一压缩了epub文件,请让我知道如何或者如果有人成功地用任何其他开源压缩API压缩了epub文件,也可以工作。
编辑
DotNetZip工作,见下面接受的答案
如果需要控制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的文档。
为什么不让生活更轻松呢?
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();
}
}
对于像我这样正在寻找其他方法的人,我想添加Jaime Olivares的zipstoreer类是一个很好的选择。你可以将代码直接复制到你的项目中,并且很容易在"deflate"answers"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"mime类型的EPUB。因此,我之前使用。net的ZipArchive代码生成的epub可以在Adobe数字版本和PocketBook中工作。