您现在的位置是:网站首页> 编程资料编程资料
ASP.Net MVC_DotNetZip简单使用方法,解决文件压缩的问题_实用技巧_
2023-05-24
286人已围观
简介 ASP.Net MVC_DotNetZip简单使用方法,解决文件压缩的问题_实用技巧_
准备工作:
在vs工具栏中找到NuGet

下载DotNetZip

现在就可以使用DotNetZip强大的类库了,在这里我给出一些简单的使用。
public ActionResult Export() { using (ZipFile zip = new ZipFile(System.Text.Encoding.Default)) { zip.AddFile(Server.MapPath("~/Img/2.png"), "Images"); zip.AddFile(Server.MapPath("~/File/1.pdf"), "Files"); zip.Save(Server.MapPath("~/ZIP/Test.zip")); return File(Server.MapPath("~/ZIP/Test.zip"), "application/zip", "sample.zip"); } }其中“System.Text.Encoding.Default”是解决中文乱码问题。
从字面上就可以理解zip.AddFile就是从指定路径把文件加入到zip中,后面的参数“Images"和“Files”就是说解压后看到了两个目录。
zip.Sava就是保存zip文件到某个目录。
解压后 
要是文件都在一个目录的话还可以这样:
public ActionResult Export() { using (ZipFile zip = new ZipFile()) { zip.AddDirectory(Server.MapPath("~/Img/")); zip.Save(Server.MapPath("~/ZIP/Test.zip")); return File(Server.MapPath("~/ZIP/Test.zip"), "application/zip", "sample.zip"); } }下面是加密
public ActionResult Export() { using (ZipFile zip = new ZipFile()) { zip.Password="123"; zip.AddDirectory(Server.MapPath("~/Img/")); zip.Save(Server.MapPath("~/ZIP/Test.zip")); return File(Server.MapPath("~/ZIP/Test.zip"), "application/zip", "sample.zip"); } }以上这篇ASP.Net MVC_DotNetZip简单使用方法,解决文件压缩的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
相关内容
- 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式问题的方法_实用技巧_
- ASP.NET中控件的EnableViewState属性及彻底禁用_实用技巧_
- ASP.NET中基于soaphead的webservice安全机制_实用技巧_
- ASP.NET中Webservice安全 实现访问权限控制_实用技巧_
- Microsoft .Net Remoting系列教程之三:Remoting事件处理全接触_自学过程_
- Microsoft .Net Remoting系列教程之二:Marshal、Disconnect与生命周期以及跟踪服务_自学过程_
- Microsoft .Net Remoting系列教程之一:.Net Remoting基础篇_自学过程_
- visual studio 2012安装配置方法图文教程 附opencv配置教程_实用技巧_
- ASP.NET配置KindEditor文本编辑器图文教程_实用技巧_
- 《解剖PetShop》之六:PetShop之表示层设计_自学过程_
