官方網(wǎng)站:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
插件描述:?ICSharpCode.SharpZipLib.dll 是一個(gè)完全由c#編寫的Zip, GZip, Tar and BZip2 library,可以方便地支持這幾種格式的壓縮解壓縮, SharpZipLib 的許可是經(jīng)過修改的GPL,底線是允許用在不開源商業(yè)軟件中,意思就是免費(fèi)使用。
一、在ThinksKing的Plugins里面找到已經(jīng)解壓好的SharpZipLib,使用net-20文件夾中的ICSharpCode.SharpZipLib.dll 。添加至項(xiàng)目引用中。
二、操作指南:
1.1????????? 創(chuàng)建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 , “”);
最后一個(gè)參數(shù)是使用正則表達(dá)式表示的過濾文件規(guī)則。CreateZip方法有3個(gè)重載版本,其中有目錄過濾參數(shù)、文件過濾參數(shù)及用于指定是否進(jìn)行子目錄遞歸的一個(gè)bool類型的參數(shù)。
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文件中的一個(gè)文件
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類包含了幾個(gè)Stream處理輔助方法:
1) Copy(Stream, Stream, Byte[])用于從一個(gè)Stream對象中復(fù)制數(shù)據(jù)到另一Stream對象。有多個(gè)重寫。
2) ReadFully(Stream, Byte [])用于從Stream對象中讀取所有的byte數(shù)據(jù)。有多個(gè)重寫。
三、幫助文檔
在SharpZipLib中有SharpZipLib_0860.chm官方幫助文檔。
參考鏈接:在路上的博文:《ICSharpCode.SharpZipLib 插件使用示例》
?
?
在處理后臺(tái)附件上載由于文件較多,需要每個(gè)文件單獨(dú)上傳關(guān)鍵是有些文件數(shù)據(jù)量比較少 也需要單獨(dú)上傳,這樣導(dǎo)致后臺(tái)數(shù)據(jù)流量較大而且用戶操作麻煩.
在處理這方面業(yè)務(wù)時(shí),可以簡化:首先驗(yàn)證用戶上傳文件的大小,設(shè)定不超過1M文件為限制并記錄,當(dāng)用戶點(diǎn)擊一次操作時(shí)后臺(tái)程序把所有小數(shù)據(jù)量文件進(jìn)行壓縮成一個(gè)單獨(dú)文件來上傳,這樣簡化用戶操作難度 增強(qiáng)用戶體驗(yàn),在獲得上載文件時(shí)同樣把這個(gè)文件進(jìn)行解壓本地即可... 使用ICSharpCode.SharpZipLib-(C#)實(shí)現(xiàn)解壓縮文件的操作類: 完整代碼如下 A:ICSharpCode.SharpZipLib.DLL組件下載地址,如果要實(shí)現(xiàn)必須在項(xiàng)目中引用該組件DLL 下載地址:http://good.gd/203866.htm B:完整的操作類代碼實(shí)例:
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實(shí)現(xiàn)解壓縮 /// Author:chenkai Time:2009年7月13日22:03:27 /// Version:Beta1.0.0-(測試版) /// </summary> public class ICSharpZipCodeTest{ /**/ /// <summary> /// 實(shí)現(xiàn)壓縮功能 /// </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 { // 使用正則表達(dá)式-判斷壓縮文件路徑 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 " 壓縮文件的路徑有誤! " ;} // 創(chuàng)建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);} // 設(shè)置CompressionLevel newzipstream.SetLevel(CompressionLevel);
// -查看0 - means store only to 9 - means best compression // 執(zhí)行壓縮 foreach (
string filename
in filenameToZip){FileStream newstream = File.OpenRead(filename);
// 打開預(yù)壓縮文件 // 判斷路徑 if (!
newRegex.Match(Zipedfiledname).Success){File.Delete(Zipedfiledname); return " 壓縮文件目標(biāo)路徑不存在! " ;} byte [] setbuffer=
new byte [newstream.Length];newstream.Read(setbuffer, 0 ,setbuffer.Length);
// 讀入文件 // 新建ZipEntrity ZipEntry newEntry =
new ZipEntry(filename); // 設(shè)置時(shí)間-長度 newEntry.DateTime =
DateTime.Now;newEntry.Size =
newstream.Length; newstream.Close(); newzipstream.PutNextEntry(newEntry);
// 壓入 newzipstream.Write(setbuffer,
0 ,setbuffer.Length);} // 重復(fù)壓入操作
newzipstream.Finish();newzipstream.Close();} catch (Exception e){ // 出現(xiàn)異常
File.Delete(Zipedfiledname); return e.Message.ToString();} return "" ;}
/**/ /// <summary> /// 實(shí)現(xiàn)解壓操作 /// </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 " 待解壓文件路徑不存在! " ;} // 創(chuàng)建ZipInputStream ZipInputStream newinStream =
new ZipInputStream(File.OpenRead(zipfilename)); // 判斷Password if (password !=
null && password.Length >
0 ){newinStream.Password =
password;}
// 執(zhí)行解壓操作 try {ZipEntry theEntry;
// 獲取Zip中單個(gè)File while ((theEntry = newinStream.GetNextEntry()) !=
null ){ // 判斷目的路徑 if (Directory.Exists(UnZipDir)){Directory.CreateDirectory(UnZipDir); // 創(chuàng)建目的目錄
}
// 獲得目的目錄信息 string Driectoryname =
Path.GetDirectoryName(UnZipDir); string pathname = Path.GetDirectoryName(theEntry.Name);
// 獲得子級目錄 string filename = Path.GetFileName(theEntry.Name);
// 獲得子集文件名 // 處理文件盤符問題 pathname = pathname.Replace(
" : " ,
" $ " );
// 處理當(dāng)前壓縮出現(xiàn)盤符問題 Driectoryname = Driectoryname +
" \\ " +
pathname;
// 創(chuàng)建
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 ){ // 寫入數(shù)據(jù) 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#)實(shí)現(xiàn)解壓縮文件的操作類》
轉(zhuǎn)載于:https://www.cnblogs.com/rainbow70626/p/4559691.html
總結(jié)
以上是生活随笔 為你收集整理的ICSharpCode.SharpZipLib 开源压缩库使用示例 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。