• 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");
                }
            }
  • 相关阅读:
    笔记:一篇关于容器和虚拟机的对比
    语义化版本说明脑图
    KiCad EDA 5.1.4 发布了
    KiCad 5.1.4 无法覆铜?
    mac 常用的终端命令
    PC 商城扫描二维码登录
    Git的撤销与回滚
    springboot 集成elasticsearch5.4.3
    redis 缓存类型为map
    基于Elasticsearch 5.4.3的商品搜索系统
  • 原文地址:https://www.cnblogs.com/zuochengsi-9/p/5548643.html
Copyright © 2020-2023  润新知