Skip to content

Instantly share code, notes, and snippets.

@fex80
Last active September 30, 2021 13:57
Show Gist options
  • Save fex80/10478783 to your computer and use it in GitHub Desktop.
Save fex80/10478783 to your computer and use it in GitHub Desktop.
Return a zip file on the fly from a ASP.NET WebApi ApiController
using Ionic.Zip; // from NUGET-Package "DotNetZip"
public HttpResponseMessage AllZipped()
{
using (var zipFile = new ZipFile())
{
// zipFile.AddEntry(...);
// zipFile.AddEntry(...);
return ZipContentResult(zipFile);
}
}
protected HttpResponseMessage ZipContentResult(ZipFile zipFile)
{
// inspired from http://stackoverflow.com/a/16171977/92756
var pushStreamContent = new PushStreamContent((stream, content, context) =>
{
zipFile.Save(stream);
stream.Close(); // After save we close the stream to signal that we are done writing.
}, "application/zip");
return new HttpResponseMessage(HttpStatusCode.OK) {Content = pushStreamContent};
}
@rabindra0388
Copy link

How can we rename file name lets say testZip.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment