Last Updated: July 26, 2021
·
45.73K
· iondrimba

Simple way to Zip files with C# .NET (Framework 4.5 /4.6)

  using System.IO.Compression;

  public void ZipImages()
  {

     string zipCreatePath = System.Web.Hosting.HostingEnvironment.MapPath("/myZip.zip");

   using(ZipArchive archive = ZipFile.Open(zipCreatePath, ZipArchiveMode.Create))
   {
       List<string> files = new List<string>();
       files.Add("wallhaven-103536.jpg");
       files.Add("wallhaven-161383.jpg");
       files.Add("wallhaven-30764.jpg");

       foreach(string file in files)
       {
           string filePath = System.Web.Hosting.HostingEnvironment.MapPath(string.Format("/images/{0}", file));

           archive.CreateEntryFromFile(filePath, file);
       }
   }
}