首先,在 http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 下載源碼,找到“ZipConstants.cs”修改
public static string ConvertToString(byte[] data) { ??????return Encoding.GetEncoding("gb2312").GetString(data, 0, data.Length); ??????//return Encoding.ASCII.GetString(data,0, data.Length); } ?? public static byte[] ConvertToArray(string str) { ???????return Encoding.GetEncoding("gb2312").GetBytes(str); ?????? //return Encoding.ASCII.GetBytes(str); }
如此就可支持中文名稱了 以下是我寫的壓縮與解壓縮的代碼:
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using ICSharpCode.SharpZipLib.Zip;
namespace OA { ???????? /// <summary> ???????? /// WebForm1 的摘要說明。 ???????? /// </summary> ???????? public class WebForm1 : System.Web.UI.Page ???????? { ??????????????public string ServerDir; ??????????????private void Page_Load(object sender, System.EventArgs e) ?????????????? { ????????????????// 在此處放置用戶代碼以初始化頁面 ?????????????????????this.ServerDir = Page.MapPath("."); ?????????????????????this.ZipFile("01.txt*02.txt*000.zip");??//只是示例,具體的大家自己去實現(xiàn) ?????????????????????this.ZipFile("01.txt*02.txt*001.zip"); ?????????????????????this.UnZipFile("000.zip*001.zip"); ???????? }
???????? public string ShortDir(string s) ???????? { ????????????? //將文件的絕對路徑轉為相對路徑 ???????????????string d=s.Replace(ServerDir,""); ???????????????return d; ???????? }
???????? //壓縮文件 p 為客戶端傳回來的文件列表:文件名+壓縮包的名稱 ??????????public void ZipFile(string p) ?????????? { ???????????????????string[] tmp = p.Split(new char[]{'*'});??//分離文件列表 ???????????????????if(tmp[tmp.Length-1]!="")??//壓縮包名稱不為空 ???????????????????? { ?????????????????????????? ZipOutputStream u = new ZipOutputStream(File.Create(ServerDir+tmp[tmp.Length-1]));????????????//新建壓縮文件流 “ZipOutputStream” ??????????????????????????for(int i =0;i<tmp.Length-1;i++) ?????????????????????????? { ?????????????????????????????????if(tmp[i]!="")??//分離出來的文件名不為空 ????????????????????????????????? { ??????????????????????????????????????????this.AddZipEntry(tmp[i],u,out u); //向壓縮文件流加入內容 ????????????????????????????????? } ??????????????????????????? } ??????????????????????????? u.Finish(); // 結束壓縮 ??????????????????????????? u.Close(); ????????????????????? } ???????????? }
???????????? //添加壓縮項目:p 為需壓縮的文件或文件夾; u 為現(xiàn)有的源ZipOutputStream;??out j為已添加“ZipEntry”的“ZipOutputStream” ????????????? public void AddZipEntry(string p,ZipOutputStream u,out ZipOutputStream j) ????????????? { ???????????????????string s =ServerDir+p; ???????????????????if(Directory.Exists(s)) //文件夾的處理 ??????????????????? { ????????????????????????? DirectoryInfo di = new DirectoryInfo(s);
???????????????????????? //***********以下內容是修訂后添加的*********** ?????????????????????????if(di.GetDirectories().Length<=0)?? //沒有子目錄 ?????????????????????????? { ???????????????????????????????????? ZipEntry z = new ZipEntry(p+""""); //末尾“""”用于文件夾的標記 ????????????????????????????????????? u.PutNextEntry(z); ??????????????????????????? } ???????????????????????????? //***************以上內容是修訂后添加的***************
???????????????????????????foreach(DirectoryInfo tem in di.GetDirectories()) //獲取子目錄 ???????????????????????????? { ???????????????????????????????????? ZipEntry z = new ZipEntry(this.ShortDir(tem.FullName)+""""); //末尾“""”用于文件夾的標記 ????????????????????????????????????? u.PutNextEntry(z);??? //此句不可少,否則空目錄不會被添加 ????????????????????????????????????? s = this.ShortDir(tem.FullName); ?????????????????????????????????????this.AddZipEntry(s,u,out u);?????? //遞歸 ??????????????????????????? } ????????????????????????????foreach(FileInfo temp in di.GetFiles())??//獲取此目錄的文件 ??????????????????????????? { ????????????????????????????????????? s = this.ShortDir(temp.FullName); ?????????????????????????????????????this.AddZipEntry(s,u,out u);?????? //遞歸 ??????????????????????????? } ????????????????????? } ?????????????????????else if(File.Exists(s)) //文件的處理 ????????????????????? { ??????????????????????????????? u.SetLevel(9);??????//壓縮等級 ???????????????????????????????? FileStream f = File.OpenRead(s); ????????????????????????????????byte[] b = new byte[f.Length]; ???????????????????????????????? f.Read(b,0,b.Length);?????????? //將文件流加入緩沖字節(jié)中 ??????????????????????????????? ZipEntry z = new ZipEntry(this.ShortDir(s)); ??????????????????????????????? u.PutNextEntry(z);????????????? //為壓縮文件流提供一個容器 ??????????????????????????????? u.Write(b,0,b.Length); //寫入字節(jié) ??????????????????????????????? f.Close(); ?????????????????????? } ?????????????????????? j=u;??? //返回已添加數(shù)據(jù)的“ZipOutputStream” ?????????????? }
?????????????? public void UnZipFile(string p)???//解壓縮 ?????????????? { ???????????????????string[] un_tmp = p.Split(new char[]{'*'}); ???????????????????int i2=0;??//防止名稱沖突的參數(shù) ???????????????????for(int j=0;j<un_tmp.Length;j++) ??????????????????? { ?????????????????????????if(un_tmp[j]!="") ????????????????????????? { ?????????????????????????????????string un_time=System.DateTime.Now.ToShortDateString()+"-"+System.DateTime.Now.Hour.ToString()+"-"+System.DateTime.Now.Minute.ToString()+"-"+(System.DateTime.Now.Second+i2).ToString(); ?????????????????????????????????string un_dir =ServerDir+"Unzip-"+un_time; ????????????????????????????????? Directory.CreateDirectory(un_dir);???? //創(chuàng)建以解壓時間為名稱的文件夾 ????????????????????????????????? ZipInputStream f = new ZipInputStream(File.OpenRead(ServerDir+un_tmp[j])); //讀取壓縮文件,并用此文件流新建 “ZipInputStream”對象 ????????????????????????????????? A:?? ZipEntry zp = f.GetNextEntry(); ?? //獲取解壓文件流中的項目。 另注(我的理解):在壓縮包里每個文件都以“ZipEntry”形式存在,其中包括存放文件的目錄信息。如果空目錄被壓縮,該目錄下將出現(xiàn)一個名稱為空、 大小為 0 、“Crc”屬性為 00000000 的“文件”。此文件只是個標記,不會被解壓。 ?????????????????????????????????while(zp!=null) ????????????????????????????????? { ???????????????????????????????????????string un_tmp2; ???????????????????????????????????????? if(zp.Name.IndexOf("""")>=0) //獲取文件的目錄信息 ???????????????????????????????????????? { ?????????????????????????????????????????????????int tmp1 = zp.Name.LastIndexOf(""""); ????????????????????????????????????????????????? un_tmp2 = zp.Name.Substring(0,tmp1); ????????????????????????????????????????????????? Directory.CreateDirectory(un_dir+""""+un_tmp2+""""); //必須先創(chuàng)建目錄,否則解壓失敗 --- (A) 關系到下面的步驟(B) ???????????????????????????????????????? } ?????????????????????????????????????????if(!zp.IsDirectory&&zp.Crc!=00000000L) //此“ZipEntry”不是“標記文件” ?????????????????????????????????????????? { ??????????????????????????????????????????????????int i =2048; ?????????????????????????????????????????????????byte[] b = new byte[i];??//每次緩沖 2048 字節(jié) ????????????????????????????????????????????????? FileStream s= File.Create(un_dir+""""+zp.Name); //(B)-新建文件流 ?????????????????????????????????????????????????while(true) //持續(xù)讀取字節(jié),直到一個“ZipEntry”字節(jié)讀完 ????????????????????????????????????????????????? { ???????????????????????????????????????????????????????? i = f.Read(b,0,b.Length); //讀取“ZipEntry”中的字節(jié) ?????????????????????????????????????????????????????????if(i>0) ????????????????????????????????????????????????????????? { ??????????????????????????????????????????????????????????????????? s.Write(b,0,i); //將字節(jié)寫入新建的文件流 ????????????????????????????????????????????????????????? } ?????????????????????????????????????????????????????????else ????????????????????????????????????????????????????????? { ???????????????????????????????????????????????????????????????????break; //讀取的字節(jié)為 0 ,跳出循環(huán) ????????????????????????????????????????????????????????? } ????????????????????????????????????????????????? } ????????????????????????????????????????????????? s.Close(); ??????????????????????????????????????????? } ????????????????????????????????????????????goto A; //進入下一個“ZipEntry” ????????????????????????????????????? } ????????????????????????????????????? f.Close(); ????????????????????????????????????? i2++; ????????????????????????? } ???????????????????? } ?????????????? } ???????? } } |