2018-06-08
工具:Visual Studio 2017 v15.7.3
官方網站
http://icsharpcode.github.io/SharpZipLib/
專案網址
http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
https://github.com/icsharpcode/SharpZipLib
(下圖) 這裡有點特別,一般 NuGet 安裝的套件,名稱會和 .dll 檔案一致,但 SharpZipLib 0.86.0 產生的 dll 稱為 ICSharpCode.SharpZipLib.dll,而非 SharpZipLib.dll。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form> </body> </html> |
Default.aspx.cs
using ICSharpCode.SharpZipLib.Zip; using System; using System.IO; namespace WebApplication1 { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string args1 = Server.MapPath("/Zip"); string args2 = Server.MapPath("/Download/") + "test.zip"; string[] filenames = Directory.GetFiles(args1); using (ZipOutputStream s = new ZipOutputStream(File.Create(args2))) { s.SetLevel(9); // 0 - store only to 9 - means best compression byte[] buffer = new byte[4096]; foreach (string file in filenames) { ZipEntry entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } s.Finish(); s.Close(); } } } } |
執行後,按下按鈕,到 Download 目錄應該可以看到 test.zip 檔案。
(完)
相關
[研究][ASP.NET]用 .NET 4.5做壓縮、解壓縮
http://shaurong.blogspot.com/2018/06/aspnet-net-40.html
[研究][ASP.NET]用 SharpZipLib 0.86.0 做壓縮、解壓縮 (NuGet 安裝)
http://shaurong.blogspot.com/2018/06/aspnet-sharpziplib-0860-nuget.html
[研究] [C#] 用 SharpZip 0.86.0 做壓縮、解壓縮
http://shaurong.blogspot.com/2016/12/c-sharpzip.html
[研究] [C#] 用 DotNetZip Library v1.9.1.8 解壓縮程式庫
http://shaurong.blogspot.com/2016/12/dotnetzip-library.html
沒有留言:
張貼留言