asp.net 文件压缩zip下载






 今天分享下昨天做的一个东西 asp.net 的文件  zip 批量下载,首先你需要去 到http://dotnetzip.codeplex.com这个站点下载zip 的包,在里面找到 Ionic.Zip.dll  引用到你的项目中去

/// <summary> /// 批量zip下载 /// </summary> /// <param name="Listimg">这里Listimg 是一个数组类型</param> public void CreateZip(string Listimg) { string[] imgs = Listimg.Split(','); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.BufferOutput = false; //网站文件生成一个readme.txt的自述文件(可以不写) String readmeText = String.Format("README.TXT" +Environment.NewLine+"网址址:http://www.aicoffees.com" ); HttpContext.Current.Response.ContentType = "application/zip";//以zip 形式输出 HttpContext.Current.Response.AddHeader("content-disposition", "inline; filename="Photo.zip");//压缩下载的名字 //批量压缩操作 using (ZipFile zip = new ZipFile()) { for (int i = 0; i < imgs.Length; i++) { ///在压缩包内添加上面的自述文件,文字编码是系统默认编码形式 zip.AddEntry("Readme.txt", readmeText, Encoding.Default); zip.Password = "www.aicoffees.com";//给压缩包设置密码 zip.Encryption = EncryptionAlgorithm.WinZipAes256;//加密方式 zip.AddFile(HttpContext.Current.Server.MapPath(imgs[i].ToString()), "");//这里"" 我给的是空就是压缩时不设置文件夹,如果需要取什么名字只需要在“”里面加上就可以了,这里是一个重载方法,如果 zip.AddFile(HttpContext.Current.Server.MapPath(imgs[i].ToString()), "");这样写的话,zip 就会默认把你的image从根目录一直压缩到你的文件所在目录。 } zip.Save(HttpContext.Current.Response.OutputStream); } HttpContext.Current.Response.Close(); }

以上是自己的一点小总结,come on

原文地址:https://www.cnblogs.com/yiliuyang/p/4253243.html