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.