Example #1
0
        public EpubWriter()
        {
            var opf = new OpfDocument {
                EpubVersion = EpubVersion.Epub3
            };

            opf.Metadata.Dates.Add(new OpfMetadataDate {
                Text = DateTimeOffset.UtcNow.ToString("o")
            });
            opf.Manifest.Items.Add(new OpfManifestItem {
                Id = "ncx", Href = "toc.ncx", MediaType = ContentType.ContentTypeToMimeType[EpubContentType.DtbookNcx]
            });
            opf.Spine.Toc = "ncx";

            format = new EpubFormat
            {
                Opf = opf,
                Nav = new NavDocument(),
                Ncx = new NcxDocument()
            };

            format.Nav.Head.Dom = new XElement(NavElements.Head);
            format.Nav.Body.Dom =
                new XElement(
                    NavElements.Body,
                    new XElement(NavElements.Nav, new XAttribute(NavNav.Attributes.Type, NavNav.Attributes.TypeValues.Toc),
                                 new XElement(NavElements.Ol)));

            resources = new EpubResources();
        }
Example #2
0
        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 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);
            }
        }
Example #4
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);
            }
        }
Example #5
0
        private List <EpubChapter> LoadChapters()
        {
            EpubFormat format = ReadFormat();

            if (format.Nav != null)
            {
                var tocNav = format.Nav.Body.Navs.SingleOrDefault(e => e.Type == NavNav.Attributes.TypeValues.Toc);
                if (tocNav != null)
                {
                    return(LoadChaptersFromNav(format.Paths.NavAbsolutePath, tocNav.Dom));
                }
            }

            if (format.Ncx != null)
            {
                return(LoadChaptersFromNcx(format.Paths.NcxAbsolutePath, format.Ncx.NavMap.NavPoints));
            }

            return(new List <EpubChapter>());
        }
Example #6
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);
        }
Example #7
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);
            }
        }
Example #8
0
        private EpubResources LoadResources(EpubFormat format)
        {
            var resources = new EpubResources();

            foreach (var item in format.Opf.Manifest.Items)
            {
                var      path  = item.Href.ToAbsolutePath(format.Paths.OpfAbsolutePath);
                FileInfo entry = _bookFolder.GetFile("." + 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.OpenRead())
                    {
                        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.OpenRead())
                    {
                        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:
                        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);
        }
Example #9
0
        /*
         * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
         * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
         * @@@@                              Method                                                                @@@@
         * @@@@****************************Read Epub      ******************************************************@@@@
         * @@@@                                                                                                    @@@@
         * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
         * @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
         */
        //------------method geeting smil fil -----------
        public void ProcessEpub(string currentFile)
        {
            try
            {
                string EpubNummerOnly = Path.GetFileNameWithoutExtension(currentFile);

                // Read an epub file
                EpubBook book = EpubReader.Read(currentFile);

                // Read metadata
                string title = book.Title;
                FinalReportlist.Add("Title of book = " + title);

                //******Author******
                var    authors          = book.Authors;
                int    Authors_count    = 0;
                string Authors_Metadata = "";
                foreach (var eachData in authors)
                {
                    Authors_count++;
                    Authors_Metadata = Authors_Metadata + " (" + eachData + "), ";
                }
                FinalReportlist.Add("Author of book= (" + Authors_count + ")" + Authors_Metadata);

                //******images***********
                ICollection <EpubByteFile> images = book.Resources.Images;
                FinalReportlist.Add("Images in  book= (" + images.Count + ")");

                //******other metadata***********
                // Access internal EPUB format specific data structures.
                EpubFormat  format      = book.Format;
                OcfDocument ocf         = format.Ocf;
                OpfDocument opf         = format.Opf;
                NcxDocument ncx         = format.Ncx;
                NavDocument nav         = format.Nav;
                var         bookVersion = format.Opf.EpubVersion;
                var         bookIsbnID  = format.Opf.Metadata.Identifiers;

                var    allMetadata       = opf.Metadata.Languages;
                string language_Metadata = "";
                foreach (var eachData in allMetadata)
                {
                    language_Metadata = language_Metadata + eachData + ", ";
                }
                FinalReportlist.Add("Language of of book= (" + allMetadata.Count + ")" + language_Metadata);
                FinalReportlist.Add("version of book= " + bookVersion);


                // Get table of contents
                FinalReportlist.Add("\n*****Table of Content*****");
                ICollection <EpubChapter> chapters = book.TableOfContents;
                foreach (var eachH1chapter in chapters)
                {
                    FinalReportlist.Add(eachH1chapter.Title);
                    //*******writing h2 Chapter****
                    if (eachH1chapter.SubChapters.Count >= 1)
                    {
                        var h2chapter = eachH1chapter.SubChapters;
                        foreach (var eachH2chapter in h2chapter)
                        {
                            FinalReportlist.Add("\t" + eachH2chapter.Title);
                            //*******writing h3 Chapter****
                            if (eachH2chapter.SubChapters.Count >= 1)
                            {
                                var h3chapter = eachH2chapter.SubChapters;
                                foreach (var eachH3chapter in h3chapter)
                                {
                                    FinalReportlist.Add("\t\t" + eachH3chapter.Title);
                                }
                            }
                        }
                    }
                }
            }// try end
            catch (Exception e)
            {
                string error = "\n Error in syncStart " + Program.Current_bookPath;
                Console.WriteLine(error + e);
                Program.Global_error = Program.Global_error + error;
            } // catch end
        }     //syncStart end