private static byte[] LoadCoverImage(EpubBook book) { if (book == null) { throw new ArgumentNullException(nameof(book)); } if (book.Format == null) { throw new ArgumentNullException(nameof(book.Format)); } var coverPath = book.Format.Opf.FindCoverPath(); if (coverPath == null) { return(null); } var coverImageFile = book.Resources.Images.FirstOrDefault(e => e.Href == coverPath); return(coverImageFile?.Content); }
private static EpubSpecialResources LoadSpecialResources(ZipArchive epubArchive, EpubBook book) { var result = new EpubSpecialResources { Ocf = new EpubTextFile { AbsolutePath = Constants.OcfPath, Href = Constants.OcfPath, ContentType = EpubContentType.Xml, MimeType = ContentType.ContentTypeToMimeType[EpubContentType.Xml], Content = epubArchive.LoadBytes(Constants.OcfPath) }, Opf = new EpubTextFile { AbsolutePath = book.Format.Paths.OpfAbsolutePath, Href = book.Format.Paths.OpfAbsolutePath, ContentType = EpubContentType.Xml, MimeType = ContentType.ContentTypeToMimeType[EpubContentType.Xml], Content = epubArchive.LoadBytes(book.Format.Paths.OpfAbsolutePath) }, HtmlInReadingOrder = new List <EpubTextFile>() }; var temp = book.Format.Opf.Manifest.Items .Where(item => ContentType.MimeTypeToContentType.ContainsKey(item.MediaType) && ContentType.MimeTypeToContentType[item.MediaType] == EpubContentType.Xhtml11) ; var htmlFiles = new Dictionary <string, string>(); foreach (var item in temp) { try { htmlFiles.Add(item.Id, item.Href); } catch (Exception) { continue; } } foreach (var item in book.Format.Opf.Spine.ItemRefs) { if (!htmlFiles.TryGetValue(item.IdRef, out string href)) { continue; } var html = book.Resources.Html.Where(e => e.Href == href).FirstOrDefault(); if (html != null) { result.HtmlInReadingOrder.Add(html); } } return(result); }
private static EpubResources LoadResources(ZipArchive epubArchive, EpubBook book) { var resources = new EpubResources(); foreach (var item in book.Format.Opf.Manifest.Items) { var path = item.Href.ToAbsolutePath(book.Format.Paths.OpfAbsolutePath); var entry = epubArchive.GetEntryImproved(path); if (entry == null) { throw new EpubParseException($"file {path} not found in archive."); } if (entry.Length > int.MaxValue) { throw new EpubParseException($"file {path} is bigger than 2 Gb."); } var href = item.Href; var mimeType = item.MediaType; EpubContentType contentType; contentType = ContentType.MimeTypeToContentType.TryGetValue(mimeType, out contentType) ? contentType : EpubContentType.Other; switch (contentType) { case EpubContentType.Xhtml11: case EpubContentType.Css: case EpubContentType.Oeb1Document: case EpubContentType.Oeb1Css: case EpubContentType.Xml: case EpubContentType.Dtbook: case EpubContentType.DtbookNcx: { var file = new EpubTextFile { AbsolutePath = path, Href = href, MimeType = mimeType, ContentType = contentType }; resources.All.Add(file); using (var stream = entry.Open()) { file.Content = stream.ReadToEnd(); } switch (contentType) { case EpubContentType.Xhtml11: resources.Html.Add(file); break; case EpubContentType.Css: resources.Css.Add(file); break; default: resources.Other.Add(file); break; } break; } default: { var file = new EpubByteFile { AbsolutePath = path, Href = href, MimeType = mimeType, ContentType = contentType }; resources.All.Add(file); using (var stream = entry.Open()) { if (stream == null) { throw new EpubException($"Incorrect EPUB file: content file \"{href}\" specified in manifest is not found"); } using (var memoryStream = new MemoryStream((int)entry.Length)) { stream.CopyTo(memoryStream); file.Content = memoryStream.ToArray(); } } switch (contentType) { case EpubContentType.ImageGif: case EpubContentType.ImageJpeg: case EpubContentType.ImagePng: case EpubContentType.ImageSvg: case EpubContentType.ImageBmp: resources.Images.Add(file); break; case EpubContentType.FontTruetype: case EpubContentType.FontOpentype: resources.Fonts.Add(file); break; default: resources.Other.Add(file); break; } break; } } } return(resources); }