官方網站:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
插件描述: ICSharpCode.SharpZipLib.dll 是一個完全由c#編寫的Zip, GZip, Tar and BZip2 library,可以方便地支持這幾種格式的壓縮解壓縮, SharpZipLib 的許可是經過修改的GPL,底線是允許用在不開源商業軟件中,意思就是免費使用。
一、在ThinksKing的Plugins里面找到已經解壓好的SharpZipLib,使用net-20文件夾中的ICSharpCode.SharpZipLib.dll 。添加至項目引用中。
二、操作指南:
1.1 創建zip文件,並添加文件:
using (ZipFile zip = ZipFile.Create(@”E:\test.zip”)) { zip.BeginUpdate(); zip.Add(@”E:\文件1.txt”); zip.Add(@”E:\文件2.txt”); zip.CommitUpdate(); }
1.2 將文件夾壓縮為文件
(new FastZip()).CreateZip(@”E:\test.zip”, @”E:\test\”, true, “”);
最后一個參數是使用正則表達式表示的過濾文件規則。CreateZip方法有3個重載版本,其中有目錄過濾參數、文件過濾參數及用於指定是否進行子目錄遞歸的一個bool類型的參數。
1.3 將文件添加到已有zip文件中
using (ZipFile zip = new ZipFile(@”E:\test.zip”)) { zip.BeginUpdate(); zip.Add(@”E:\test.doc”); zip.CommitUpdate(); }
1.4 列出zip文件中文件
using (ZipFile zip = new ZipFile(@”E:\test.zip”)) { string list = string.Empty; foreach (ZipEntry entry in zip) { list += entry.Name + “\r\n”; } MessageBox.Show(list); }
1.5 刪除zip文件中的一個文件
using (ZipFile zip = new ZipFile(@”E:\test.zip”)) { zip.BeginUpdate(); zip.Delete(@”test.doc”); zip.Delete(@”test22.txt”); zip.CommitUpdate(); }
1.6 解壓zip文件中文件到指定目錄下
(new FastZip()).ExtractZip(@”E:\test.zip”, @”E:\test\”, “”);
1.7 常用類
ZipInputStream、GZipInputStream用於解壓縮Deflate、GZip格式流,ZipOutputStream、GZipOutputStream用於壓縮Deflate、GZip格式流。
StreamUtil類包含了幾個Stream處理輔助方法:
1) Copy(Stream, Stream, Byte[])用於從一個Stream對象中復制數據到另一Stream對象。有多個重寫。
2) ReadFully(Stream, Byte [])用於從Stream對象中讀取所有的byte數據。有多個重寫。
三、幫助文檔
在SharpZipLib中有SharpZipLib_0860.chm官方幫助文檔。
參考鏈接:在路上的博文:《ICSharpCode.SharpZipLib 插件使用示例》
在處理后台附件上載由於文件較多,需要每個文件單獨上傳關鍵是有些文件數據量比較少 也需要單獨上傳,這樣導致后台數據流量較大而且用戶操作麻煩.
在處理這方面業務時,可以簡化:首先驗證用戶上傳文件的大小,設定不超過1M文件為限制並記錄,當用戶點擊一次操作時后台程序把所有小數據量文件進行壓縮成一個單獨文件來上傳,這樣簡化用戶操作難度 增強用戶體驗,在獲得上載文件時同樣把這個文件進行解壓本地即可...
使用ICSharpCode.SharpZipLib-(C#)實現解壓縮文件的操作類: 完整代碼如下
A:ICSharpCode.SharpZipLib.DLL組件下載地址,如果要實現必須在項目中引用該組件DLL
下載地址:http://good.gd/203866.htm
B:完整的操作類代碼實例:
using System; using System.Collections.Generic; using System.Linq; using System.Web; //using the Compent Comspac using System.IO; using System.Text; using System.Threading; using ICSharpCode.SharpZipLib; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Checksums; namespace TestJqueryAjax { /**//// <summary> /// 使用ICSharpZipCode.Dll實現解壓縮 /// Author:chenkai Time:2009年7月13日22:03:27 /// Version:Beta1.0.0-(測試版) /// </summary> public class ICSharpZipCodeTest { /**//// <summary> /// 實現壓縮功能 /// </summary> /// <param name="filenameToZip">要壓縮文件(絕對文件路徑)</param> /// <param name="Zipedfiledname">壓縮(絕對文件路徑)</param> /// <param name="CompressionLevel">壓縮比</param> /// <param name="password">加密密碼</param> /// <param name="comment">壓縮文件描述</param> /// <returns>異常信息</returns> public static string MakeZipFile(string[] filenameToZip, string Zipedfiledname, int CompressionLevel, string password, string comment) { try { //使用正則表達式-判斷壓縮文件路徑 System.Text.RegularExpressions.Regex newRegex = new System.Text. RegularExpressions.Regex(@"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w ]*.*))"); if (!newRegex.Match(Zipedfiledname).Success) { File.Delete(Zipedfiledname); return "壓縮文件的路徑有誤!"; } //創建ZipFileOutPutStream ZipOutputStream newzipstream = new ZipOutputStream(File.Open(Zipedfiledname, FileMode.OpenOrCreate)); //判斷Password if (password != null && password.Length > 0) { newzipstream.Password = password; } if (comment != null && comment.Length > 0) { newzipstream.SetComment(comment); } //設置CompressionLevel newzipstream.SetLevel(CompressionLevel); //-查看0 - means store only to 9 - means best compression //執行壓縮 foreach (string filename in filenameToZip) { FileStream newstream = File.OpenRead(filename);//打開預壓縮文件 //判斷路徑 if (!newRegex.Match(Zipedfiledname).Success) { File.Delete(Zipedfiledname); return "壓縮文件目標路徑不存在!"; } byte[] setbuffer=new byte[newstream.Length]; newstream.Read(setbuffer,0,setbuffer.Length);//讀入文件 //新建ZipEntrity ZipEntry newEntry = new ZipEntry(filename); //設置時間-長度 newEntry.DateTime = DateTime.Now; newEntry.Size = newstream.Length; newstream.Close(); newzipstream.PutNextEntry(newEntry);//壓入 newzipstream.Write(setbuffer,0,setbuffer.Length); } //重復壓入操作 newzipstream.Finish(); newzipstream.Close(); }catch (Exception e) { //出現異常 File.Delete(Zipedfiledname); return e.Message.ToString(); } return ""; } /**//// <summary> /// 實現解壓操作 /// </summary> /// <param name="zipfilename">要解壓文件Zip(物理路徑)</param> /// <param name="UnZipDir">解壓目的路徑(物理路徑)</param> /// <param name="password">解壓密碼</param> /// <returns>異常信息</returns> public static string UnMakeZipFile(string zipfilename,string UnZipDir,string password) { //判斷待解壓文件路徑 if (!File.Exists(zipfilename)) { File.Delete(UnZipDir); return "待解壓文件路徑不存在!"; } //創建ZipInputStream ZipInputStream newinStream = new ZipInputStream(File.OpenRead(zipfilename)); //判斷Password if (password != null && password.Length > 0) { newinStream.Password = password; } //執行解壓操作 try { ZipEntry theEntry; //獲取Zip中單個File while ((theEntry = newinStream.GetNextEntry()) != null) { //判斷目的路徑 if (Directory.Exists(UnZipDir)) { Directory.CreateDirectory(UnZipDir);//創建目的目錄 } //獲得目的目錄信息 string Driectoryname = Path.GetDirectoryName(UnZipDir); string pathname = Path.GetDirectoryName(theEntry.Name);//獲得子級目錄 string filename = Path.GetFileName(theEntry.Name);//獲得子集文件名 //處理文件盤符問題 pathname = pathname.Replace(":", "$");//處理當前壓縮出現盤符問題 Driectoryname = Driectoryname + "\\" + pathname; //創建 Directory.CreateDirectory(Driectoryname); //解壓指定子目錄 if (filename != string.Empty) { FileStream newstream = File.Create(Driectoryname + "\\" + pathname); int size = 2048; byte[] newbyte = new byte[size]; while (true) { size = newinStream.Read(newbyte, 0, newbyte.Length); if (size > 0) { //寫入數據 newstream.Write(newbyte, 0, size); } else { break; } newstream.Close(); } } } newinStream.Close(); } catch (Exception se) { return se.Message.ToString(); } finally { newinStream.Close(); } return ""; } } }
參考鏈接:陳凱的博文:《使用ICSharpCode.SharpZipLib-(C#)實現解壓縮文件的操作類》