Ejemplo n.º 1
0
        public async Task LoadAsync(Stream stream)
        {
            await Task.Run(() =>
            {
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                try
                {
                    Content = new ZipArchive(stream, ZipArchiveMode.Read, false,
                                             System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName == "ja" ?
                                             Encoding.GetEncoding(932) : Encoding.UTF8);
                    OnLoaded(new EventArgs());
                }
                catch { }
            }
                           );

            if (Content == null)
            {
                return;
            }

            var entries = new List <ZipArchiveEntry>();

            string[] supportedFile = BookManager.AvailableExtensionsImage;
            var      cm            = Content.Mode;
            var      files         = Content.Entries;

            foreach (var file in files)
            {
                var s = Path.GetExtension(file.Name).ToLower();
                var b = supportedFile.Contains(Path.GetExtension(file.Name).ToLower());
                if (supportedFile.Contains(Path.GetExtension(file.Name).ToLower()))
                {
                    entries.Add(file);
                }
            }

            IOrderedEnumerable <ZipArchiveEntry> tempOrder;

            if ((bool)SettingStorage.GetValue("SortNaturalOrder"))
            {
                tempOrder = entries.OrderBy((a) => new NaturalSort.NaturalList(a.FullName));
                //entries.Sort((a, b) => NaturalSort.NaturalCompare(a.FullName, b.FullName));
            }
            else
            {
                tempOrder = entries.OrderBy((a) => a.FullName);
                //entries.Sort((a, b) => a.FullName.CompareTo(b.FullName));
            }

            if ((bool)SettingStorage.GetValue("SortCoverComesFirst"))
            {
                tempOrder = tempOrder.ThenBy((a) => a.FullName.ToLower().Contains("cover"));
                //entries.Sort((a, b) => b.FullName.ToLower().Contains("cover").CompareTo(a.FullName.ToLower().Contains("cover")));
            }
            entries = tempOrder.ToList();

            {
                //toc関係
                var toc = new List <TocItem>();
                for (int i = 0; i < entries.Count; i++)
                {
                    var dirs = Path.GetDirectoryName(entries[i].FullName).Split(Path.DirectorySeparatorChar).ToList();
                    dirs.Add(".");

                    var     ctoc     = toc;
                    TocItem lastitem = null;
                    for (int j = 0; j < dirs.Count(); j++)
                    {
                        dirs[j] = dirs[j] == "" ? "." : dirs[j];
                        if (ctoc.Count == 0 || ctoc.Last().Title != dirs[j])
                        {
                            ctoc.Add(new TocItem()
                            {
                                Children = new TocItem[0], Title = dirs[j], Page = i
                            });
                        }

                        if (lastitem != null)
                        {
                            lastitem.Children = ctoc.ToArray();
                        }
                        lastitem = ctoc.Last();
                        ctoc     = lastitem.Children.ToList();
                    }
                }
                Toc = toc.ToArray();
            }

            AvailableEntries = entries.ToArray();
        }
