using (FileStream zipToCreate = new FileStream(@"C:\example.zip", FileMode.Create)) { using (ZipArchive archive = new ZipArchive(zipToCreate, ZipArchiveMode.Create)) { // add files to the archive ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt"); using (StreamWriter writer = new StreamWriter(readmeEntry.Open())) { writer.WriteLine("This is the readme file."); } } }
using (ZipArchive archive = ZipFile.OpenRead(@"C:\example.zip")) { foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)) { // create a file on disk with the same name as the archive entry string outputFileName = Path.Combine(@"C:\output\", entry.FullName); entry.ExtractToFile(outputFileName); } } }This code opens an existing zip archive, searches for all files with the ".txt" extension, and extracts them to a folder on disk. The ExtractToFile method is used to write the data from the archive entry to a new file on disk. This code requires the System.IO.Compression package.