DotNetZipLib is one of the best compression/zip libraries for dotnet I found. You can download it from CodePlex.

Here is a snippet which demonstrates how to use DotNetZipLib to zip and force download directly to browser.

ZipFile zip = new ZipFile();
 
// Add files to zip
zip.AddFile(@"C:\blah.jpg", string.Empty);
zip.AddFile(@"C:\blee.jpg", string.Empty);
 
// save to memory stream instead of a direct file
MemoryStream ms = new MemoryStream();
zip.Save(ms);
 
//convert to bytes
byte[] bytes = ms.ToArray();
ms.Flush();
ms.Close();
 
// Download now
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=myzipfile.zip");
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(bytes);
VN:F [1.1.6_502]
Rating: 0.0/5 (0 votes cast)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

Related posts:

  1. Download any file from ASP.NET
  2. UTF7 Converter
  3. Top 10 users on CommunityServer
  4. Find your lucky number in c#
  5. Get currently logged in user from a service running under LocalSystem account