asp.net mvc_dotnetzip simple use method, solve the problem of file compression _ practical skills

Source: Internet
Author: User

Preparatory work:

Find NuGet in the VS tool bar


Download DotNetZip

Now you can use the DotNetZip powerful class library, where I give some simple use.

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

One of the "System.Text.Encoding.Default" is to solve the problem of garbled Chinese.

You can literally understand zip. AddFile is to add files from the specified path to the zip, followed by the parameters "Images" and "files" that is, after the decompression to see two directories.

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

After decompression

You can do this if the files are in one directory:

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

Here's the encryption.

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

Above this article asp.net mvc_dotnetzip simple use method, solves the file compression question is the small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

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.