ZipFileClass.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:ZipFileClass.cs
  5. * 2.功能描述:压缩文件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/04 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.IO;
  12. using ICSharpCode.SharpZipLib.Checksums;
  13. using ICSharpCode.SharpZipLib.Zip;
  14. namespace Dongke.IBOSS.PRD.Client.AutoUpgrade
  15. {
  16. /// <summary>
  17. /// 解压/压缩文件
  18. /// </summary>
  19. public static class ZipFileClass
  20. {
  21. /// <summary>
  22. /// 压缩文件
  23. /// </summary>
  24. /// <param name="strFile">文件名</param>
  25. /// <param name="strZip">压缩文件名</param>
  26. public static void ZipFile(string strFile, string strZip)
  27. {
  28. if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
  29. strFile += Path.DirectorySeparatorChar;
  30. using (ZipOutputStream s = new ZipOutputStream(File.Create(strZip)))
  31. {
  32. //BZip2OutputStream bZip2 = new BZip2OutputStream(File.Create(strZip));
  33. //GZipOutputStream gZip = new GZipOutputStream(File.Create(strZip));
  34. s.SetLevel(7); // 0 - store only to 9 - means best compression
  35. Zip(strFile, s, strFile);
  36. s.Finish();
  37. s.Close();
  38. }
  39. }
  40. /// <summary>
  41. /// 压缩文件
  42. /// </summary>
  43. /// <param name="strFile"></param>
  44. /// <param name="s"></param>
  45. /// <param name="staticFile"></param>
  46. private static void Zip(string strFile, ZipOutputStream s, string staticFile)
  47. {
  48. if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
  49. strFile += Path.DirectorySeparatorChar;
  50. Crc32 crc = new Crc32();
  51. string[] filenames = Directory.GetFileSystemEntries(strFile);
  52. foreach (string file in filenames)
  53. {
  54. if (Directory.Exists(file))
  55. {
  56. Zip(file, s, staticFile);
  57. }
  58. else // 否则直接压缩文件
  59. {
  60. //打开压缩文件
  61. using (FileStream fs = File.OpenRead(file))
  62. {
  63. byte[] buffer = new byte[fs.Length];
  64. fs.Read(buffer, 0, buffer.Length);
  65. string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
  66. ZipEntry entry = new ZipEntry(tempfile);
  67. entry.DateTime = DateTime.Now;
  68. entry.Size = fs.Length;
  69. fs.Close();
  70. crc.Reset();
  71. crc.Update(buffer);
  72. entry.Crc = crc.Value;
  73. s.PutNextEntry(entry);
  74. s.Write(buffer, 0, buffer.Length);
  75. }
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// 解压缩文件
  81. /// </summary>
  82. /// <param name="TargetFile">目标文件</param>
  83. /// <param name="fileDir">解压文件</param>
  84. /// <returns></returns>
  85. public static string UnZipFile(string TargetFile, string fileDir)
  86. {
  87. string rootFile = " ";
  88. //读取压缩文件(zip文件),准备解压缩
  89. using (ZipInputStream s = new ZipInputStream(File.OpenRead(TargetFile.Trim())))
  90. {
  91. ZipEntry theEntry;
  92. string path = fileDir;
  93. //解压出来的文件保存的路径
  94. string rootDir = " ";
  95. //根目录下的第一个子文件夹的名称
  96. while ((theEntry = s.GetNextEntry()) != null)
  97. {
  98. rootDir = Path.GetDirectoryName(theEntry.Name);
  99. //得到根目录下的第一级子文件夹的名称
  100. if (rootDir.IndexOf("\\") >= 0)
  101. {
  102. rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
  103. }
  104. string dir = Path.GetDirectoryName(theEntry.Name);
  105. //根目录下的第一级子文件夹的下的文件夹的名称
  106. string fileName = Path.GetFileName(theEntry.Name);
  107. //根目录下的文件名称
  108. if (dir != " ")
  109. //创建根目录下的子文件夹,不限制级别
  110. {
  111. if (!Directory.Exists(fileDir + "\\" + dir))
  112. {
  113. path = fileDir + "\\" + dir;
  114. //在指定的路径创建文件夹
  115. Directory.CreateDirectory(path);
  116. }
  117. }
  118. else if (dir == " " && fileName != "")
  119. //根目录下的文件
  120. {
  121. path = fileDir;
  122. rootFile = fileName;
  123. }
  124. else if (dir != " " && fileName != "")
  125. //根目录下的第一级子文件夹下的文件
  126. {
  127. if (dir.IndexOf("\\") > 0)
  128. //指定文件保存的路径
  129. {
  130. path = fileDir + "\\" + dir;
  131. }
  132. }
  133. if (dir == rootDir)
  134. //判断是不是需要保存在根目录下的文件
  135. {
  136. path = fileDir + "\\" + rootDir;
  137. }
  138. //以下为解压缩zip文件的基本步骤
  139. //基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。
  140. if (fileName != String.Empty)
  141. {
  142. using (FileStream streamWriter = File.Create(path + "\\" + fileName))
  143. {
  144. int size = 2048;
  145. byte[] data = new byte[2048];
  146. while (true)
  147. {
  148. size = s.Read(data, 0, data.Length);
  149. if (size > 0)
  150. {
  151. streamWriter.Write(data, 0, size);
  152. }
  153. else
  154. {
  155. break;
  156. }
  157. }
  158. streamWriter.Close();
  159. }
  160. }
  161. }
  162. s.Close();
  163. return rootFile;
  164. }
  165. }
  166. /// <summary>
  167. /// 压缩文件夹
  168. /// </summary>
  169. /// <param name="path">要压缩的目录</param>
  170. /// <param name="address">输出的压缩文件</param>
  171. public static void DirectoryToZip(string path, string address)
  172. {
  173. // 获取当前文件夹中的所有文件
  174. string[] filenames = Directory.GetFiles(path);
  175. Crc32 crc = new Crc32();
  176. // 创建输出文件
  177. ZipOutputStream zos = new ZipOutputStream(File.Create(address));
  178. zos.SetLevel(6);
  179. // 遍历所有文件
  180. foreach (string name in filenames)
  181. {
  182. FileStream fs = File.OpenRead(name);
  183. byte[] buffer = new byte[fs.Length];
  184. // 读取文件
  185. fs.Read(buffer, 0, Convert.ToInt32(fs.Length));
  186. // 获取文件的文件名和后缀名
  187. string file = Path.GetFileName(name);
  188. // 输出文件的名称
  189. ZipEntry entry = new ZipEntry(file);
  190. crc.Reset();
  191. crc.Update(buffer);
  192. entry.Crc = crc.Value;
  193. zos.PutNextEntry(entry);
  194. zos.Write(buffer, 0, Convert.ToInt32(fs.Length));
  195. fs.Close();
  196. }
  197. zos.Finish();
  198. zos.Close();
  199. }
  200. }
  201. }