ASP.Net MVC_DotNetZip简单使用方法,解决文件压缩的问题

2025-05-29 0 84

准备工作:

在vs工具栏中找到NuGet

ASP.Net MVC_DotNetZip简单使用方法,解决文件压缩的问题

下载DotNetZip

ASP.Net MVC_DotNetZip简单使用方法,解决文件压缩的问题

现在就可以使用DotNetZip强大的类库了,在这里我给出一些简单的使用。

?

1

2

3

4

5

6

7

8

9

10

11
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文件到某个目录。

ASP.Net MVC_DotNetZip简单使用方法,解决文件压缩的问题解压后 ASP.Net MVC_DotNetZip简单使用方法,解决文件压缩的问题

要是文件都在一个目录的话还可以这样:

?

1

2

3

4

5

6

7

8

9

10
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");

}

}

下面是加密

?

1

2

3

4

5

6

7

8

9

10

11
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简单使用方法,解决文件压缩的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 ASP.Net MVC_DotNetZip简单使用方法,解决文件压缩的问题 https://www.kuaiidc.com/100528.html

相关文章

发表评论
暂无评论