Created
January 7, 2026 20:08
-
-
Save AnthonyGiretti/9d2aaf64b6824350a1b54801121618bc to your computer and use it in GitHub Desktop.
.NET 10 ZipExtractor better async performances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.IO.Compression; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| public static class ZipExtractor | |
| { | |
| public static async Task ExtractZipAsync( | |
| string zipPath, | |
| string destination, | |
| CancellationToken ct = default) | |
| { | |
| Directory.CreateDirectory(destination); | |
| await using var fs = File.OpenRead(zipPath); | |
| using var archive = new ZipArchive(fs, ZipArchiveMode.Read, leaveOpen: false); | |
| foreach (var entry in archive.Entries) | |
| { | |
| // Skip directory entries | |
| if (string.IsNullOrEmpty(entry.Name)) | |
| continue; | |
| string outPath = Path.Combine(destination, entry.FullName); | |
| Directory.CreateDirectory(Path.GetDirectoryName(outPath)!); | |
| await using var entryStream = entry.Open(); | |
| await using var outStream = File.Create(outPath); | |
| // Asynchronous copy avoids blocking the calling thread | |
| await entryStream.CopyToAsync(outStream, ct); | |
| Console.WriteLine($"Extracted: {entry.FullName}"); | |
| } | |
| } | |
| } | |
| // Usage: | |
| // No code changes with .NET 10, only better performances | |
| await ZipExtractor.ExtractZipAsync("sample.zip", "./out", CancellationToken.None); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment