public static async Task ExtractToDirectoryAsync(this ZipArchive archive, string destinationDirectoryName, bool overwrite, CancellationToken cancellationToken = default, bool deleteAtCancellation = false) { try { foreach (var file in archive.Entries) { string completeFileName = Path.Combine(destinationDirectoryName, file.FullName); string directory = Path.GetDirectoryName(completeFileName); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } if (!file.Name.IsNullOrEmpty()) { await file.ExtractToFileAsync(completeFileName, overwrite, cancellationToken); } } } catch (OperationCanceledException) { if (deleteAtCancellation) { await DirectoryAsync.TryAndRetryDeleteAsync(destinationDirectoryName); } } }
public static async Task ExtractToDirectoryAsync(this ZipArchiveEntry entry, string destinationDirectoryName, bool overwrite, CancellationToken cancellationToken = default, bool deleteAtCancellation = false) { try { if (entry.FullName.IsNullOrEmpty() || destinationDirectoryName.IsNullOrEmpty()) { return; } var entries = await entry.Archive.Entries.WhereAsync(e => e.FullName.StartsWith(entry.FullName) && !e.FullName.EndsWith("/"), cancellationToken); foreach (var e in entries) { string path = Path.Combine(destinationDirectoryName, e.FullName.Substring(entry.FullName.Length)); Directory.CreateDirectory(Path.GetDirectoryName(path)); await e.ExtractToFileAsync(path, overwrite, cancellationToken, deleteAtCancellation); } } catch (OperationCanceledException) { if (deleteAtCancellation) { await DirectoryAsync.TryAndRetryDeleteAsync(destinationDirectoryName); } } }