c#压缩解压缩_第1页
c#压缩解压缩_第2页
c#压缩解压缩_第3页
c#压缩解压缩_第4页
c#压缩解压缩_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、c#压缩和解压缩文件 使用SharpZip压缩与解压缩的实战经验 首先,在 下载源码,找到“ZipConstants.cs”修改public static string ConvertToString(byte data      return Encoding.GetEncoding("gb2312".GetString(data, 0, data.Length;      /return Encoding.ASCII.GetString(da

2、ta,0, data.Length;  public static byte ConvertToArray(string str       return Encoding.GetEncoding("gb2312".GetBytes(str;       /return Encoding.ASCII.GetBytes(str;如此就可支持中文名称了以下是我写的压缩与解压缩的代码:using System;using Syste

3、m.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       

4、;  /          / WebForm1 的摘要说明。         /          public class WebForm1 : System.Web.UI.Page              

5、60;        public string ServerDir;              private void Page_Load(object sender, System.EventArgs e              

6、                / 在此处放置用户代码以初始化页面                     this.ServerDir = Page.MapPath("."   

7、                  this.ZipFile("01.txt*02.txt*000.zip"  /只是示例,具体的大家自己去实现                   

8、0; this.ZipFile("01.txt*02.txt*001.zip"                     this.UnZipFile("000.zip*001.zip"              

9、;    public string ShortDir(string s                       /将文件的绝对路径转为相对路径               string d=s.Repla

10、ce(ServerDir,""               return d;                  /压缩文件 p 为客户端传回来的文件列表:文件名+压缩包的名称        

11、;  public void ZipFile(string p                              string tmp = p.Split(new char'*'  /分离文件列表    &

12、#160;              if(tmptmp.Length-1!=""  /压缩包名称不为空                           &

13、#160;                    ZipOutputStream u = new ZipOutputStream(File.Create(ServerDir+tmptmp.Length-1;            /新建压缩文件流 “ZipOutputStream”

14、0;                         for(int i =0;i                       &

15、#160;                                      if (tmpi!=""   /分离出来的文件名不为空      

16、;                                                  

17、0;                     this .AddZipEntry(tmpi,u, out u; /向压缩文件流加入内容                       

18、                                                   

19、0;                u.Finish(; / 结束压缩                             u.Close(;   

20、0;                                             /添加压缩项目:p 为需压缩的文件或文件夹; u 为现有的源ZipOutputStre

21、am;  out j为已添加“ZipEntry”的“ZipOutputStream”              public void AddZipEntry(string p,ZipOutputStream u,out ZipOutputStream j                 

22、                string s =ServerDir+p;                   if(Directory.Exists(s /文件夹的处理      

23、60;                                       DirectoryInfo di = new DirectoryInfo(s;     

24、0;                   /*以下内容是修订后添加的*                         if(di.GetDirectories(.Len

25、gth<=0   /没有子目录                                              &

26、#160;                 ZipEntry z = new ZipEntry(p+"""" /末尾“""”用于文件夹的标记                    

27、                  u.PutNextEntry(z;                               

28、;                          /*以上内容是修订后添加的*                      &#

29、160;    foreach(DirectoryInfo tem in di.GetDirectories( /获取子目录                                     &

30、#160;                            ZipEntry z = new ZipEntry(this.ShortDir(tem.FullName+"""" /末尾“""”用于文件夹的标记     

31、;                                 u.PutNextEntry(z;    /此句不可少,否则空目录不会被添加         

32、;                             s = this.ShortDir(tem.FullName;                

33、60;                    this.AddZipEntry(s,u,out u;       /递归                  &#

34、160;                                     foreach(FileInfo temp in di.GetFiles(  /获取此目录的文件   

35、60;                                                  &#

36、160;           s = this.ShortDir(temp.FullName;                                  

37、;   this.AddZipEntry(s,u,out u;       /递归                                    

38、60;                                  else if(File.Exists(s /文件的处理           

39、0;                                          u.SetLevel(9;      /压缩等级&#

40、160;                                FileStream f = File.OpenRead(s;             

41、                   byte b = new bytef.Length;                          

42、0;      f.Read(b,0,b.Length;           /将文件流加入缓冲字节中                             

43、   ZipEntry z = new ZipEntry(this.ShortDir(s;                                u.PutNextEntry(z;       

44、;       /为压缩文件流提供一个容器                                u.Write(b,0,b.Length; /写入字节      

45、;                          f.Close(;                        

46、                      j=u;    /返回已添加数据的“ZipOutputStream”                     

47、60;        public void UnZipFile(string p   /解压缩                                  str

48、ing un_tmp = p.Split(new char'*'                   int i2=0;  /防止名称冲突的参数                   f

49、or(int j=0;j                                               if (un_tmpj!="&q

50、uot;                                                   

51、          string un_time=System.DateTime.Now.ToShortDateString(+"-"+System.DateTime.Now.Hour.ToString(+"-"+System.DateTime.Now.Minute.ToString(+"-"+(System.DateTime.Now.Second+i2.ToString(;       

52、;                            string un_dir =ServerDir+"Unzip-"+un_time;               

53、;                    Directory.CreateDirectory(un_dir;      /创建以解压时间为名称的文件夹                   &

54、#160;               ZipInputStream f = new ZipInputStream(File.OpenRead(ServerDir+un_tmpj; /读取压缩文件,并用此文件流新建 “ZipInputStream”对象                  &

55、#160;                A:   ZipEntry zp = f.GetNextEntry(;    /获取解压文件流中的项目。 另注(我的理解):在压缩包里每个文件都以“ZipEntry”形式存在,其中包括存放文件的目录信息。如果空目录被压缩,该目录下将出现一个名称为空、 大小为 0 、“Crc”属性为 00000000 的“文件”。此文件只是个标记,不会被解压。  

56、                               while(zp!=null                

57、0;                                                  

58、60;     string un_tmp2;                                         if(zp.Name.

59、IndexOf("""">=0 /获取文件的目录信息                                          

60、0;                                               int tmp1 = zp.Na

61、me.LastIndexOf(""""                                           

62、60;      un_tmp2 = zp.Name.Substring(0,tmp1;                                      

63、60;           Directory.CreateDirectory(un_dir+""""+un_tmp2+"""" /必须先创建目录,否则解压失败 - (A) 关系到下面的步骤(B)                  

64、0;                                                  

65、60;            if(!zp.IsDirectory&&zp.Crc!=00000000L /此“ZipEntry”不是“标记文件”                          

66、60;                                                  &#

67、160;               int i =2048;                                &#

68、160;                byte b = new bytei;  /每次缓冲 2048 字节                          

69、                        FileStream s= File.Create(un_dir+""""+zp.Name; /(B-新建文件流              &

70、#160;                                  while(true /持续读取字节,直到一个“ZipEntry”字节读完         

71、                                                   

72、;                                               i = f.Read(b,0,b.Lengt

73、h; /读取“ZipEntry”中的字节                                              

74、0;          if(i>0                                      &

75、#160;                                                  

76、                                     s.Write(b,0,i; /将字节写入新建的文件流         &#

77、160;                                                  &

78、#160;                                                 &

79、#160;    else                                            

80、60;                                                                   

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论