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))
                };

                format.Paths.OcfAbsolutePath = Constants.OcfPath;

                format.Paths.OpfAbsolutePath = format.Ocf.RootFilePath;
                if (format.Paths.OpfAbsolutePath == null)
                {
                    throw new EpubParseException("Epub OCF doesn't specify a root file.");
                }

                format.Opf = OpfReader.Read(archive.LoadXml(format.Paths.OpfAbsolutePath));

                var navPath = format.Opf.FindNavPath();
                if (navPath != null)
                {
                    format.Paths.NavAbsolutePath = navPath.ToAbsolutePath(format.Paths.OpfAbsolutePath);
                    format.Nav = NavReader.Read(archive.LoadHtml(format.Paths.NavAbsolutePath));
                }

                var ncxPath = format.Opf.FindNcxPath();
                if (ncxPath != null)
                {
                    format.Paths.NcxAbsolutePath = ncxPath.ToAbsolutePath(format.Paths.OpfAbsolutePath);
                    format.Ncx = NcxReader.Read(archive.LoadXml(format.Paths.NcxAbsolutePath));
                }

                var book = new EpubBook {
                    Format = format
                };
                book.Resources        = LoadResources(archive, book);
                book.SpecialResources = LoadSpecialResources(archive, book);
                book.CoverImage       = LoadCoverImage(book);
                book.TableOfContents  = LoadChapters(book);
                foreach (var chapter in book.TableOfContents)
                {
                    if (string.IsNullOrEmpty(chapter.AbsolutePath))
                    {
                        chapter.AbsolutePath = book.SpecialResources.HtmlInReadingOrder.First().AbsolutePath;
                    }
                }
                return(book);
            }
        }
Beispiel #2
0
        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);
            }
        }
Beispiel #3
0
        ///////////////////////////////////////

        public EpubFormat ReadFormat()
        {
            var format = new EpubFormat {
                Ocf = OcfReader.Read(_bookFolder.LoadXml(Constants.OcfPath))
            };

            format.Paths.OcfAbsolutePath = Constants.OcfPath;

            format.Paths.OpfAbsolutePath = format.Ocf.RootFilePath;
            if (format.Paths.OpfAbsolutePath == null)
            {
                throw new EpubParseException("Epub OCF doesn't specify a root file.");
            }

            format.Opf = OpfReader.Read(_bookFolder.LoadXml(format.Paths.OpfAbsolutePath));

            //var navPath = format.Opf.FindNavPath();
            //if (navPath != null)
            //{
            //    format.Paths.NavAbsolutePath = navPath.ToAbsolutePath(format.Paths.OpfAbsolutePath);
            //    format.Nav = NavReader.Read(_bookFolder.LoadHtml(format.Paths.NavAbsolutePath));
            //}

            //var ncxPath = format.Opf.FindNcxPath();
            //if (ncxPath != null)
            //{
            //    format.Paths.NcxAbsolutePath = ncxPath.ToAbsolutePath(format.Paths.OpfAbsolutePath);
            //    format.Ncx = NcxReader.Read(_bookFolder.LoadXml(format.Paths.NcxAbsolutePath));
            //}

            //Resources = LoadResources(archive);
            //SpecialResources = LoadSpecialResources(archive);
            //CoverImage = LoadCoverImage();
            //TableOfContents = LoadChapters();

            return(format);
        }
Beispiel #4
0
        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);
            }
        }