ZipHelper.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. *
  3. * @date 2014-10-23
  4. *
  5. * @author 邓尹平
  6. *
  7. * @par 成员列表类 说明
  8. * 主要负责管理成员 提供搜索\排序和操作的方法
  9. *
  10. * @par 版权声明
  11. * 深圳市多元世纪信息技术有限公司 版权所有
  12. *
  13. * @see 使用此类时参照一些其他类可以写在这里
  14. *
  15. * @todo 该类有待完成的任务 一条条列出 完成一条删除一条
  16. *
  17. * @bug 该类已知的Bug一条条列出 完成一条删除一条
  18. *
  19. */
  20. using System;
  21. using System.IO;
  22. using System.IO.Compression;
  23. namespace Muchinfo.PC.Common.Bosn
  24. {
  25. /// <summary>
  26. /// 压缩 解压功能组件
  27. /// </summary>
  28. public static class ZipHelper
  29. {
  30. #region Fields
  31. private const int BufferLength = 1024;
  32. #endregion Fields
  33. #region Methods
  34. #region Public Static Methods
  35. /// <summary>
  36. /// 压缩文件
  37. /// </summary>
  38. /// <param name="sourceFile">需要压缩的文件</param>
  39. /// <param name="zippedFile">压缩后的文件</param>
  40. /// <exception cref="ArgumentNullException">参数为空异常</exception>
  41. /// <exception cref="FileNotFoundException">需要压缩的文件不存在</exception>
  42. public static void Compress(string sourceFile, string zippedFile)
  43. {
  44. if (string.IsNullOrEmpty(sourceFile))
  45. throw new ArgumentNullException("sourceFile");
  46. if (string.IsNullOrEmpty(zippedFile))
  47. throw new ArgumentNullException("zippedFile");
  48. if (File.Exists(sourceFile))
  49. {
  50. var streamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
  51. var streamZipped = new FileStream(zippedFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
  52. Compress(streamSource, streamZipped);
  53. streamSource.Close();
  54. streamZipped.Close();
  55. }
  56. else
  57. {
  58. throw new FileNotFoundException("需要压缩的文件不存在.", "sourceFile");
  59. }
  60. }
  61. /// <summary>
  62. /// 压缩流
  63. /// </summary>
  64. /// <param name="source">需要压缩的流</param>
  65. /// <param name="zipped">压缩后的流</param>
  66. /// <exception cref="ArgumentNullException">参数为空异常</exception>
  67. /// <exception cref="ArgumentException">无法读取需要压缩的流以及无法写入压缩的流.</exception>
  68. public static void Compress(Stream source, Stream zipped)
  69. {
  70. if (source == null)
  71. throw new ArgumentNullException("source");
  72. if (zipped == null)
  73. throw new ArgumentNullException("zipped");
  74. if (!source.CanRead)
  75. throw new ArgumentException("无法读取需要压缩的流.", "source");
  76. if (!zipped.CanWrite)
  77. throw new ArgumentException("无法写入压缩的流.", "zipped");
  78. var zipper = new GZipStream(zipped, CompressionMode.Compress);
  79. var readBuffer = new byte[BufferLength];
  80. int readCount = source.Read(readBuffer, 0, BufferLength);
  81. while (readCount != 0)
  82. {
  83. zipper.Write(readBuffer, 0, readCount);
  84. readCount = source.Read(readBuffer, 0, BufferLength);
  85. }
  86. zipper.Close();
  87. }
  88. /// <summary>
  89. /// 压缩字节数组
  90. /// </summary>
  91. /// <param name="source">需要压缩的字节数组</param>
  92. /// <exception cref="ArgumentNullException">参数为空异常</exception>
  93. /// <exception cref="ArgumentException">需要压缩的字节数组长度不正确</exception>
  94. /// <returns>压缩后的字节数组</returns>
  95. public static byte[] Compress(byte[] source)
  96. {
  97. if (source == null)
  98. throw new ArgumentNullException("source");
  99. if (source.Length == 0)
  100. throw new ArgumentException("需要压缩的字节数组长度不正确.", "source");
  101. var streamSource = new MemoryStream(source);
  102. var streamZipped = new MemoryStream();
  103. Compress(streamSource, streamZipped);
  104. streamSource.Close();
  105. streamZipped.Position = 0;
  106. byte[] zipped = streamZipped.ToArray();
  107. streamZipped.Close();
  108. return zipped;
  109. }
  110. /// <summary>
  111. /// 解压文件
  112. /// </summary>
  113. /// <param name="targetFile">解压的文件</param>
  114. /// <param name="zippedFile">压缩的文件</param>
  115. /// <exception cref="ArgumentNullException">参数为空异常</exception>
  116. /// <exception cref="FileNotFoundException">需要压缩的文件不存在</exception>
  117. public static void Decompress(string zippedFile, string targetFile)
  118. {
  119. if (string.IsNullOrEmpty(targetFile))
  120. throw new ArgumentNullException("targetFile");
  121. if (string.IsNullOrEmpty(zippedFile))
  122. throw new ArgumentNullException("zippedFile");
  123. if (File.Exists(zippedFile))
  124. {
  125. var streamTarget = new FileStream(targetFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
  126. var streamZipped = new FileStream(zippedFile, FileMode.Open, FileAccess.Read, FileShare.Read);
  127. Decompress(streamZipped, streamTarget);
  128. streamTarget.Close();
  129. streamZipped.Close();
  130. }
  131. else
  132. {
  133. throw new FileNotFoundException("需要压缩的文件不存在.", "sourceFile");
  134. }
  135. }
  136. /// <summary>
  137. /// 解压缩流
  138. /// </summary>
  139. /// <param name="target">需要压缩的流</param>
  140. /// <param name="zipped">压缩的流</param>
  141. /// <exception cref="ArgumentNullException">参数为空异常</exception>
  142. /// <exception cref="ArgumentException">无法读取压缩的流以及无法写入解压的流.</exception>
  143. public static void Decompress(Stream zipped, Stream target)
  144. {
  145. if (target == null)
  146. throw new ArgumentNullException("target");
  147. if (zipped == null)
  148. throw new ArgumentNullException("zipped");
  149. if (!zipped.CanRead)
  150. throw new ArgumentException("无法读取压缩的流.", "zipped");
  151. if (!target.CanWrite)
  152. throw new ArgumentException("无法写入解压的流.", "target");
  153. var zipper = new GZipStream(zipped, CompressionMode.Decompress);
  154. var readBuffer = new byte[BufferLength];
  155. int readCount = zipper.Read(readBuffer, 0, BufferLength);
  156. while (readCount != 0)
  157. {
  158. target.Write(readBuffer, 0, readCount);
  159. readCount = zipper.Read(readBuffer, 0, BufferLength);
  160. }
  161. target.Position = 0;
  162. zipper.Close();
  163. }
  164. /// <summary>
  165. /// 解压缩字节数组
  166. /// </summary>
  167. /// <param name="zipped">需要解压缩的字节数组</param>
  168. /// <exception cref="ArgumentNullException">参数为空异常</exception>
  169. /// <exception cref="ArgumentException">压缩的字节数组长度不正确</exception>
  170. /// <returns>解压缩后的字节数组</returns>
  171. public static byte[] Decompress(byte[] zipped)
  172. {
  173. if (zipped == null)
  174. throw new ArgumentNullException("zipped");
  175. if (zipped.Length == 0)
  176. throw new ArgumentException("压缩的字节数组长度不正确.", "zipped");
  177. var streamTarget = new MemoryStream();
  178. var streamZipped = new MemoryStream(zipped);
  179. Decompress(streamZipped, streamTarget);
  180. streamZipped.Close();
  181. streamTarget.Position = 0;
  182. byte[] target = streamTarget.ToArray();
  183. streamTarget.Close();
  184. return target;
  185. }
  186. #endregion Public Static Methods
  187. #endregion Methods
  188. }
  189. }