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; }
public async Task ExtractCover(Libraries.LibraryModel library, ComicFile comicFile) { try { // OPEN ZIP ARCHIVE if (!File.Exists(comicFile.FilePath)) { return; } using (var zipArchiveStream = new FileStream(comicFile.FilePath, FileMode.Open, FileAccess.Read)) { using (var zipArchive = new ZipArchive(zipArchiveStream, ZipArchiveMode.Read)) { // LOCATE FIRST JPG ENTRY var zipEntry = zipArchive.Entries .Where(x => x.Name.ToLower().EndsWith(".jpg") || x.Name.ToLower().EndsWith(".jpeg") || x.Name.ToLower().EndsWith(".png")) .OrderBy(x => x.Name) .Take(1) .FirstOrDefault(); if (zipEntry == null) { return; } // OPEN STREAM using (var zipEntryStream = zipEntry.Open()) { await this.SaveThumbnail(zipEntryStream, comicFile.CoverPath); zipEntryStream.Close(); zipEntryStream.Dispose(); } // RELEASE DATE File.SetLastWriteTime(comicFile.CoverPath, zipEntry.LastWriteTime.DateTime.ToLocalTime()); comicFile.ReleaseDate = zipEntry.LastWriteTime.DateTime.ToLocalTime(); zipArchive.Dispose(); } zipArchiveStream.Close(); zipArchiveStream.Dispose(); } } catch (Exception) { throw; } }
public async Task <bool> ExtractCover(LibraryModel library, ComicFile comicFile) { try { // VALIDATE if (!File.Exists(comicFile.FilePath)) { return(false); } // OPEN STREAM using (var zipArchiveStream = new FileStream(comicFile.FilePath, FileMode.Open, FileAccess.Read)) { using (var zipArchive = new ZipArchive(zipArchiveStream, ZipArchiveMode.Read)) { // FIRST ENTRY var zipEntry = zipArchive.Entries .Where(x => x.Name.ToLower().EndsWith(".jpg") || x.Name.ToLower().EndsWith(".jpeg") || x.Name.ToLower().EndsWith(".png")) .OrderBy(x => x.Name) .Take(1) .FirstOrDefault(); if (zipEntry == null) { return(false); } // RETRIEVE IMAGE CONTENT using (var zipEntryStream = zipEntry.Open()) { await this.FileSystem.SaveThumbnail(zipEntryStream, comicFile.CoverPath); zipEntryStream.Close(); } // RELEASE DATE File.SetLastWriteTime(comicFile.CoverPath, zipEntry.LastWriteTime.DateTime.ToLocalTime()); comicFile.ReleaseDate = zipEntry.LastWriteTime.DateTime.ToLocalTime(); } zipArchiveStream.Close(); } // RETURN return(File.Exists(comicFile.CoverPath)); } catch (Exception ex) { Helpers.AppCenter.TrackEvent(ex); return(false); } }
/// <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 async Task <List <ComicPageVM> > ExtractPages(LibraryModel library, ComicFile comicFile) { try { // INITIALIZE short pageIndex = 0; if (!Directory.Exists(comicFile.CachePath)) { Directory.CreateDirectory(comicFile.CachePath); } // DOWNLOAD COMIC FILE FROM REMOTE SERVER IF LOCAL CACHE DOESNT EXIST YET try { if (Directory.GetFiles(comicFile.CachePath).Count() == 0) { if (Xamarin.Essentials.Connectivity.NetworkAccess != Xamarin.Essentials.NetworkAccess.Internet) { return(null); } var downloadUrl = await this.Connector.GetDownloadUrlAsync(new FileData { id = comicFile.Key }); if (string.IsNullOrEmpty(downloadUrl)) { return(null); } // OPEN REMOTE STREAM var downloadStart = DateTime.Now; using (var zipStream = new System.IO.Compression.HttpZipStream(downloadUrl)) { // STREAM SIZE var streamSizeValue = comicFile.GetKeyValue("StreamSize"); if (!string.IsNullOrEmpty(streamSizeValue)) { long streamSize; if (long.TryParse(streamSizeValue, out streamSize)) { zipStream.SetContentLength(streamSize); } } // FIRST ENTRY var entryList = await zipStream.GetEntriesAsync(); entryList.RemoveAll(x => !x.FileName.ToLower().EndsWith(".jpg") && !x.FileName.ToLower().EndsWith(".jpeg") && !x.FileName.ToLower().EndsWith(".png")); var entries = entryList .OrderBy(x => x.FileName) .ToList(); if (entries == null) { return(null); } // RETRIEVE REMOTE IMAGE CONTENT var tasks = new List <Task>(); pageIndex = 0; foreach (var entry in entryList) { tasks.Add(this.ExtractPage(zipStream, entry, comicFile.CachePath, pageIndex++)); } await Task.WhenAll(tasks.ToArray()); } var downloadFinish = DateTime.Now; Helpers.AppCenter.TrackEvent($"Comic.OneDrive.DownloadingPages", $"ElapsedSeconds:{(downloadFinish - downloadStart).TotalSeconds}"); } } catch (Exception exDownload) { Helpers.AppCenter.TrackEvent(exDownload); throw; } if (Directory.GetFiles(comicFile.CachePath).Count() == 0) { return(null); } // LOCATE PAGE IMAGES FROM PATH var pages = Directory.GetFiles(comicFile.CachePath) .Where(x => x.ToLower().EndsWith(".jpg") || x.ToLower().EndsWith(".jpeg") || x.ToLower().EndsWith(".png")) .OrderBy(x => x) .Select(pagePath => new ComicPageVM { Text = Path.GetFileNameWithoutExtension(pagePath), Path = pagePath, IsVisible = false }) .ToList(); // LOOP THROUGH PAGES pageIndex = 0; foreach (var page in pages) { page.Index = pageIndex++; page.Text = page.Text.Substring(1); var pageSize = await this.FileSystem.GetPageSize(page.Path); page.PageSize = new ComicPageSize(pageSize.Width, pageSize.Height); } return(pages); } catch (Exception ex) { Helpers.AppCenter.TrackEvent(ex); return(null); } finally { GC.Collect(); } }
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); }
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 { } } } } }
private void ExtractFile(SevenZipExtractor extractor, int i, ComicFile comicFile) { //if it is an image add it to array list if (Utils.ValidateImageFileExtension(_fileNames[i])) { using (MemoryStream ms = new MemoryStream()) { try { extractor.ExtractFile(_fileNames[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(_fileNames[i])) { using (var ms = new MemoryStream()) { extractor.ExtractFile(_fileNames[i], ms); ms.Position = 0; using (var sr = new StreamReader(ms)) { try { comicFile.InfoText = sr.ReadToEnd(); } catch { } } } } }
/// <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; } }
public async Task <List <ComicPageVM> > ExtractPages(LibraryModel library, ComicFile comicFile) { try { // INITIALIZE var pages = new List <ComicPageVM>(); if (!File.Exists(comicFile.FilePath)) { return(null); } if (!Directory.Exists(comicFile.CachePath)) { Directory.CreateDirectory(comicFile.CachePath); } // OPEN ZIP ARCHIVE using (var zipArchiveStream = new FileStream(comicFile.FilePath, FileMode.Open, FileAccess.Read)) { using (var zipArchive = new ZipArchive(zipArchiveStream, ZipArchiveMode.Read)) { // LOCATE PAGE ENTRIES short pageIndex = 0; var zipEntries = zipArchive.Entries .Where(x => x.Name.ToLower().EndsWith(".jpg") || x.Name.ToLower().EndsWith(".jpeg") || x.Name.ToLower().EndsWith(".png")) .OrderBy(x => x.Name) .ToList(); if (zipEntries == null) { return(null); } // LOOP THROUGH ZIP ENTRIES foreach (var zipEntry in zipEntries) { // PAGE DATA var page = new ComicPageVM { Index = pageIndex, Text = pageIndex.ToString().PadLeft(3, "0".ToCharArray()[0]), IsVisible = false }; page.Path = $"{comicFile.CachePath}{this.FileSystem.PathSeparator}P{page.Text}.jpg"; pages.Add(page); pageIndex++; // EXTRACT PAGE FILE if (!File.Exists(page.Path)) { using (var zipEntryStream = zipEntry.Open()) { using (var pageStream = new FileStream(page.Path, FileMode.CreateNew, FileAccess.Write)) { await zipEntryStream.CopyToAsync(pageStream); await pageStream.FlushAsync(); pageStream.Close(); } zipEntryStream.Close(); } } // PAGE SIZE var pageSize = await this.FileSystem.GetPageSize(page.Path); page.PageSize = new ComicPageSize(pageSize.Width, pageSize.Height); } } zipArchiveStream.Close(); } return(pages); } catch (Exception ex) { Helpers.AppCenter.TrackEvent(ex); return(null); } }
public async Task <bool> ExtractCover(LibraryModel library, ComicFile comicFile) { try { // VALIDATE if (Xamarin.Essentials.Connectivity.NetworkAccess != Xamarin.Essentials.NetworkAccess.Internet) { return(false); } // RETRIEVE THE DOWNLOAD URL var downloadUrl = await this.Connector.GetDownloadUrlAsync(new FileData { id = comicFile.Key }); if (string.IsNullOrEmpty(downloadUrl)) { return(false); } // OPEN REMOTE STREAM using (var zipStream = new System.IO.Compression.HttpZipStream(downloadUrl)) { // STREAM SIZE var streamSizeValue = comicFile.GetKeyValue("StreamSize"); if (!string.IsNullOrEmpty(streamSizeValue)) { long streamSize; if (long.TryParse(streamSizeValue, out streamSize)) { zipStream.SetContentLength(streamSize); } } // FIRST ENTRY var entryList = await zipStream.GetEntriesAsync(); var entry = entryList .Where(x => x.FileName.ToLower().EndsWith(".jpg") || x.FileName.ToLower().EndsWith(".jpeg") || x.FileName.ToLower().EndsWith(".png")) .OrderBy(x => x.FileName) .FirstOrDefault(); if (entry == null) { return(false); } // RETRIEVE REMOTE IMAGE CONTENT var imageByteArray = await zipStream.ExtractAsync(entry); if (imageByteArray == null || imageByteArray.Length == 0) { return(false); } // SAVE CACHE FILE using (var imageStream = new System.IO.MemoryStream(imageByteArray)) { await imageStream.FlushAsync(); imageStream.Position = 0; await this.FileSystem.SaveThumbnail(imageStream, comicFile.CoverPath); imageStream.Close(); System.IO.File.SetLastWriteTime(comicFile.CoverPath, comicFile.ReleaseDate); } } return(System.IO.File.Exists(comicFile.CoverPath)); } catch (Exception ex) { Helpers.AppCenter.TrackEvent(ex); return(false); } }
/// <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(); } }