阐述VS2003压缩代码的有关常识

开发 后端
我们经常会遇到编制VS2003压缩代码的问题,也会遇到将VS2003压缩代码修改的的问题。那么,如何解决此类问题呢?下文就给大家进行全面的讲解。

昨天到今天搞了一整天的VS2003压缩代码,我都快崩溃了! 一看到那些代码,脑袋顿时就像爆炸一样,所以有了许多的问题出现,还好,我一个个把他记录下来了,同时,在相关论坛上找了一些相关的解决办法,分享一下,供大家相互学习交流

1、首先从这里下载0.84版本的VS2003压缩代码及示例码。

2、下载下来之后你发现它没有VS2003的解决方案文件,没有关系。你可以自己建立,首先新建一个ZipUnzip的解决方案,然后,将上面经过解压缩之后的所有文件及目录COPY到你的解决方案所在的目录下。 #t#

3、在VS2003解决方案资源管理器(一般是在右上方中部点的位置)中点击显示所有文件按钮,然后可以见到很多“虚”的图标、文件及文件夹等,可以一次选择它们,然后包含进项目中。

4、编译,***使用Release选项,编译完成之后你可以在\bin\Release\看到ZipUnzip.dll的类了。如果你编译时报错,说什么AssemblyKeyFile之类的,你可以使用强命名工具新建一个,也可以将AssemblyInfo.cs中[assembly: AssemblyKeyFile("。。。。。")]改成:[assembly: AssemblyKeyFile("")] (不推荐这样做)。

