使用C#.NET压缩文件的简单方法(Framework 4.5/4.6)(示例)

・1 分钟阅读


 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);
 }
 }
}
Jiaoyang75 profile image