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; } var comicFile = new ComicFile {Location = files[0]}; returnValue.ComicBook.Add(comicFile); int initialFilesToRead; 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! return returnValue; ArchiveLoader.NumericalSort(_fileNames); // 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); } } // 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); } } }); _t1.Start(); return returnValue; }
/// <summary> /// Loads the comic book. /// </summary> /// <param name="files">The files.</param> /// <returns>A ComicBook where the files are part of a single ComicFile.</returns> public LoadedFilesData LoadComicBook(string[] files) { var returnValue = new LoadedFilesData(); returnValue.ComicBook = new ComicBook(); var comicFile = new ComicFile(); try { foreach (string file in files) { if (!System.IO.File.Exists(file)) { returnValue.Error = "One or more files could not be read, and were skipped"; continue; // KBR just skip the file } // KBR TODO wasn't this check already made? [not from the Q&D multi-file loader...] if (!Utils.ValidateImageFileExtension(file)) continue; // KBR not a supported image extension, skip it using (var fs = System.IO.File.OpenRead(file)) { try { var b = new byte[fs.Length]; fs.Read(b, 0, b.Length); comicFile.Add(b); } catch (Exception) { // couldn't read the file, just skip it returnValue.Error = "One or more files could not be read, and were skipped"; } } } //return the ComicBook on success comicFile.Location = ""; // KBR TODO comicFile is now a collection of images, but each image needs to have its own location returnValue.ComicBook.Add(comicFile); return returnValue; } catch (Exception e) { //show error and return nothing returnValue.Error = e.Message; return returnValue; } }
/// <summary> /// Loads the comic book from a set of separate image files. /// </summary> /// <param name="files">The files.</param> /// <returns>A ComicBook where each file is a separate ComicFile.</returns> public LoadedFilesData LoadComicBook(string[] files) { var returnValue = new LoadedFilesData(); returnValue.ComicBook = new ComicBook(); foreach (string file in files) { if (!System.IO.File.Exists(file)) { returnValue.Error = "One or more files could not be read, and were skipped"; continue; // KBR just skip the file } // KBR Might appear duplicated check, but wasn't performed from the Q&D multi-file loader... if (!Utils.ValidateImageFileExtension(file)) { continue; // KBR not a supported image extension, skip it } try { using (var fs = System.IO.File.OpenRead(file)) { var b = new byte[fs.Length]; fs.Read(b, 0, b.Length); // Change to prior behavior: load each image as a separate ComicFile. This way we // have a per-image location value we can display. var comicFile = new ComicFile { b }; comicFile.Location = file; returnValue.ComicBook.Add(comicFile); } } catch (Exception) { // couldn't read or access the file, just skip it returnValue.Error = "One or more files could not be read, and were skipped"; } } return(returnValue); }
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 comic book. /// </summary> /// <param name="files">The files.</param> /// <returns></returns> public LoadedFilesData LoadComicBook(string[] files) { LoadedFiles = 0; LoadedFilesData returnValue = new LoadedFilesData(); returnValue.ComicBook = new ComicBook(); Comic.ComicFile comicFile = new Comic.ComicFile(); Array.Sort(files); string infoTxt = ""; MemoryStream ms = new MemoryStream(); SevenZipExtractor extractor; Boolean nextFile = false; foreach (String file in files) { if (!System.IO.File.Exists(file)) { returnValue.Error = "One or more archives where not found"; return returnValue; } } try { TotalFiles = files.Length; foreach (string file in files) { //open archive extractor = new SevenZipExtractor(file); string[] fileNames = extractor.ArchiveFileNames.ToArray(); Array.Sort(fileNames); //create ComicFiles for every single archive for (int i = 0; i < extractor.FilesCount; i++) { for (int x = 0; x < Enum.GetNames(typeof(SupportedImages)).Length; x++) { //if it is an image add it to array list if (Utils.ValidateImageFileExtension(fileNames[i])) { ms = new MemoryStream(); extractor.ExtractFile(fileNames[i], ms); ms.Position = 0; try { comicFile.Add(ms.ToArray()); } catch (Exception) { ms.Close(); returnValue.Error = "One or more files are corrupted, and where skipped"; return returnValue; } ms.Close(); nextFile = true; } //if it is a txt file set it as InfoTxt else if (Utils.ValidateTextFileExtension(fileNames[i])) { ms = new MemoryStream(); extractor.ExtractFile(fileNames[i], ms); ms.Position = 0; try { StreamReader sr = new StreamReader(ms); infoTxt = sr.ReadToEnd(); } catch (Exception) { ms.Close(); returnValue.Error = "One or more files are corrupted, and where skipped"; return returnValue; } ms.Close(); nextFile = true; } if (nextFile) { nextFile = false; x = Enum.GetNames(typeof(SupportedImages)).Length; } } } //unlock files again extractor.Dispose(); //Add a ComicFile if (comicFile.Count > 0) { comicFile.Location = file; comicFile.InfoText = infoTxt; returnValue.ComicBook.Add(comicFile); infoTxt = ""; } comicFile = new ComicFile(); LoadedFiles++; } //return the ComicBook on success return returnValue; } catch (Exception e) { //show error and return nothing returnValue.Error = e.Message; return returnValue; } }
/// <summary> /// Loads the comic book. /// </summary> /// <param name="files">The files.</param> /// <returns></returns> public LoadedFilesData LoadComicBook(string[] files) { LoadedFiles = 0; LoadedFilesData returnValue = new LoadedFilesData(); returnValue.ComicBook = new ComicBook(); Comic.ComicFile comicFile = new Comic.ComicFile(); Array.Sort(files); FileStream fs; bool NextFile = false; foreach (string image in files) { if (!System.IO.File.Exists(image)) { returnValue.Error = "One or more images where not found"; return returnValue; } } try { TotalFiles = files.Length; foreach (string file in files) { //open archive for (int x = 0; x < Enum.GetNames(typeof(SupportedImages)).Length; x++) { //if it is an image add it to array list if (Utils.ValidateImageFileExtension(file)) { fs = System.IO.File.OpenRead(file); fs.Position = 0; try { byte[] b = new byte[fs.Length]; fs.Read(b, 0, b.Length); comicFile.Add(b); } catch (Exception) { fs.Close(); returnValue.Error = "One or more files are corrupted, and where skipped"; return returnValue; } fs.Close(); NextFile = true; } if (NextFile) { NextFile = false; x = Enum.GetNames(typeof(SupportedImages)).Length; } } //Add a ComicFile if (comicFile.Count > 0) { comicFile.Location = file; returnValue.ComicBook.Add(comicFile); } comicFile = new ComicFile(); LoadedFiles++; } //return the ComicBook on success return returnValue; } catch (Exception e) { //show error and return nothing returnValue.Error = e.Message; return returnValue; } }
/// <summary> /// Loads the comic book. /// </summary> /// <param name="files">The files.</param> /// <returns></returns> public LoadedFilesData LoadComicBook(string[] files) { LoadedFiles = 0; LoadedFilesData returnValue = new LoadedFilesData { ComicBook = new ComicBook() }; var comicFile = new ComicFile(); Array.Sort(files); string infoTxt = ""; SevenZipExtractor extractor = null; if (files.Any(file => !System.IO.File.Exists(file))) { returnValue.Error = "One or more archives were not found"; return(returnValue); } try { foreach (string file in files) { //open archive extractor = new SevenZipExtractor(file); string[] fileNames = extractor.ArchiveFileNames.ToArray(); // 20140901 Sort using numeric rules NumericalSort(fileNames); //create ComicFiles for every single archive for (int i = 0; i < extractor.FilesCount; i++) { //if it is an image add it to array list if (Utils.ValidateImageFileExtension(fileNames[i])) { using (MemoryStream ms = new MemoryStream()) { extractor.ExtractFile(fileNames[i], ms); ms.Position = 0; try { comicFile.Add(ms.ToArray()); } catch (Exception) { returnValue.Error = "One or more files are corrupted, and were skipped"; return(returnValue); } } } //if it is a txt file set it as InfoTxt if (Utils.ValidateTextFileExtension(fileNames[i])) { var ms = new MemoryStream(); extractor.ExtractFile(fileNames[i], ms); ms.Position = 0; StreamReader sr = null; try { sr = new StreamReader(ms); infoTxt = sr.ReadToEnd(); } catch (Exception) { returnValue.Error = "One or more files are corrupted, and were skipped"; return(returnValue); } finally { if (sr != null) { sr.Dispose(); } ms.Dispose(); } } } //unlock files again extractor.Dispose(); extractor = null; //Add a ComicFile if (comicFile.Count > 0) { comicFile.Location = file; comicFile.InfoText = infoTxt; returnValue.ComicBook.Add(comicFile); infoTxt = ""; } comicFile = new ComicFile(); LoadedFiles++; } //return the ComicBook on success return(returnValue); } catch (Exception e) { //show error and return nothing returnValue.Error = e.Message; return(returnValue); } finally { if (extractor != null) { extractor.Dispose(); } } }
/// <summary> /// Loads the comic book. /// </summary> /// <param name="files">The files.</param> /// <returns></returns> public LoadedFilesData LoadComicBook(string[] files) { LoadedFiles = 0; LoadedFilesData returnValue = new LoadedFilesData {ComicBook = new ComicBook()}; var comicFile = new ComicFile(); Array.Sort(files); string infoTxt = ""; SevenZipExtractor extractor = null; if (files.Any(file => !System.IO.File.Exists(file))) { returnValue.Error = "One or more archives were not found"; return returnValue; } try { foreach (string file in files) { //open archive extractor = new SevenZipExtractor(file); string[] fileNames = extractor.ArchiveFileNames.ToArray(); // 20140901 Sort using numeric rules NumericalSort(fileNames); //create ComicFiles for every single archive for (int i = 0; i < extractor.FilesCount; i++) { //if it is an image add it to array list if (Utils.ValidateImageFileExtension(fileNames[i])) { using (MemoryStream ms = new MemoryStream()) { extractor.ExtractFile(fileNames[i], ms); ms.Position = 0; try { comicFile.Add(ms.ToArray()); } catch (Exception) { returnValue.Error = "One or more files are corrupted, and were skipped"; return returnValue; } } } //if it is a txt file set it as InfoTxt if (Utils.ValidateTextFileExtension(fileNames[i])) { var ms = new MemoryStream(); extractor.ExtractFile(fileNames[i], ms); ms.Position = 0; StreamReader sr = null; try { sr = new StreamReader(ms); infoTxt = sr.ReadToEnd(); } catch (Exception) { returnValue.Error = "One or more files are corrupted, and were skipped"; return returnValue; } finally { if (sr != null) sr.Dispose(); ms.Dispose(); } } } //unlock files again extractor.Dispose(); extractor = null; //Add a ComicFile if (comicFile.Count > 0) { comicFile.Location = file; comicFile.InfoText = infoTxt; returnValue.ComicBook.Add(comicFile); infoTxt = ""; } comicFile = new ComicFile(); LoadedFiles++; } //return the ComicBook on success return returnValue; } catch (Exception e) { //show error and return nothing returnValue.Error = e.Message; return returnValue; } finally { if (extractor != null) extractor.Dispose(); } }