If you search for "generate zip in ASP.NET/C#" you get tons of blog posts and StackOverflow answers suggesting DotNetZip, SharpZipLib and other 3rd party archiving libraries. And up until now that was the only solution - all because .NET had no built-in support for zip format.
But .NET Framework 4.5 and later (finally) includes native tools for working with Zip archives - ZipArchive
class from the System.IO.Compressions
namespace.
Here's an example of how you can dynamically generate a zip file inside an ASP.NET MVC action method (comes handy if you want to return multiple files within one response, for example):
I've just added two images files (in a form of byte arrays) into one zip-archive and returned it to the user. GetImage1Bytes
and GetImage2Bytes
are simply stubs for your code here. Nice and easy. But the best part - you're not using any external dependencies, all pure .NET.