日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

ICSharpCode.SharpZipLib 开源压缩库使用示例

發(fā)布時(shí)間:2024/7/19 65 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ICSharpCode.SharpZipLib 开源压缩库使用示例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

官方網(wǎng)站:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx

插件描述:?ICSharpCode.SharpZipLib.dll 是一個(gè)完全由c#編寫(xiě)的Zip, GZip, Tar and BZip2 library,可以方便地支持這幾種格式的壓縮解壓縮, SharpZipLib 的許可是經(jīng)過(guò)修改的GPL,底線是允許用在不開(kāi)源商業(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ò)濾文件規(guī)則。CreateZip方法有3個(gè)重載版本,其中有目錄過(guò)濾參數(shù)、文件過(guò)濾參數(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對(duì)象中復(fù)制數(shù)據(jù)到另一Stream對(duì)象。有多個(gè)重寫(xiě)。

  2) ReadFully(Stream, Byte [])用于從Stream對(duì)象中讀取所有的byte數(shù)據(jù)。有多個(gè)重寫(xiě)。

三、幫助文檔

  在SharpZipLib中有SharpZipLib_0860.chm官方幫助文檔。

參考鏈接:在路上的博文:《ICSharpCode.SharpZipLib 插件使用示例》

?


?

在處理后臺(tái)附件上載由于文件較多,需要每個(gè)文件單獨(dú)上傳關(guān)鍵是有些文件數(shù)據(jù)量比較少 也需要單獨(dú)上傳,這樣導(dǎo)致后臺(tái)數(shù)據(jù)流量較大而且用戶操作麻煩.

在處理這方面業(yè)務(wù)時(shí),可以簡(jiǎn)化:首先驗(yàn)證用戶上傳文件的大小,設(shè)定不超過(guò)1M文件為限制并記錄,當(dāng)用戶點(diǎn)擊一次操作時(shí)后臺(tái)程序把所有小數(shù)據(jù)量文件進(jìn)行壓縮成一個(gè)單獨(dú)文件來(lái)上傳,這樣簡(jiǎn)化用戶操作難度 增強(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-(測(cè)試版)/// </summary>public class ICSharpZipCodeTest{ /**//// <summary>/// 實(shí)現(xiàn)壓縮功能/// </summary>/// <param name="filenameToZip">要壓縮文件(絕對(duì)文件路徑)</param>/// <param name="Zipedfiledname">壓縮(絕對(duì)文件路徑)</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)建ZipFileOutPutStreamZipOutputStream newzipstream = new ZipOutputStream(File.Open(Zipedfiledname,FileMode.OpenOrCreate));//判斷Passwordif (password != null && password.Length > 0){newzipstream.Password = password;} if (comment != null && comment.Length > 0){newzipstream.SetComment(comment);} //設(shè)置CompressionLevelnewzipstream.SetLevel(CompressionLevel); //-查看0 - means store only to 9 - means best compression //執(zhí)行壓縮foreach (string filename in filenameToZip){FileStream newstream = File.OpenRead(filename);//打開(kāi)預(yù)壓縮文件//判斷路徑if (!newRegex.Match(Zipedfiledname).Success){File.Delete(Zipedfiledname);return "壓縮文件目標(biāo)路徑不存在!";} byte[] setbuffer=new byte[newstream.Length];newstream.Read(setbuffer,0,setbuffer.Length);//讀入文件//新建ZipEntrityZipEntry newEntry = new ZipEntry(filename); //設(shè)置時(shí)間-長(zhǎng)度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)建ZipInputStreamZipInputStream newinStream = new ZipInputStream(File.OpenRead(zipfilename));//判斷Passwordif (password != null && password.Length > 0){newinStream.Password = password;} //執(zhí)行解壓操作try{ZipEntry theEntry; //獲取Zip中單個(gè)Filewhile ((theEntry = newinStream.GetNextEntry()) != null){//判斷目的路徑if (Directory.Exists(UnZipDir)){Directory.CreateDirectory(UnZipDir);//創(chuàng)建目的目錄 } //獲得目的目錄信息string Driectoryname = Path.GetDirectoryName(UnZipDir);string pathname = Path.GetDirectoryName(theEntry.Name);//獲得子級(jí)目錄string filename = Path.GetFileName(theEntry.Name);//獲得子集文件名//處理文件盤(pán)符問(wèn)題pathname = pathname.Replace(":", "$");//處理當(dāng)前壓縮出現(xiàn)盤(pán)符問(wè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){//寫(xiě)入數(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 开源压缩库使用示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。