Example #1
0
        public static async Task <string> GetRootFilePathAsync(IZipArchive epubArchive)
        {
            const string     EPUB_CONTAINER_FILE_PATH = "META-INF/container.xml";
            IZipArchiveEntry containerFileEntry       = epubArchive.GetEntry(EPUB_CONTAINER_FILE_PATH);

            if (containerFileEntry == null)
            {
                throw new Exception(string.Format(CultureInfo.InvariantCulture, "EPUB parsing error: {0} file not found in archive.", EPUB_CONTAINER_FILE_PATH));
            }

            XDocument containerDocument;

            using (Stream containerStream = containerFileEntry.Open())
            {
                containerDocument = await XmlUtils.LoadDocumentAsync(containerStream).ConfigureAwait(false);
            }

            XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(new NameTable());

            xmlNamespaceManager.AddNamespace("cns", "urn:oasis:names:tc:opendocument:xmlns:container");

            //XElement rootFileNode = containerDocument.XPathSelectElement("/cns:container/cns:rootfiles/cns:rootfile", xmlNamespaceManager);
            IXPather xPather      = DependencyService.Get <IXPather>();
            XElement rootFileNode = xPather.SelectElement(containerDocument, "/cns:container/cns:rootfiles/cns:rootfile", xmlNamespaceManager);

            XAttribute attribute    = rootFileNode.Attribute("full-path");
            string     rootFilePath = attribute.Value;

            return(rootFilePath);
        }
        private Stream OpenContentStream(IZipArchiveEntry contentFileEntry)
        {
            Stream contentStream = contentFileEntry.Open();

            if (contentStream == null)
            {
                throw new Exception(string.Format(CultureInfo.InvariantCulture, "Incorrect EPUB file: content file \"{0}\" specified in manifest is not found.", FileName));
            }

            return(contentStream);
        }
        public async Task <byte[]> ReadContentAsBytesAsync()
        {
            IZipArchiveEntry contentFileEntry = GetContentFileEntry();

            byte[] content = new byte[(int)contentFileEntry.Length];

            using (Stream contentStream = OpenContentStream(contentFileEntry))
            {
                using (MemoryStream memoryStream = new MemoryStream(content))
                {
                    await contentStream.CopyToAsync(memoryStream).ConfigureAwait(false);
                }
            }

            return(content);
        }
        private IZipArchiveEntry GetContentFileEntry()
        {
            string           contentFilePath  = ZipPathUtils.Combine(epubBookRef.Schema.ContentDirectoryPath, FileName);
            IZipArchiveEntry contentFileEntry = epubBookRef.EpubArchive.GetEntry(contentFilePath);

            if (contentFileEntry == null)
            {
                throw new Exception(string.Format(CultureInfo.InvariantCulture, "EPUB parsing error: file {0} not found in archive.", contentFilePath));
            }

            if (contentFileEntry.Length > Int32.MaxValue)
            {
                throw new Exception(string.Format(CultureInfo.InvariantCulture, "EPUB parsing error: file {0} is bigger than 2 Gb.", contentFilePath));
            }

            return(contentFileEntry);
        }
Example #5
0
        protected IDictionary <string, object> LoadMetaFromFile(IZipArchiveEntry entry)
        {
            var xmlDoc  = new XmlDocument();
            var content = new StreamReader(entry.Open()).ReadToEnd();

            var start = content.IndexOf("<dict>", StringComparison.Ordinal);
            var end   = content.LastIndexOf("</dict>", StringComparison.Ordinal) + 7;

            if (start < 0)
            {
                return(null);
            }

            xmlDoc.LoadXml(content.Substring(start, end - start));

            XmlNodeList tagsDict;

            tagsDict = xmlDoc.GetElementsByTagName("dict");
            return(LoadMetaDataFromNode(tagsDict[0]));
        }
 public static void ExtractToFile(this IZipArchiveEntry source, string destinationFileName, bool overwrite)
 {
     source.Instance.ExtractToFile(destinationFileName, overwrite);
 }
Example #7
0
        public static async Task <EpubPackage> ReadPackageAsync(IZipArchive epubArchive, string rootFilePath)
        {
            IZipArchiveEntry 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(CultureInfo.InvariantCulture, "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);
        }
Example #8
0
        protected IDictionary<string, object> LoadMetaFromFile(IZipArchiveEntry entry)
        {
            var xmlDoc = new XmlDocument();
            var content = new StreamReader(entry.Open()).ReadToEnd();

            var start = content.IndexOf("<dict>", StringComparison.Ordinal);
            var end = content.LastIndexOf("</dict>", StringComparison.Ordinal) + 7;

            if (start < 0) return null;

            xmlDoc.LoadXml(content.Substring(start, end - start));

            XmlNodeList tagsDict;
            tagsDict = xmlDoc.GetElementsByTagName("dict");
            return LoadMetaDataFromNode(tagsDict[0]);
        }
        public static async Task <EpubNavigation> ReadNavigationAsync(IZipArchive 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(CultureInfo.InvariantCulture, "EPUB parsing error: TOC item {0} not found in EPUB manifest.", tocId));
            }

            string           tocFileEntryPath = ZipPathUtils.Combine(contentDirectoryPath, tocManifestItem.Href);
            IZipArchiveEntry tocFileEntry     = epubArchive.GetEntry(tocFileEntryPath);

            if (tocFileEntry == null)
            {
                throw new Exception(string.Format(CultureInfo.InvariantCulture, "EPUB parsing error: TOC file {0} not found in archive.", tocFileEntryPath));
            }

            if (tocFileEntry.Length > Int32.MaxValue)
            {
                throw new Exception(string.Format(CultureInfo.InvariantCulture, "EPUB parsing error: TOC file {0} is bigger 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);
        }