您好,登錄后才能下訂單哦!
這篇文章給大家介紹MVC_DotNetZip如何在ASP.Net項目中使用,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
準備工作:
在vs工具欄中找到NuGet
下載DotNetZip
現(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文件到某個目錄。
解壓后
要是文件都在一個目錄的話還可以這樣:
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項目中使用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。