打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ASP.Net MVC

准备工作:

 在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");            }        }

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# .NET 使用第三方类库DotNetZip解压/压缩Zip文件 (ZT)
如何在Java中加密和解密zip文件?
寻求*.docx,*.xlsx,*.pptx文件下载后变成.zip文件的解决办法
C# 文件操作(上传 下载 删除 文件列表...) - 《八零年代》.NET之家 - 博客...
如何用Python读取Excel中图片?又如何用Python往Excel中写入图片?
5个有趣的Python脚本,建议收藏!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服