コード例 #1
0
ファイル: Epub.cs プロジェクト: slapware/AdobeIngest
        private void AnalyzeEpub()
        {
            this.FileSizeBytes    = new FileInfo(this.FullName).Length;
            this.IsbnFromFileName = HCPUtils.GetIsbn13(this.FullName);
            this.Isbn             = this.IsbnFromFileName;
            try
            {
                using (ZipArchive archive = ZipFile.OpenRead(this.FullName))
                {
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        // check for invalid DOCTYPE in any .xhtml file
                        if (entry.FullName.EndsWith(".xhtml") && !this.InvalidDocType)
                        {
                            using (var stream = entry.Open())
                                using (var reader = new StreamReader(stream))
                                {
                                    string xhtml = reader.ReadToEnd();
                                    if (xhtml.Contains("about:legacy-compat"))
                                    {
                                        this.InvalidDocType = true;
                                    }
                                }
                        }

                        // check .opf - primarily for isbn
                        if (entry.FullName.EndsWith(".opf"))
                        {
                            this.OpfFileName = entry.FullName;
                            this.HasOpf      = true;

                            // get isbn from opf
                            using (var stream = entry.Open())
                            {
                                XDocument xmlOpf = XDocument.Load(stream);

                                XNamespace dc  = "http://purl.org/dc/elements/1.1/";
                                XNamespace opf = "http://www.idpf.org/2007/opf";
                                this.OpfIdentifier = xmlOpf.Descendants(opf + "metadata").Elements(dc + "identifier").FirstOrDefault().Value;

                                if (!String.IsNullOrEmpty(this.OpfIdentifier))
                                {
                                    // sometimes isbns are formatted with dashes
                                    string tmpIsbn = this.OpfIdentifier.Replace("-", "");
                                    this.IsbnFromOpf = HCPUtils.GetIsbn13(tmpIsbn);
                                }
                            }
                        }
                    }
                }
            }
            catch (System.IO.InvalidDataException exIO)
            {
                this.CorruptFile = true;
                log.Error(String.Format("IO Exception analyzing Epub {0} - {1}", this.FullName, exIO.StackTrace));
            }
            catch (Exception ex)
            {
                log.Error(String.Format("Exception analyzing Epub {0} - {1}", this.FullName, ex.StackTrace));
            }
        }