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