public async Task ExtractAllAsync(CancellationToken token) { var file = new FileInfo(ContainerPath); var extensions = new[] { ".zip", ".cbz" }; if (!extensions.Contains(file.Extension)) { return; } await Task.Run(() => { using var archive = new ZipArchive(File.OpenRead(ContainerPath)); var entries = FileNames.Select(f => archive.Entries.First(e => e.FullName == f)).ToList(); for (var index = 0; index < entries.Count; index++) { var entry = entries[index]; if (Extracted > index) { continue; } if (token.IsCancellationRequested) { return; } try { var tempFile = GetFileFromUnorderedIndex(index); if (File.Exists(tempFile)) { continue; } var fileStream = File.OpenWrite(tempFile); var zipStream = entry.Open(); zipStream.CopyTo(fileStream); zipStream.Dispose(); fileStream.Dispose(); } finally { Extracted++; UpdateExtracted?.Invoke(nameof(Extracted)); } } }, token); }
public string GetFile(int index, out string displayName, SevenZipExtractor extractor, BackgroundWorker worker) { displayName = null; if (index == -1) { return(null); } GetFileFromOrderedIndex(index, out var archivePath, out displayName, out var tempFile); var fullPath = Path.GetFullPath(tempFile); Directory.CreateDirectory(Directory.GetParent(fullPath).FullName); try { if (File.Exists(tempFile)) { return(fullPath); } using var stream = File.OpenWrite(tempFile); lock (this) { extractor.ExtractFile(archivePath, stream); } } catch (Exception ex) { Debug.WriteLine($"Error in RarContainer.GetFile - {ex.Message}"); return(File.Exists(tempFile) ? fullPath : Path.GetFullPath(StaticHelpers.LoadFailedImage)); } finally { worker?.ReportProgress(Extracted); Extracted++; UpdateExtracted?.Invoke(nameof(Extracted)); } return(fullPath); }