public void LireLesInfo() { EpubBook epubBook = EpubReader.ReadBook(chemin); // Lis les informations de base d'un fichier EPUB auteur = epubBook.Author; titre = epubBook.Title; if (epubBook.AuthorList.Count() != 0) { auteurs = epubBook.AuthorList[0]; } else { auteurs = "Pas de plusieurs auteurs."; } byte[] coverImageContent = epubBook.CoverImage; if (coverImageContent != null) { using (MemoryStream memoryStream = new MemoryStream(coverImageContent)) { picture = Image.FromStream(memoryStream); } } EpubPackage epubPackage = epubBook.Schema.Package; if (epubPackage.Metadata.Dates.Count() != 0) { date = epubPackage.Metadata.Dates[0].Date; } if (epubPackage.Metadata.Languages.Count() != 0) { langage = epubPackage.Metadata.Languages[0]; } if (epubPackage.Metadata.Identifiers.Count() != 0) { isbn = epubPackage.Metadata.Identifiers[0].Identifier; //isbn = epubPackage.Metadata.Identifiers[0].Id; } if (epubPackage.Metadata.Subjects.Count() != 0) { genre = epubPackage.Metadata.Subjects[0]; } if (epubPackage.Metadata.Publishers.Count() != 0) { editeur = epubPackage.Metadata.Publishers[0]; } if (epubPackage.Metadata.Rights.Count() != 0) { droits = epubPackage.Metadata.Rights[0]; } descri = epubPackage.Metadata.Description; }
public static async Task <EpubPackage> ReadPackageAsync(ZipArchive epubArchive, string rootFilePath) { var rootFileEntry = epubArchive.GetEntry(rootFilePath); if (rootFileEntry == null) { throw new Exception("EPUB parsing error: root file not found in archive."); } XDocument containerDocument; using (var containerStream = rootFileEntry.Open()) { containerDocument = await XmlUtils.LoadDocumentAsync(containerStream).ConfigureAwait(false); } XNamespace opfNamespace = "http://www.idpf.org/2007/opf"; var packageNode = containerDocument.Element(opfNamespace + "package"); var result = new EpubPackage(); var epubVersionValue = packageNode.Attribute("version").Value; EpubVersion epubVersion; switch (epubVersionValue) { case "1.0": case "2.0": epubVersion = EpubVersion.EPUB_2; break; case "3.0": epubVersion = EpubVersion.EPUB_3_0; break; case "3.1": epubVersion = EpubVersion.EPUB_3_1; break; default: throw new Exception($"Unsupported EPUB version: {epubVersionValue}."); } result.EpubVersion = epubVersion; var metadataNode = packageNode.Element(opfNamespace + "metadata"); if (metadataNode == null) { throw new Exception("EPUB parsing error: metadata not found in the package."); } var metadata = ReadMetadata(metadataNode, result.EpubVersion); result.Metadata = metadata; return(result); }
public static async Task <EpubSchema> ReadSchemaAsync(ZipArchive epubArchive) { var result = new EpubSchema(); var rootFilePath = await RootFilePathReader.GetRootFilePathAsync(epubArchive).ConfigureAwait(false); var contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath); result.ContentDirectoryPath = contentDirectoryPath; EpubPackage package = await PackageReader.ReadPackageAsync(epubArchive, rootFilePath).ConfigureAwait(false); result.Package = package; return(result); }
public static EpubSchema ReadSchema(ZipArchive epubArchive) { EpubSchema result = new EpubSchema(); string rootFilePath = RootFilePathReader.GetRootFilePath(epubArchive); string contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath); result.ContentDirectoryPath = contentDirectoryPath; EpubPackage package = PackageReader.ReadPackage(epubArchive, rootFilePath); result.Package = package; EpubNavigation navigation = NavigationReader.ReadNavigation(epubArchive, contentDirectoryPath, package); result.Navigation = navigation; return(result); }
public static async Task <EpubSchema> ReadSchemaAsync(ZipArchive epubArchive) { EpubSchema result = new EpubSchema(); string rootFilePath = await RootFilePathReader.GetRootFilePathAsync(epubArchive).ConfigureAwait(false); string contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath); result.ContentDirectoryPath = contentDirectoryPath; EpubPackage package = await PackageReader.ReadPackageAsync(epubArchive, rootFilePath).ConfigureAwait(false); result.Package = package; EpubNavigation navigation = await NavigationReader.ReadNavigationAsync(epubArchive, contentDirectoryPath, package).ConfigureAwait(false); result.Navigation = navigation; return(result); }
public static async Task <EpubSchema> ReadSchemaAsync(ZipArchive epubArchive) { EpubSchema result = new EpubSchema(); string rootFilePath = await RootFilePathReader.GetRootFilePathAsync(epubArchive).ConfigureAwait(false); string contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath); result.ContentDirectoryPath = contentDirectoryPath; EpubPackage package = await PackageReader.ReadPackageAsync(epubArchive, rootFilePath).ConfigureAwait(false); result.Package = package; result.Epub2Ncx = await Epub2NcxReader.ReadEpub2NcxAsync(epubArchive, contentDirectoryPath, package).ConfigureAwait(false); result.Epub3NavDocument = await Epub3NavDocumentReader.ReadEpub3NavDocumentAsync(epubArchive, contentDirectoryPath, package).ConfigureAwait(false); return(result); }
public static async Task <EpubSchema> ReadSchemaAsync(ZipArchive epubArchive) { EpubSchema result = new EpubSchema(); // Reading META-INF/container.xml string rootFilePath = await RootFilePathReader.GetRootFilePathAsync(epubArchive); // Getting directory path - usually it's: META-INF/ string contentDirectoryPath = ZipPathUtils.GetDirectoryPath(rootFilePath); result.ContentDirectoryPath = contentDirectoryPath; //Reading the file content.opf EpubPackage package = await PackageReader.ReadPackageAsync(epubArchive, rootFilePath); result.Package = package; EpubNavigation navigation = await NavigationReader.ReadNavigationAsync(epubArchive, contentDirectoryPath, package); result.Navigation = navigation; return(result); }
public static EpubPackage ReadPackage(ZipArchive epubArchive, string rootFilePath) { ZipArchiveEntry rootFileEntry = epubArchive.GetEntry(rootFilePath); if (rootFileEntry == null) { throw new Exception("EPUB parsing error: root file not found in archive."); } XmlDocument containerDocument; using (Stream containerStream = rootFileEntry.Open()) containerDocument = XmlUtils.LoadDocument(containerStream); XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(containerDocument.NameTable); xmlNamespaceManager.AddNamespace("opf", "http://www.idpf.org/2007/opf"); XmlNode packageNode = containerDocument.DocumentElement.SelectSingleNode("/opf:package", xmlNamespaceManager); EpubPackage result = new EpubPackage(); string epubVersionValue = packageNode.Attributes["version"].Value; if (epubVersionValue == "2.0") { result.EpubVersion = EpubVersion.EPUB_2; } else if (epubVersionValue == "3.0") { result.EpubVersion = EpubVersion.EPUB_3; } else { throw new Exception(String.Format("Unsupported EPUB version: {0}.", epubVersionValue)); } XmlNode metadataNode = packageNode.SelectSingleNode("opf:metadata", xmlNamespaceManager); if (metadataNode == null) { throw new Exception("EPUB parsing error: metadata not found in the package."); } EpubMetadata metadata = ReadMetadata(metadataNode, result.EpubVersion); result.Metadata = metadata; XmlNode manifestNode = packageNode.SelectSingleNode("opf:manifest", xmlNamespaceManager); if (manifestNode == null) { throw new Exception("EPUB parsing error: manifest not found in the package."); } EpubManifest manifest = ReadManifest(manifestNode); result.Manifest = manifest; XmlNode spineNode = packageNode.SelectSingleNode("opf:spine", xmlNamespaceManager); if (spineNode == null) { throw new Exception("EPUB parsing error: spine not found in the package."); } EpubSpine spine = ReadSpine(spineNode); result.Spine = spine; XmlNode guideNode = packageNode.SelectSingleNode("opf:guide", xmlNamespaceManager); if (guideNode != null) { EpubGuide guide = ReadGuide(guideNode); result.Guide = guide; } return(result); }
public static async Task <EpubNavigation> ReadNavigationAsync(ZipArchive epubArchive, string contentDirectoryPath, EpubPackage package) { EpubNavigation result = new EpubNavigation(); string tocId = package.Spine.Toc; if (String.IsNullOrEmpty(tocId)) { throw new Exception("EPUB parsing error: TOC ID is empty."); } EpubManifestItem tocManifestItem = package.Manifest.FirstOrDefault(item => String.Compare(item.Id, tocId, StringComparison.OrdinalIgnoreCase) == 0); if (tocManifestItem == null) { throw new Exception(String.Format("EPUB parsing error: TOC item {0} not found in EPUB manifest.", tocId)); } string tocFileEntryPath = ZipPathUtils.Combine(contentDirectoryPath, tocManifestItem.Href); ZipArchiveEntry tocFileEntry = epubArchive.GetEntry(tocFileEntryPath); if (tocFileEntry == null) { throw new Exception(String.Format("EPUB parsing error: TOC file {0} not found in archive.", tocFileEntryPath)); } if (tocFileEntry.Length > Int32.MaxValue) { throw new Exception(String.Format("EPUB parsing error: TOC file {0} is larger than 2 Gb.", tocFileEntryPath)); } XDocument containerDocument; using (Stream containerStream = tocFileEntry.Open()) { containerDocument = await XmlUtils.LoadDocumentAsync(containerStream).ConfigureAwait(false); } XNamespace ncxNamespace = "http://www.daisy.org/z3986/2005/ncx/"; XElement ncxNode = containerDocument.Element(ncxNamespace + "ncx"); if (ncxNode == null) { throw new Exception("EPUB parsing error: TOC file does not contain ncx element."); } XElement headNode = ncxNode.Element(ncxNamespace + "head"); if (headNode == null) { throw new Exception("EPUB parsing error: TOC file does not contain head element."); } EpubNavigationHead navigationHead = ReadNavigationHead(headNode); result.Head = navigationHead; XElement docTitleNode = ncxNode.Element(ncxNamespace + "docTitle"); if (docTitleNode == null) { throw new Exception("EPUB parsing error: TOC file does not contain docTitle element."); } EpubNavigationDocTitle navigationDocTitle = ReadNavigationDocTitle(docTitleNode); result.DocTitle = navigationDocTitle; result.DocAuthors = new List <EpubNavigationDocAuthor>(); foreach (XElement docAuthorNode in ncxNode.Elements(ncxNamespace + "docAuthor")) { EpubNavigationDocAuthor navigationDocAuthor = ReadNavigationDocAuthor(docAuthorNode); result.DocAuthors.Add(navigationDocAuthor); } XElement navMapNode = ncxNode.Element(ncxNamespace + "navMap"); if (navMapNode == null) { throw new Exception("EPUB parsing error: TOC file does not contain navMap element."); } EpubNavigationMap navMap = ReadNavigationMap(navMapNode); result.NavMap = navMap; XElement pageListNode = ncxNode.Element(ncxNamespace + "pageList"); if (pageListNode != null) { EpubNavigationPageList pageList = ReadNavigationPageList(pageListNode); result.PageList = pageList; } result.NavLists = new List <EpubNavigationList>(); foreach (XElement navigationListNode in ncxNode.Elements(ncxNamespace + "navList")) { EpubNavigationList navigationList = ReadNavigationList(navigationListNode); result.NavLists.Add(navigationList); } return(result); }
public static async Task <Epub3NavDocument> ReadEpub3NavDocumentAsync(ZipArchive epubArchive, string contentDirectoryPath, EpubPackage package) { Epub3NavDocument result = new Epub3NavDocument(); EpubManifestItem navManifestItem = package.Manifest.FirstOrDefault(item => item.Properties != null && item.Properties.Contains(ManifestProperty.NAV)); if (navManifestItem == null) { if (package.EpubVersion == EpubVersion.EPUB_2) { return(null); } else { throw new Exception("EPUB parsing error: NAV item not found in EPUB manifest."); } } string navFileEntryPath = ZipPathUtils.Combine(contentDirectoryPath, navManifestItem.Href); ZipArchiveEntry navFileEntry = epubArchive.GetEntry(navFileEntryPath); if (navFileEntry == null) { throw new Exception($"EPUB parsing error: navigation file {navFileEntryPath} not found in archive."); } if (navFileEntry.Length > Int32.MaxValue) { throw new Exception($"EPUB parsing error: navigation file {navFileEntryPath} is larger than 2 Gb."); } XDocument navDocument; using (Stream containerStream = navFileEntry.Open()) { navDocument = await XmlUtils.LoadDocumentAsync(containerStream).ConfigureAwait(false); } XNamespace xhtmlNamespace = navDocument.Root.Name.Namespace; XElement htmlNode = navDocument.Element(xhtmlNamespace + "html"); if (htmlNode == null) { throw new Exception("EPUB parsing error: navigation file does not contain html element."); } XElement bodyNode = htmlNode.Element(xhtmlNamespace + "body"); if (bodyNode == null) { throw new Exception("EPUB parsing error: navigation file does not contain body element."); } result.Navs = new List <Epub3Nav>(); string folder = ZipPathUtils.GetDirectoryPath(navManifestItem.Href); foreach (XElement navNode in bodyNode.Elements(xhtmlNamespace + "nav")) { Epub3Nav epub3Nav = ReadEpub3Nav(navNode); AdjustRelativePath(epub3Nav.Ol, folder); result.Navs.Add(epub3Nav); } return(result); }
public static async Task <EpubPackage> ReadPackageAsync(ZipArchive epubArchive, string rootFilePath) { ZipArchiveEntry rootFileEntry = epubArchive.GetEntry(rootFilePath); if (rootFileEntry == null) { throw new Exception("EPUB parsing error: root file not found in archive."); } XDocument containerDocument; using (Stream containerStream = rootFileEntry.Open()) containerDocument = await XmlUtils.LoadDocumentAsync(containerStream).ConfigureAwait(false); XNamespace opfNamespace = "http://www.idpf.org/2007/opf"; XElement packageNode = containerDocument.Element(opfNamespace + "package"); EpubPackage result = new EpubPackage(); string epubVersionValue = packageNode.Attribute("version").Value; if (epubVersionValue == "2.0") { result.EpubVersion = EpubVersion.EPUB_2; } else if (epubVersionValue == "3.0") { result.EpubVersion = EpubVersion.EPUB_3; } else { throw new Exception(String.Format("Unsupported EPUB version: {0}.", epubVersionValue)); } XElement metadataNode = packageNode.Element(opfNamespace + "metadata"); if (metadataNode == null) { throw new Exception("EPUB parsing error: metadata not found in the package."); } EpubMetadata metadata = ReadMetadata(metadataNode, result.EpubVersion); result.Metadata = metadata; XElement manifestNode = packageNode.Element(opfNamespace + "manifest"); if (manifestNode == null) { throw new Exception("EPUB parsing error: manifest not found in the package."); } EpubManifest manifest = ReadManifest(manifestNode); result.Manifest = manifest; XElement spineNode = packageNode.Element(opfNamespace + "spine"); if (spineNode == null) { throw new Exception("EPUB parsing error: spine not found in the package."); } EpubSpine spine = ReadSpine(spineNode); result.Spine = spine; XElement guideNode = packageNode.Element(opfNamespace + "guide"); if (guideNode != null) { EpubGuide guide = ReadGuide(guideNode); result.Guide = guide; } return(result); }
public static async Task <EpubNavigation> ReadNavigationAsync(ZipArchive epubArchive, string contentDirectoryPath, EpubPackage package) { EpubNavigation result = new EpubNavigation(); string tocId = package.Spine.Toc; XmlReaderSettings xmlReaderSettings = new XmlReaderSettings { // XmlResolver = null, Async = true, DtdProcessing = DtdProcessing.Ignore }; if (String.IsNullOrEmpty(tocId)) { throw new Exception("EPUB parsing error: TOC ID is empty."); } //Cheking if toc id referenced in spine exist in manifest EpubManifestItem tocManifestItem = package.Manifest.FirstOrDefault(item => String.Compare(item.Id, tocId, StringComparison.OrdinalIgnoreCase) == 0); if (tocManifestItem == null) { throw new Exception(String.Format("EPUB parsing error: TOC item {0} not found in EPUB manifest.", tocId)); } //Opening .toc file in archive using href-reference from manifest string tocFileEntryPath = ZipPathUtils.Combine(contentDirectoryPath, tocManifestItem.Href); ZipArchiveEntry tocFileEntry = epubArchive.GetEntry(tocFileEntryPath); if (tocFileEntry == null) { throw new Exception(String.Format("EPUB parsing error: TOC file {0} not found in archive.", tocFileEntryPath)); } if (tocFileEntry.Length > Int32.MaxValue) { throw new Exception(String.Format("EPUB parsing error: TOC file {0} is bigger than 2 Gb.", tocFileEntryPath)); } // ------------------ Actual Parsing starts here: ------------------------- using (Stream containerStream = tocFileEntry.Open()) { using (XmlReader xmlReader = XmlReader.Create(containerStream, xmlReaderSettings)) { result.Head = await ReadNavigationHeadAsync(xmlReader); result.DocTitle = await ReadNavigationDocTitleAsync(xmlReader); result.DocAuthors = await ReadNavigationAuthorsAsync(xmlReader); result.NavMap = await ReadNavigationMapAsync(xmlReader); result.NavLists = new List <EpubNavigationList>(); //Empty, because not implemented result.PageList = new EpubNavigationPageList(); //Empty, because not implemented } } return(result); //-------------------------------------------Boring old style Silverlight code...----------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------------------------------------------ //XmlDocument containerDocument; //containerDocument = XmlDocument.Load(containerStream); //XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(containerDocument.NameTable); //xmlNamespaceManager.AddNamespace("ncx", "http://www.daisy.org/z3986/2005/ncx/"); ////Parsing head section //XmlNode headNode = containerDocument.DocumentElement.SelectSingleNode("ncx:head", xmlNamespaceManager); //if (headNode == null) // throw new Exception("EPUB parsing error: TOC file does not contain head element"); //EpubNavigationHead navigationHead = ReadNavigationHead(headNode); //result.Head = navigationHead; ////Parsing title //XmlNode docTitleNode = containerDocument.DocumentElement.SelectSingleNode("ncx:docTitle", xmlNamespaceManager); //if (docTitleNode == null) // throw new Exception("EPUB parsing error: TOC file does not contain docTitle element"); //EpubNavigationDocTitle navigationDocTitle = ReadNavigationDocTitle(docTitleNode); //result.DocTitle = navigationDocTitle; ////Parsing authors section... //result.DocAuthors = new List<EpubNavigationDocAuthor>(); //foreach (XmlNode docAuthorNode in containerDocument.DocumentElement.SelectNodes("ncx:docAuthor", xmlNamespaceManager)) //{ // EpubNavigationDocAuthor navigationDocAuthor = ReadNavigationDocAuthor(docAuthorNode); // result.DocAuthors.Add(navigationDocAuthor); //} //Parsing navMap section //XmlNode navMapNode = containerDocument.DocumentElement.SelectSingleNode("ncx:navMap", xmlNamespaceManager); //if (navMapNode == null) // throw new Exception("EPUB parsing error: TOC file does not contain navMap element"); //EpubNavigationMap navMap = ReadNavigationMap(navMapNode); //result.NavMap = navMap; //-----------------------------------TO-DO: Implement ----------------------------------------------------------- //TO-DO: Implement pageList parsing. Needed to tide-up position inside epub to actual pages of the printed book //-------------------------------------------------------------------------------------------------------------- //Parsing pageList node //XmlNode pageListNode = containerDocument.DocumentElement.SelectSingleNode("ncx:pageList", xmlNamespaceManager); //if (pageListNode != null) //{ // EpubNavigationPageList pageList = ReadNavigationPageList(pageListNode); // result.PageList = pageList; //} ////TO-DO: Implement navList parsing. It is a secondary navigation system for supplied book info - schemes, fugures, diagrams, illustrations etc ////Parsing navList nodes //result.NavLists = new List<EpubNavigationList>(); //foreach (XmlNode navigationListNode in containerDocument.DocumentElement.SelectNodes("ncx:navList", xmlNamespaceManager)) //{ // EpubNavigationList navigationList = ReadNavigationList(navigationListNode); // result.NavLists.Add(navigationList); //} //-------------------------------------------------------------------------------------------------------------- }
private void Load_Click(object sender, RoutedEventArgs e) { var openFileDialog = new Microsoft.Win32.OpenFileDialog() { Filter = "Epub Files (*.epub)|*.epub" }; var result = openFileDialog.ShowDialog(); if (result == true) { // Opens a book and reads all of its content into memory epubBook = EpubReader.ReadBook(openFileDialog.FileName); // COMMON PROPERTIES // Book's title string title = epubBook.Title; // Book's authors (comma separated list) string author = epubBook.Author; // Book's authors (list of authors names) List <string> authors = epubBook.AuthorList; // Book's cover image (null if there is no cover) byte[] coverImageContent = epubBook.CoverImage; if (coverImageContent != null) { using (MemoryStream coverImageStream = new MemoryStream(coverImageContent.ToArray())) { // Assign the Source property of your image try { File.Delete(openFileDialog.FileName + ".jpg"); File.WriteAllBytes(openFileDialog.FileName + ".jpg", coverImageStream.ToArray()); } catch { File.WriteAllBytes(openFileDialog.FileName + ".jpg", coverImageStream.ToArray()); } BitmapImage imageSource = new BitmapImage(new Uri(@"C:/Users/shish/Downloads/1.jpg", UriKind.Absolute)); Image1.Source = imageSource; } } Info.Text = "Title: " + title + "\n" + "Author: " + author + "\n"; Info.FontWeight = FontWeights.Bold; // CHAPTERS // Enumerating chapters foreach (EpubChapter chapter in epubBook.Chapters) { // Title of chapter string chapterTitle = chapter.Title; // HTML content of current chapter string chapterHtmlContent = chapter.HtmlContent; // Nested chapters List <EpubChapter> subChapters = chapter.SubChapters; //PrintChapter(chapter); //Chapters.Inlines.Add(new Run(chapterTitle + "\n")); Chapters.Items.Add(chapterTitle); } // CONTENT // Book's content (HTML files, stlylesheets, images, fonts, etc.) EpubContent bookContent = epubBook.Content; // IMAGES // All images in the book (file name is the key) Dictionary <string, EpubByteContentFile> images = bookContent.Images; EpubByteContentFile firstImage = images.Values.First(); // Content type (e.g. EpubContentType.IMAGE_JPEG, EpubContentType.IMAGE_PNG) EpubContentType contentType = firstImage.ContentType; // MIME type (e.g. "image/jpeg", "image/png") string mimeContentType = firstImage.ContentMimeType; // Creating Image class instance from the content using (MemoryStream imageStream = new MemoryStream(firstImage.Content)) { System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream); } // HTML & CSS // All XHTML files in the book (file name is the key) Dictionary <string, EpubTextContentFile> htmlFiles = bookContent.Html; // All CSS files in the book (file name is the key) Dictionary <string, EpubTextContentFile> cssFiles = bookContent.Css; // Entire HTML content of the book foreach (EpubTextContentFile htmlFile in htmlFiles.Values) { string htmlContent = htmlFile.Content; } // All CSS content in the book foreach (EpubTextContentFile cssFile in cssFiles.Values) { string cssContent = cssFile.Content; } // OTHER CONTENT // All fonts in the book (file name is the key) Dictionary <string, EpubByteContentFile> fonts = bookContent.Fonts; // All files in the book (including HTML, CSS, images, fonts, and other types of files) Dictionary <string, EpubContentFile> allFiles = bookContent.AllFiles; // ACCESSING RAW SCHEMA INFORMATION // EPUB OPF data EpubPackage package = epubBook.Schema.Package; // Enumerating book's contributors foreach (EpubMetadataContributor contributor in package.Metadata.Contributors) { string contributorName = contributor.Contributor; string contributorRole = contributor.Role; } // EPUB NCX data EpubNavigation navigation = epubBook.Schema.Navigation; // Enumerating NCX metadata foreach (EpubNavigationHeadMeta meta in navigation.Head) { string metadataItemName = meta.Name; string metadataItemContent = meta.Content; } } }
//Parsing metadata, manifest, spine and guide public static async Task <EpubPackage> ReadPackageAsync(ZipArchive epubArchive, string rootFilePath) { EpubPackage result = new EpubPackage(); XmlReaderSettings xmlReaderSettings = new XmlReaderSettings { // XmlResolver = null, Async = true, DtdProcessing = DtdProcessing.Ignore }; ZipArchiveEntry rootFileEntry = epubArchive.GetEntry(rootFilePath); if (rootFileEntry == null) { throw new Exception(string.Format("EPUB parsing error: {0} file not found in archive.", rootFilePath)); } //Starting content.opf parsing... using (Stream containerStream = rootFileEntry.Open()) { using (XmlReader xmlReader = XmlReader.Create(containerStream, xmlReaderSettings)) { await xmlReader.ReadToFollowingAsync("package", "http://www.idpf.org/2007/opf"); //Trying to get version attribute from <package version=... //Looks like we only need EPUB version data and we don`t care about unique-identifier //if EPUB version is FUBAR then throwing an exeption xmlReader.MoveToAttribute("version"); string epubVersionValue = xmlReader.Value; if (epubVersionValue == "2.0") { result.EpubVersion = EpubVersion.EPUB_2; } else if (epubVersionValue == "3.0") { result.EpubVersion = EpubVersion.EPUB_3; } else { throw new Exception(String.Format("Unsupported EPUB version: {0}.", epubVersionValue)); } //Reading metadata EpubMetadata metadata = await ReadMetadataAsync(xmlReader, result.EpubVersion); result.Metadata = metadata; //Reading manifest EpubManifest manifest = await ReadManifestAsync(xmlReader); result.Manifest = manifest; //Reading spine EpubSpine spine = await ReadSpineAsync(xmlReader); result.Spine = spine; //Reading guide. And we actually don`t care if it is no present in our EPUB... bool isGuidePresent = await xmlReader.ReadToFollowingAsync("guide", "http://www.idpf.org/2007/opf"); if (isGuidePresent) { EpubGuide guide = await ReadGuideAsync(xmlReader); result.Guide = guide; } } } return(result); }
public static EpubNavigation ReadNavigation(ZipArchive epubArchive, string contentDirectoryPath, EpubPackage package) { EpubNavigation result = new EpubNavigation(); string tocId = package.Spine.Toc; if (String.IsNullOrEmpty(tocId)) { throw new Exception("EPUB parsing error: TOC ID is empty."); } EpubManifestItem tocManifestItem = package.Manifest.FirstOrDefault(item => String.Compare(item.Id, tocId, StringComparison.OrdinalIgnoreCase) == 0); if (tocManifestItem == null) { throw new Exception(String.Format("EPUB parsing error: TOC item {0} not found in EPUB manifest.", tocId)); } string tocFileEntryPath = ZipPathUtils.Combine(contentDirectoryPath, tocManifestItem.Href); ZipArchiveEntry tocFileEntry = epubArchive.GetEntry(tocFileEntryPath); if (tocFileEntry == null) { throw new Exception(String.Format("EPUB parsing error: TOC file {0} not found in archive.", tocFileEntryPath)); } if (tocFileEntry.Length > Int32.MaxValue) { throw new Exception(String.Format("EPUB parsing error: TOC file {0} is bigger than 2 Gb.", tocFileEntryPath)); } XmlDocument containerDocument; using (Stream containerStream = tocFileEntry.Open()) containerDocument = XmlUtils.LoadDocument(containerStream); XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(containerDocument.NameTable); xmlNamespaceManager.AddNamespace("ncx", "http://www.daisy.org/z3986/2005/ncx/"); XmlNode headNode = containerDocument.DocumentElement.SelectSingleNode("ncx:head", xmlNamespaceManager); if (headNode == null) { throw new Exception("EPUB parsing error: TOC file does not contain head element"); } EpubNavigationHead navigationHead = ReadNavigationHead(headNode); result.Head = navigationHead; XmlNode docTitleNode = containerDocument.DocumentElement.SelectSingleNode("ncx:docTitle", xmlNamespaceManager); if (docTitleNode == null) { throw new Exception("EPUB parsing error: TOC file does not contain docTitle element"); } EpubNavigationDocTitle navigationDocTitle = ReadNavigationDocTitle(docTitleNode); result.DocTitle = navigationDocTitle; result.DocAuthors = new List <EpubNavigationDocAuthor>(); foreach (XmlNode docAuthorNode in containerDocument.DocumentElement.SelectNodes("ncx:docAuthor", xmlNamespaceManager)) { EpubNavigationDocAuthor navigationDocAuthor = ReadNavigationDocAuthor(docAuthorNode); result.DocAuthors.Add(navigationDocAuthor); } XmlNode navMapNode = containerDocument.DocumentElement.SelectSingleNode("ncx:navMap", xmlNamespaceManager); if (navMapNode == null) { throw new Exception("EPUB parsing error: TOC file does not contain navMap element"); } EpubNavigationMap navMap = ReadNavigationMap(navMapNode); result.NavMap = navMap; XmlNode pageListNode = containerDocument.DocumentElement.SelectSingleNode("ncx:pageList", xmlNamespaceManager); if (pageListNode != null) { EpubNavigationPageList pageList = ReadNavigationPageList(pageListNode); result.PageList = pageList; } result.NavLists = new List <EpubNavigationList>(); foreach (XmlNode navigationListNode in containerDocument.DocumentElement.SelectNodes("ncx:navList", xmlNamespaceManager)) { EpubNavigationList navigationList = ReadNavigationList(navigationListNode); result.NavLists.Add(navigationList); } return(result); }