using (ZipOutputStream zipStream = new ZipOutputStream(File.Create("archive.zip"))) { ZipEntry entry = new ZipEntry("file.txt"); zipStream.PutNextEntry(entry); byte[] buffer = Encoding.ASCII.GetBytes("Hello, World!"); zipStream.Write(buffer, 0, buffer.Length); zipStream.CloseEntry(); }
using (ZipFile zipFile = new ZipFile("archive.zip")) { foreach (ZipEntry entry in zipFile) { if (!entry.IsFile) continue; string destinationPath = Path.Combine("extracted", entry.Name); using (FileStream stream = File.Create(destinationPath)) { byte[] buffer = new byte[1024]; Stream entryStream = zipFile.GetInputStream(entry); int bytesRead; do { bytesRead = entryStream.Read(buffer, 0, buffer.Length); stream.Write(buffer, 0, bytesRead); } while (bytesRead > 0); } } }In this example, we open an existing zip file called "archive.zip" and extract all its contents to a directory named "extracted". The contents are written to individual files named according to their original names in the zip file. Package library: ICSharpCode.SharpZipLib.Zip is part of the SharpZipLib package library.