ASP. Net MVC -- DotNetZip is simple to use to solve the File compression problem ., Mvcdotnetzip

Source: Internet
Author: User

ASP. Net MVC -- DotNetZip is simple to use to solve the File compression problem ., Mvcdotnetzip

Preparations:

Find NuGet in the vs Toolbar

 

Download DotNetZip

Now we can use the powerful DotNetZip class library. Here I will give some simple usage.

    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" solves Chinese garbled characters.

Literally, we can understand that zip. AddFile adds the file to the zip file from the specified path. The following parameters "Images" and "Files" mean that two directories are displayed after decompression.

Zip. Sava is used to save the zip file to a directory.

Decompress the package

If all files are in a directory, you can also do this:

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

 

Encryption is as follows:

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.