示例#1
0
        /// <summary>
        /// Open the comic based on archivePath, this closes the current one
        /// </summary>
        public void OpenComicBook(string archivePath)
        {
            //get the comic book from the archivePath using ComicAccess
            ComicAccess comAcc = new ComicAccess();

            comicBook = comAcc.GetComicBook(archivePath);

            //set extract folder in temporary folder pertaining to comic, and make it the new tempPath
            //this will be the cache for the comic book
            tempPath = Path.Combine(tempPath,
                                    Path.GetFileNameWithoutExtension(archivePath));

            //check if comic book has already been cached to temp folder
            if (Directory.Exists(tempPath) && Directory.GetFiles(tempPath).Length != 0)
            {
                //don't extract anymore, load the comic book from cache
                comicBook.SetComicPath(tempPath);
                //set current page as cover page(first page)
                currentPage = 1;
                //nothing to report
                ReportProgress?.Invoke(this, new ProgressArgs
                {
                    TotalProcessed   = 100,
                    TotalRecords     = 100,
                    Description      = "Done!",
                    IsDoneProcessing = true
                });
            }
            //comic book has not been cached yet, perform archive extraction
            else
            {
                //create extract folder in temporary folder pertaining to comic
                Directory.CreateDirectory(tempPath);

                //direct the program to the dependency: 7z.dll (7z64.dll for 64-bit)
                SevenZipBase.SetLibraryPath(Path.Combine(Environment.CurrentDirectory, "7z.dll"));

                //extract archive to temporary folder
                using (var extractor = new SevenZipExtractor(archivePath))
                {
                    //extract all images in temporary folder
                    for (var i = 0; i < extractor.ArchiveFileData.Count; i++)
                    {
                        //check if file to be extracted is an image
                        string fileToBeExtractedExtension = Path.GetExtension(extractor.ArchiveFileData[i].FileName);
                        if (ImageChecker.IsImageExtension(fileToBeExtractedExtension))
                        {
                            //extract the file
                            extractor.ExtractFiles(tempPath, extractor.ArchiveFileData[i].Index);
                        }

                        //report progress that a file has been processed
                        ReportProgress?.Invoke(this, new ProgressArgs
                        {
                            TotalProcessed   = i + 1,
                            TotalRecords     = extractor.ArchiveFileData.Count,
                            Description      = "Extracting comic book to cache.",
                            IsDoneProcessing = false
                        });
                    }
                }

                //move images outside the directories, if there are any
                string[] comicDirs = Directory.GetDirectories(tempPath);
                if (comicDirs.Length != 0)
                {
                    string[] fileList;
                    foreach (var dir in comicDirs)
                    {
                        //get files in directory
                        fileList = Directory.GetFiles(dir);
                        //start a new report
                        //report progress that a file has been processed
                        ReportProgress?.Invoke(this, new ProgressArgs
                        {
                            TotalProcessed   = 0,
                            TotalRecords     = fileList.Length,
                            Description      = "Rearranging comic book.",
                            IsDoneProcessing = false
                        });

                        int filesProcessed = 0;
                        //bring them to tempPath
                        foreach (var file in fileList)
                        {
                            string newFilePath = Path.Combine(tempPath, Path.GetFileName(file));
                            File.Move(file, newFilePath);
                            ReportProgress?.Invoke(this, new ProgressArgs
                            {
                                TotalProcessed   = filesProcessed,
                                TotalRecords     = fileList.Length,
                                Description      = "Rearranging comic book.",
                                IsDoneProcessing = false
                            });
                            filesProcessed++;
                        }
                        //delete the directory
                        Directory.Delete(dir);
                    }
                }

                //set the comic book path
                comicBook.SetComicPath(tempPath);

                //set current page as cover page(first page)
                currentPage = 1;

                //end reporting
                ReportProgress?.Invoke(this, new ProgressArgs
                {
                    TotalProcessed   = 100,
                    TotalRecords     = 100,
                    Description      = "Done!",
                    IsDoneProcessing = true
                });
                //go back to temp directory
                tempPath = Path.Combine(tempPath, @"..\");
            }
        }