Example #1
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);
            }
        }
Example #2
0
        public void Write(Stream stream)
        {
            using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
            {
                archive.CreateEntry("mimetype", MimeTypeWriter.Format());
                archive.CreateEntry(Constants.OcfPath, OcfWriter.Format(opfPath));
                archive.CreateEntry(opfPath, OpfWriter.Format(format.Opf));

                if (format.Ncx != null)
                {
                    archive.CreateEntry(ncxPath, NcxWriter.Format(format.Ncx));
                }

                var allFiles = new[]
                {
                    resources.Html.Cast <EpubFile>(),
                    resources.Css,
                    resources.Images,
                    resources.Fonts,
                    resources.Other
                }.SelectMany(collection => collection as EpubFile[] ?? collection.ToArray());
                var relativePath = PathExt.GetDirectoryPath(opfPath);
                foreach (var file in allFiles)
                {
                    var absolutePath = PathExt.Combine(relativePath, file.FileName);
                    archive.CreateEntry(absolutePath, file.Content);
                }
            }
        }
Example #3
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 #4
0
        private static EpubResources LoadResources(ZipArchive epubArchive, EpubBook book)
        {
            var resources = new EpubResources();

            foreach (var item in book.Format.Opf.Manifest.Items)
            {
                var path  = PathExt.Combine(Path.GetDirectoryName(book.Format.Ocf.RootFilePath), item.Href);
                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 fileName = 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
                    {
                        FileName    = fileName,
                        MimeType    = mimeType,
                        ContentType = contentType
                    };

                    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
                    {
                        FileName    = fileName,
                        MimeType    = mimeType,
                        ContentType = contentType
                    };

                    using (var stream = entry.Open())
                    {
                        if (stream == null)
                        {
                            throw new EpubException($"Incorrect EPUB file: content file \"{fileName}\" 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 #5
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);
            }
        }