将.net 项目中的文件夹压缩下载

 

 参考:https://www.cnblogs.com/zuochengsi-9/p/5548643.html

 

在vs工具栏中找到NuGet,右键NuGet程序包管理,安装DotNetZip

using一下ZipFile,然后直接在控制器写一下代码就行了,

注意:System.Text.Encoding.Default是解决中文乱码,否则即使下载成功但也无法解压

 using (ZipFile zip = new ZipFile(System.Text.Encoding.Default))
            {
                //加密
                zip.Password = "123";
                string zippath = "~/Controllers/";
                zip.AddDirectory(Server.MapPath(zippath));
                zip.Save(Server.MapPath("~/ZIP/压缩后保存在项目中的文件名.zip"));
                return File(Server.MapPath("~/ZIP/压缩后保存在项目中的文件名.zip"),
                                           "application/zip", "下载后的文件名.zip");
            }

  

posted @ 2019-04-08 23:34  风葬秋暝  阅读(570)  评论(0编辑  收藏  举报