/// <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; } }