hgotua
Last Updated: February 25, 2016
·
8.24K
· iondrimba
178548

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);
       }
   }
}
Say Thanks
Respond