private void ExtractFile(SevenZipExtractor extractor, int i, ComicFile comicFile, string [] activeFileNames)
        {
            // KBR 04/01/2016 Attempt to deal with archives-of-archives. Alas, this could go into infinite recursive mode
            if (Utils.ValidateArchiveFileExtension(activeFileNames[i]))
            {
                string tempPath = Path.GetTempFileName();
                using (FileStream fs = new FileStream(tempPath, FileMode.Create))
                {
                    extractor.ExtractFile(activeFileNames[i], fs);
                }
                using (SevenZipExtractor extractor3 = new SevenZipExtractor(tempPath))
                {
                    List <string> tempFileNames = new List <string>();
                    foreach (var archiveFileInfo in extractor3.ArchiveFileData)
                    {
                        if (!archiveFileInfo.IsDirectory)
                        {
                            tempFileNames.Add(archiveFileInfo.FileName);
                        }
                    }
                    _fileNames2 = tempFileNames.ToArray();
                    if (_fileNames2.Length < 1) // Nothing to show!
                    {
                        return;
                    }

                    ArchiveLoader.NumericalSort(_fileNames2);
                    for (int j = 0; j < _fileNames2.Length; j++)
                    {
                        ExtractFile(extractor3, j, comicFile, _fileNames2);
                    }
                }
            }

            //if it is an image add it to array list
            if (Utils.ValidateImageFileExtension(activeFileNames[i]))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    try
                    {
                        extractor.ExtractFile(activeFileNames[i], ms);
                        ms.Position = 0;
                        comicFile.Add(ms.ToArray());
                    }
                    catch
                    {
                        // Effect of exception will be to NOT add the image to the comic
                    }
                }
            }

            //if it is a txt file set it as InfoTxt
            if (Utils.ValidateTextFileExtension(activeFileNames[i]))
            {
                using (var ms = new MemoryStream())
                {
                    extractor.ExtractFile(activeFileNames[i], ms);
                    ms.Position = 0;
                    using (var sr = new StreamReader(ms))
                    {
                        try
                        {
                            comicFile.InfoText = sr.ReadToEnd();
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
        public LoadedFilesData LoadComicBook(string[] files)
        {
            LoadedFilesData returnValue = new LoadedFilesData {
                ComicBook = new ComicBook()
            };

            Array.Sort(files);
            if (files.Any(file => !System.IO.File.Exists(file)))
            {
                returnValue.Error = "One or more archives were not found";
                return(returnValue);
            }

            NukeThread(); // kill any unfinished async load

            var comicFile = new ComicFile {
                Location = files[0]
            };

            returnValue.ComicBook.Add(comicFile);

            int initialFilesToRead;

            try
            {
                using (SevenZipExtractor extractor = new SevenZipExtractor(files[0]))
                {
                    // "bye bye love letter" comic has a folder whose name ends in .PNG, and the extractor thinks it is an image
                    List <string> tempFileNames = new List <string>();
                    foreach (var archiveFileInfo in extractor.ArchiveFileData)
                    {
                        if (!archiveFileInfo.IsDirectory)
                        {
                            tempFileNames.Add(archiveFileInfo.FileName);
                        }
                    }
                    _fileNames = tempFileNames.ToArray();
                    if (_fileNames.Length < 1) // Nothing to show!
                    {
                        returnValue.Error = "Archive has no files.";
                        return(returnValue);
                    }

                    ArchiveLoader.NumericalSort(_fileNames);

                    // TODO need to check validity and keep going if necessary. May result in loading everything synchronous...
                    // The file count may be out-of-sync between the extractor and _filenames, due to skipped folders above
                    // Load the first 5 files (if possible) before returning to GUI
                    initialFilesToRead = Math.Min(5, _fileNames.Count()); // extractor.FilesCount);
                    for (int j = 0; j < initialFilesToRead; j++)
                    {
                        ExtractFile(extractor, j, comicFile, _fileNames);
                    }
                }
            }
            catch (SevenZipArchiveException)
            {
                returnValue.Error = "Extractor failed to handle the archive.";
                return(returnValue);
            }

            // Load remaining files in the background
            _t1 = new Thread(() =>
            {
                using (SevenZipExtractor extractor2 = new SevenZipExtractor(files[0])) // need 2d extractor for thread: see comment at top of file
                {
                    for (int i = initialFilesToRead; i < _fileNames.Length; i++)
                    {
                        ExtractFile(extractor2, i, comicFile, _fileNames);
                    }
                }
            });
            _t1.Start();

            return(returnValue);
        }
        /// <summary>
        /// Loads the specified files.
        /// </summary>
        /// <param name="files">The files.</param>
        /// <returns>Returns <c>true</c> if successful; Otherwise <c>false</c>.</returns>
        public bool Load(string[] files)
        {
            //Clear error
            this.Error = string.Empty;

            this.PageType = PageType.Archive;
            bool returnValue;

            //Check for file types.
            foreach (string file in files)
            {
                int startExtension = file.ToLower().LastIndexOf('.');
                if (startExtension < 0)
                {
                    //File does not have an extension so skip it.
                    break;
                }

                string extension = file.ToLower().Substring(startExtension + 1);
                SupportedImages empty;
                if (Enum.TryParse<SupportedImages>(extension, true, out empty))
                {
                    this.PageType = PageType.Image;
                    break;
                }
                else if (this.PageType == PageType.Image)
                {
                    this.Error = "Please select only archives or only images.";
                }
            }

            if (string.IsNullOrEmpty(this.Error))
            {
                if (PageType == PageType.Archive)
                {
                    ArchiveLoader archiveLoader = new ArchiveLoader();
                    LoadedFileData = archiveLoader.LoadComicBook(files);

                    this.Error = LoadedFileData.Error;
                }
                else if (PageType == PageType.Image)
                {
                    ImageLoader imageLoader = new ImageLoader();
                    LoadedFileData = imageLoader.LoadComicBook(files);

                    this.Error = LoadedFileData.Error;
                }
            }

            returnValue = string.IsNullOrEmpty(this.Error) ? true : false;
            return returnValue;
        }