Ejemplo n.º 2
0
        public async Task LoadAsync(Stream sr)
        {
            SharpCompress.Archives.IArchive archive;
            await Task.Run(() =>
            {
                try
                {
                    archive     = SharpCompress.Archives.ArchiveFactory.Open(sr);
                    var entries = new List <SharpCompress.Archives.IArchiveEntry>();
                    foreach (var entry in archive.Entries)
                    {
                        if (!entry.IsDirectory && !entry.IsEncrypted)
                        {
                            if (BookManager.AvailableExtensionsImage.Contains(Path.GetExtension(entry.Key).ToLower()))
                            {
                                entries.Add(entry);
                            }
                        }
                    }

                    IOrderedEnumerable <SharpCompress.Archives.IArchiveEntry> tempOrder;
                    if ((bool)SettingStorage.GetValue("SortNaturalOrder"))
                    {
                        tempOrder = entries.OrderBy((a) => new NaturalSort.NaturalList(a.Key));
                    }
                    else
                    {
                        tempOrder = entries.OrderBy((a) => a.Key);
                    }
                    if ((bool)SettingStorage.GetValue("SortCoverComesFirst"))
                    {
                        tempOrder = tempOrder.ThenBy((a) => a.Key.ToLower().Contains("cover"));
                    }
                    entries = tempOrder.ToList();

                    {
                        //toc関係
                        var toc = new List <TocItem>();
                        for (int i = 0; i < entries.Count; i++)
                        {
                            var dirs = Path.GetDirectoryName(entries[i].Key).Split(Path.DirectorySeparatorChar).ToList();
                            dirs.Add(".");

                            var ctoc         = toc;
                            TocItem lastitem = null;
                            for (int j = 0; j < dirs.Count(); j++)
                            {
                                dirs[j] = dirs[j] == "" ? "." : dirs[j];
                                if (ctoc.Count == 0 || ctoc.Last().Title != dirs[j])
                                {
                                    ctoc.Add(new TocItem()
                                    {
                                        Children = new TocItem[0], Title = dirs[j], Page = i
                                    });
                                }

                                if (lastitem != null)
                                {
                                    lastitem.Children = ctoc.ToArray();
                                }
                                lastitem = ctoc.Last();
                                ctoc     = lastitem.Children.ToList();
                            }
                        }
                        Toc = toc.ToArray();
                    }

                    Entries = entries.ToArray();
                    OnLoaded();
                }
                catch { Entries = new SharpCompress.Archives.IArchiveEntry[0]; }
            });
        }
Ejemplo n.º 3
0
        public static TocItem[] GetTocs(Array list, Hashtable nd, List <iTextSharp.text.pdf.PRIndirectReference> pageRefs)
        {
            int GetPage(iTextSharp.text.pdf.PdfIndirectReference pref)
            {
                return(pageRefs.FindIndex(a =>
                {
                    return a.Number == pref.Number && a.Generation == pref.Generation;
                }));
            }

            if (list == null)
            {
                return(new TocItem[0]);
            }

            var result = new List <TocItem>();

            foreach (var item in list)
            {
                TocItem tocItem = new TocItem();
                if (item is Hashtable itemd)
                {
                    if (itemd.ContainsKey("Named") && itemd["Named"] is string named)
                    {
                        if (nd.ContainsKey(named) && nd[named] is iTextSharp.text.pdf.PdfArray nditem)
                        {
                            //Note
                            //http://www.pdf-tools.trustss.co.jp/Syntax/catalog.html#destinations

                            if (nditem.Length > 0 && nditem[0] is iTextSharp.text.pdf.PdfIndirectReference pir)
                            {
                                //This pir thing is not page number. This is reference to the /Page.
                                //https://stackoverflow.com/questions/30855432/how-to-get-the-page-number-of-an-arbitrary-pdf-object
                                tocItem.Page = GetPage(pir);
                            }
                        }
                        else if (nd.ContainsKey(named) && nd[named] is iTextSharp.text.pdf.PdfDictionary ndDict)
                        {
                            //I dont have PDF file to test this...
                            continue;
                        }
                    }
                    else if (itemd.ContainsKey("Page"))
                    {
                        if (itemd["Page"] is string s)
                        {
                            var res  = s.Split(' ');
                            int page = 0;
                            if (res.Length > 0 && int.TryParse(res[0], out page))
                            {
                                tocItem.Page = page - 1;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else if (itemd["Page"] is iTextSharp.text.pdf.PdfArray nditem)
                        {
                            //There may be the case where this is PdfIndirectReference... But I dont have one.
                            //Does this really happen?
                            if (nditem.Length > 0 && nditem[0] is iTextSharp.text.pdf.PdfIndirectReference pir)
                            {
                                tocItem.Page = GetPage(pir);
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }
                    if (itemd.ContainsKey("Title") && itemd["Title"] is string title)
                    {
                        tocItem.Title = title;
                    }
                    else
                    {
                        continue;
                    }
                    if (itemd.ContainsKey("Kids") && itemd["Kids"] is ArrayList kids)
                    {
                        tocItem.Children = GetTocs(kids.ToArray(), nd, pageRefs);
                    }
                    result.Add(tocItem);
                }
            }
            return(result.ToArray());
        }