How to create a Zip file with the .NET 4.5 System.IO.Compression classes in C#
June 13, 2013
0 Comments
If you want to create a zip file in C# with the OOB BCL, in C# you can use the following code:
public void CreateZipFile() { string zipPath = @"C:\Test.zip"; string entryName = "Readme.txt"; string content = "Hello world!"; using (var zipToOpen = new System.IO.FileStream(zipPath, System.IO.FileMode.CreateNew)) { using (var archive = new System.IO.Compression.ZipArchive(zipToOpen, System.IO.Compression.ZipArchiveMode.Create)) { System.IO.Compression.ZipArchiveEntry readmeEntry = archive.CreateEntry(entryName); using (var writer = new System.IO.StreamWriter(readmeEntry.Open())) { writer.Write(content); } } } }Tags: C#