5、新建一个WEBFORM项目,添加ZipUnzip.dll类的引用,然后添加如下文件及内容:

  1. using System;  
  2. using System.IO;  
  3. using ICSharpCode.SharpZipLib.Zip;  
  4. using ICSharpCode.SharpZipLib.GZip;  
  5. using ICSharpCode.SharpZipLib.BZip2;  
  6. using ICSharpCode.SharpZipLib.Checksums;  
  7. using ICSharpCode.SharpZipLib.Zip.Compression;  
  8. using ICSharpCode.SharpZipLib.Zip.Compression.Streams;  
  9.  
  10. namespace WebZipUnzip  
  11. {  
  12.  public class AttachmentUnZip  
  13.  {  
  14. public AttachmentUnZip()  
  15. {}  
  16. public static void UpZip(string zipFile)  
  17. {  
  18. string []FileProperties=new string[2];  
  19. FileProperties[0]=zipFile;//待解压的文件  
  20. FileProperties[1]=zipFile.Substring(0,zipFile.LastIndexOf("\\")+1);//解压后放置的目标目录  
  21. UnZipClass UnZc=new UnZipClass();  
  22. UnZc.UnZip(FileProperties);  
  23. }  
  24.  }  
  25. }  
  26.  
  27. // ---------------------------------------------  
  28. // 2. UnZipClass.cs  
  29. // ---------------------------------------------  
  30.  
  31. using System;  
  32. using System.IO;  
  33. using ICSharpCode.SharpZipLib.Zip;  
  34. using ICSharpCode.SharpZipLib.GZip;  
  35. using ICSharpCode.SharpZipLib.BZip2;  
  36. using ICSharpCode.SharpZipLib.Checksums;  
  37. using ICSharpCode.SharpZipLib.Zip.Compression;  
  38. using ICSharpCode.SharpZipLib.Zip.Compression.Streams;  
  39.  
  40. namespace WebZipUnzip  
  41. {  
  42.  public class UnZipClass  
  43.  {   
  44. ///   
  45. /// 解压文件  
  46. ///   
  47. /// 包含要解压的文件名和要解压到的目录名数组  
  48. public void UnZip(string[] args)  
  49. {  
  50. ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));  
  51. try  
  52. {   
  53.  ZipEntry theEntry;  
  54.  while ((theEntry = s.GetNextEntry()) != null)   
  55.  {   
  56. string directoryName = Path.GetDirectoryName(args[1]);  
  57. string fileName = Path.GetFileName(theEntry.Name);  
  58.  
  59. //生成解压目录  
  60. Directory.CreateDirectory(directoryName);  
  61.  
  62. if (fileName != String.Empty)   
  63. {   
  64. //解压文件到指定的目录  
  65. FileStream streamWriter = File.Create(args[1]+fileName);  
  66. int size = 2048;  
  67. byte[] data = new byte[2048];  
  68. while (true)   
  69. {  
  70.  ssize = s.Read(data, 0, data.Length);  
  71.  if (size > 0)   
  72.  {  
  73. streamWriter.Write(data, 0, size);  
  74.  }   
  75.  else   
  76.  {  
  77. break;  
  78.  }  
  79. }  
  80. streamWriter.Close();  
  81. }  
  82.  }  
  83.  s.Close();  
  84. }  
  85. catch(Exception eu)  
  86. {  
  87.  throw eu;  
  88. }  
  89. finally  
  90. {  
  91.  s.Close();  
  92. }  
  93. }//end UnZip  
  94.  
  95. public static bool UnZipFile(string file, string dir)  
  96. {  
  97. try  
  98. {  
  99.  if (!Directory.Exists(dir))  
  100. Directory.CreateDirectory(dir);  
  101. string fileFullName = Path.Combine(dir,file);  
  102. ZipInputStream s = new ZipInputStream(File.OpenRead( fileFullName ));  
  103.    
  104. ZipEntry theEntry;  
  105. while ((theEntry = s.GetNextEntry()) != null)  
  106. {  
  107. string directoryName = Path.GetDirectoryName(theEntry.Name);  
  108. string fileName = Path.GetFileName(theEntry.Name);  
  109.    
  110. if (directoryName != String.Empty)  
  111.  Directory.CreateDirectory( Path.Combine(dir, directoryName));  
  112.  if (fileName != String.Empty)  
  113.  {  
  114. FileStream streamWriter = File.Create( Path.Combine(dir,theEntry.Name) );  
  115. int size = 2048;  
  116. byte[] data = new byte[2048];  
  117. while (true)  
  118. {  
  119. ssize = s.Read(data, 0, data.Length);  
  120. if (size > 0)  
  121. {  
  122.  streamWriter.Write(data, 0, size);  
  123. }  
  124. else  
  125. {  
  126.  break;  
  127. }  
  128. }  
  129. streamWriter.Close();  
  130.  }  
  131. }  
  132. s.Close();  
  133. return true;  
  134. }  
  135. catch (Exception)  
  136. {  
  137. throw;  
  138. }  
  139.  }  
  140. }//end UnZipClass  

此方案解决了文件名中文字的问题,目录VS2003压缩代码问题,至于整个文件夹批量上传并压缩成一个WINZIP压缩包的问题,没有时间解决了,各位如有解决方案,不妨共享一下。

责任编辑:chenqingxiang 来源: 计世网
相关推荐

2009-11-26 17:02:29

VS2003配置

2009-12-01 14:30:08

VS2003使用

2009-11-30 09:27:38

VS2003源代码

2009-11-30 09:16:44

VS2003源代码

2009-11-25 10:14:43

2009-12-15 13:39:43

2009-11-27 08:59:29

VS2003配置文件

2009-12-09 13:41:04

VS 2003 报错

2009-12-18 10:10:49

VS 2003程序

2009-11-26 13:55:35

VS2003源代码

2009-11-30 13:51:28

2009-12-09 16:52:51

VS 2003插件

2009-11-30 15:57:18

VS2003 MFC

2009-11-30 11:05:19

VS2003 WebS

2009-12-01 17:55:11

VS2003配置

2009-12-01 10:54:48

VS2003 英文版

2009-11-30 16:50:26

VS2003调试

2009-11-30 17:28:39

VS2003 ASP

2009-11-25 13:57:25

VS2003发布

2009-12-01 15:32:48

VS2003配置
点赞
收藏

51CTO技术栈公众号