If you're working with ASP.NET or ASP.NET Core and you need to create a zip file on-the-fly within your controller - for example to allow the user to download multiple files with a single request - you can easily do that using the ZipArchive class, introduced in Framework 4.5.
Here's a quick code sample using a MemoryStream and a couple of byte arrays representing two files:
byte file1 = GetFile1ByteArray();
byte file2 = GetFile2ByteArray();
using (MemoryStream ms = new MemoryStream())
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
var zipArchiveEntry = archive.CreateEntry("file1.txt", CompressionLevel.Fastest);
using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(file1, 0, file1.Length);
zipArchiveEntry = archive.CreateEntry("file2.txt", CompressionLevel.Fastest);
using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(file2, 0, file2.Length);
return File(ms.ToArray(), "application/zip", "Archive.zip");
Depending on your scenario you can use it inside a MVC Controller returning an ActionResult, / FileContentResult, persist the MemoryStream to disk, replace it with a FileStream and so on: in the above example I used the Controller.File method (from the System.Web.Mvc namespace) to return a FileContentResult object. If you're not using the MVC Framework you cannot do that, but you can still handle the MemoryStream in a different way, such as using Response.OutputStream or other standard techniques.
Needless to say, you can change the CompressionLevel to better suit your needs in terms of speed vs size. Also, if you need to phisically create the zip archive you can either persist the MemoryStream to disk or entirely replace it with a FileStream.
That's it for now: happy archiving!