溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MVC_DotNetZip如何在ASP.Net項目中使用

發(fā)布時間:2020-12-09 15:39:37 來源:億速云 閱讀:161 作者:Leah 欄目:開發(fā)技術

這篇文章給大家介紹MVC_DotNetZip如何在ASP.Net項目中使用,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

準備工作:

 在vs工具欄中找到NuGet

 MVC_DotNetZip如何在ASP.Net項目中使用

下載DotNetZip

MVC_DotNetZip如何在ASP.Net項目中使用

現(xiàn)在就可以使用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中,后面的參數(shù)“Images"和“Files”就是說解壓后看到了兩個目錄。

zip.Sava就是保存zip文件到某個目錄。

MVC_DotNetZip如何在ASP.Net項目中使用 解壓后    MVC_DotNetZip如何在ASP.Net項目中使用

要是文件都在一個目錄的話還可以這樣:

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

關于MVC_DotNetZip如何在ASP.Net項目中使用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI