日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

SharpDevelop 开源的 C# IDE ! 和 SharpZipLib

發布時間:2025/5/22 C# 122 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SharpDevelop 开源的 C# IDE ! 和 SharpZipLib 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SharpDevelop 開源的 C# IDE ! 和 SharpZipLib

http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx

--------------------------------------------------

http://www.cnblogs.com/zengwei/archive/2007/08/22/865778.html

?

使用SharpZip壓縮與解壓縮的實戰經驗 2007-08-17 09:39
首先,在 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");??//只是示例,具體的大家自己去實現
?????????????????????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 為現有的源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);?????????? //將文件流加入緩沖字節中
??????????????????????????????? ZipEntry z = new ZipEntry(this.ShortDir(s));
??????????????????????????????? u.PutNextEntry(z);????????????? //為壓縮文件流提供一個容器
??????????????????????????????? u.Write(b,0,b.Length); //寫入字節
??????????????????????????????? f.Close();
?????????????????????? }
?????????????????????? j=u;??? //返回已添加數據的“ZipOutputStream”
?????????????? }


?????????????? public void UnZipFile(string p)???//解壓縮
?????????????? {
???????????????????string[] un_tmp = p.Split(new char[]{'*'});
???????????????????int i2=0;??//防止名稱沖突的參數
???????????????????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);???? //創建以解壓時間為名稱的文件夾
????????????????????????????????? ZipInputStream f = new ZipInputStream(File.OpenRead(ServerDir+un_tmp[j])); //讀取壓縮文件,并用此文件流新建 “ZipInputStream”對象

????????????????????????????????? A:?? ZipEntry zp = f.GetNextEntry(); ?? //獲取解壓文件流中的項目。 另注(我的理解):在壓縮包里每個文件都以“ZipEntry”形式存在,其中包括存放文件的目錄信息。如果空目錄被壓縮,該目錄下將出現一個名稱為空、 大小為 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+""""); //必須先創建目錄,否則解壓失敗 --- (A) 關系到下面的步驟(B)
???????????????????????????????????????? }
?????????????????????????????????????????if(!zp.IsDirectory&&zp.Crc!=00000000L) //此“ZipEntry”不是“標記文件”
?????????????????????????????????????????? {
??????????????????????????????????????????????????int i =2048;
?????????????????????????????????????????????????byte[] b = new byte[i];??//每次緩沖 2048 字節
????????????????????????????????????????????????? FileStream s= File.Create(un_dir+"
"""+zp.Name); //(B)-新建文件流
?????????????????????????????????????????????????while(true) //持續讀取字節,直到一個“ZipEntry”字節讀完
????????????????????????????????????????????????? {
???????????????????????????????????????????????????????? i = f.Read(b,0,b.Length); //讀取“ZipEntry”中的字節
?????????????????????????????????????????????????????????if(i>0)
????????????????????????????????????????????????????????? {
??????????????????????????????????????????????????????????????????? s.Write(b,0,i); //將字節寫入新建的文件流
????????????????????????????????????????????????????????? }
?????????????????????????????????????????????????????????else
????????????????????????????????????????????????????????? {
???????????????????????????????????????????????????????????????????break; //讀取的字節為 0 ,跳出循環
????????????????????????????????????????????????????????? }
????????????????????????????????????????????????? }
????????????????????????????????????????????????? s.Close();
??????????????????????????????????????????? }
????????????????????????????????????????????goto A; //進入下一個“ZipEntry”
????????????????????????????????????? }
????????????????????????????????????? f.Close();
????????????????????????????????????? i2++;
????????????????????????? }
???????????????????? }
?????????????? }
???????? }
}

總結

以上是生活随笔為你收集整理的SharpDevelop 开源的 C# IDE ! 和 SharpZipLib的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。