public EpubWriter(EpubBook book) { if (book == null) { throw new ArgumentNullException(nameof(book)); } if (book.Format?.Opf == null) { throw new ArgumentException("book opf instance == null", nameof(book)); } format = book.Format; resources = book.Resources; opfPath = format.Ocf.RootFilePath; ncxPath = format.Opf.FindNcxPath(); if (ncxPath != null) { // Remove NCX file from the resources - Write() will format a new one. resources.Other = resources.Other.Where(e => e.FileName != ncxPath).ToList(); ncxPath = PathExt.Combine(PathExt.GetDirectoryPath(opfPath), ncxPath); } }
public void Write(Stream stream) { using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true)) { archive.CreateEntry("mimetype", MimeTypeWriter.Format()); archive.CreateEntry(Constants.OcfPath, OcfWriter.Format(opfPath)); archive.CreateEntry(opfPath, OpfWriter.Format(format.Opf)); if (format.Ncx != null) { archive.CreateEntry(ncxPath, NcxWriter.Format(format.Ncx)); } var allFiles = new[] { resources.Html.Cast <EpubFile>(), resources.Css, resources.Images, resources.Fonts, resources.Other }.SelectMany(collection => collection as EpubFile[] ?? collection.ToArray()); var relativePath = PathExt.GetDirectoryPath(opfPath); foreach (var file in allFiles) { var absolutePath = PathExt.Combine(relativePath, file.FileName); archive.CreateEntry(absolutePath, file.Content); } } }
public static EpubBook Read(Stream stream, bool leaveOpen, Encoding encoding = null) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (encoding == null) { encoding = Constants.DefaultEncoding; } using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen, encoding)) { var format = new EpubFormat { Ocf = OcfReader.Read(archive.LoadXml(Constants.OcfPath)) }; var rootFilePath = format.Ocf.RootFilePath; if (rootFilePath == null) { throw new EpubParseException("Epub OCF doesn't specify a root file."); } format.Opf = OpfReader.Read(archive.LoadXml(rootFilePath)); var navPath = format.Opf.FindNavPath(); if (navPath != null) { var absolutePath = PathExt.Combine(PathExt.GetDirectoryPath(rootFilePath), navPath); format.Nav = NavReader.Read(archive.LoadHtml(absolutePath)); } var ncxPath = format.Opf.FindNcxPath(); if (ncxPath != null) { var absolutePath = PathExt.Combine(PathExt.GetDirectoryPath(rootFilePath), ncxPath); format.Ncx = NcxReader.Read(archive.LoadXml(absolutePath)); } var book = new EpubBook { Format = format }; book.Resources = LoadResources(archive, book); book.SpecialResources = LoadSpecialResources(archive, book); book.CoverImage = LoadCoverImage(book); book.TableOfContents = LoadChapters(book); return(book); } }
public static EpubBook Read(Stream stream, string password) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } using (var archive = ZipFile.Read(stream)) { // OCF var entryOCF = archive.Entries.SingleOrDefault(entry => entry.FileName.Equals(Constants.OcfPath)); if (entryOCF == null) { throw new EpubParseException("Epub OCF doesn't specify a root file."); } var textOCF = GetText(entryOCF, password); var format = new EpubFormat { Ocf = OcfReader.Read(XDocument.Parse(textOCF)) }; var rootFilePath = format.Ocf.RootFilePath; if (rootFilePath == null) { throw new EpubParseException("Epub OCF doesn't specify a root file."); } // OPF var entryOPF = archive.Entries.SingleOrDefault(entry => entry.FileName.Equals(rootFilePath)); if (entryOPF == null) { throw new EpubParseException("Epub OPF doesn't specify a root file."); } var textOPF = GetText(entryOPF, password); format.Opf = OpfReader.Read(XDocument.Parse(textOPF)); // Nav var navPath = format.Opf.FindNavPath(); if (navPath != null) { var absolutePath = PathExt.Combine(PathExt.GetDirectoryPath(rootFilePath), navPath); var entryNav = archive.Entries.SingleOrDefault(entry => entry.FileName.Equals(absolutePath)); if (entryNav != null) { var textNav = GetText(entryNav, password); format.Nav = NavReader.Read(XDocument.Parse(textNav)); } } // Ncx var ncxPath = format.Opf.FindNcxPath(); if (ncxPath != null) { var absolutePath = PathExt.Combine(PathExt.GetDirectoryPath(rootFilePath), ncxPath); var entryNcx = archive.Entries.SingleOrDefault(entry => entry.FileName.Equals(absolutePath)); if (entryNcx != null) { var textNcx = GetText(entryNcx, password); format.Ncx = NcxReader.Read(XDocument.Parse(textNcx)); } } var book = new EpubBook { Format = format }; book.Resources = LoadResources(archive, book, password); book.SpecialResources = LoadSpecialResources(archive, book, password); book.CoverImage = LoadCoverImage(book); book.TableOfContents = new TableOfContents { EpubChapters = LoadChapters(book) }; return(book); } }