关键词搜索

源码搜索 ×
×

C# 文件流压缩解压

发布2015-11-09浏览6901次

详情内容

  1. /// <summary>
  2. /// 文件流压缩解压
  3. /// </summary>
  4. public class ZipHelper
  5. {
  6. public static int BEST_COMPRESSION = 9;
  7. public static int BEST_SPEED = 1;
  8. public static int DEFAULT_COMPRESSION = -1;
  9. public static int NO_COMPRESSION = 0;
  10. #region Deflate压缩
  11. #region Deflate压缩
  12. /// <summary>
  13. /// Deflate方式压缩(默认压缩级别最高)
  14. /// </summary>
  15. /// <param name="stream"></param>
  16. /// <returns></returns>
  17. public static Stream Deflate(Stream stream)
  18. {
  19. return ZipHelper.Deflate(stream, ZipHelper.DEFAULT_COMPRESSION);
  20. }
  21. /// <summary>
  22. /// Deflate方式压缩
  23. /// </summary>
  24. /// <param name="stream"></param>
  25. /// <param name="level">压缩品质级别(0~9)</param>
  26. /// <returns></returns>
  27. public static Stream Deflate(Stream stream, int level)
  28. {
  29. byte[] array = ZipHelper.StreamToBytes(stream);
  30. byte[] array2 = new byte[array.Length];
  31. Deflater deflater = new Deflater();
  32. deflater.SetLevel(level);
  33. deflater.SetStrategy(DeflateStrategy.Default);
  34. deflater.SetInput(array);
  35. deflater.Finish();
  36. int num = deflater.Deflate(array2);
  37. byte[] array3 = new byte[num];
  38. Array.Copy(array2, array3, num);
  39. return ZipHelper.BytesToStream(array3);
  40. }
  41. /// <summary>
  42. /// Deflate方式压缩
  43. /// </summary>
  44. /// <param name="input"></param>
  45. /// <param name="level">压缩品质级别(0~9)</param>
  46. /// <returns></returns>
  47. public static byte[] Deflate(byte[] input, int level)
  48. {
  49. byte[] result;
  50. try
  51. {
  52. if (input == null && input.Length == 0)
  53. {
  54. result = new byte[0];
  55. }
  56. else
  57. {
  58. byte[] array = new byte[input.Length];
  59. Deflater deflater = new Deflater();
  60. deflater.SetLevel(level);
  61. deflater.SetStrategy(DeflateStrategy.Default);
  62. deflater.SetInput(input);
  63. deflater.Finish();
  64. int num = deflater.Deflate(array);
  65. byte[] array2 = new byte[num];
  66. Array.Copy(array, array2, num);
  67. result = array2;
  68. }
  69. }
  70. catch (Exception innerException)
  71. {
  72. throw new Exception("压缩程序出错!", innerException);
  73. }
  74. return result;
  75. }
  76. #endregion
  77. #region Inflate解压
  78. /// <summary>
  79. /// Inflate解压
  80. /// </summary>
  81. /// <param name="input"></param>
  82. /// <returns></returns>
  83. public static byte[] Inflate(byte[] input)
  84. {
  85. byte[] result;
  86. try
  87. {
  88. if (input == null && input.Length == 0)
  89. {
  90. result = new byte[0];
  91. }
  92. else
  93. {
  94. Inflater inflater = new Inflater();
  95. inflater.SetInput(input);
  96. byte[] array = new byte[1024];
  97. using (MemoryStream memoryStream = new MemoryStream())
  98. {
  99. for (int i = inflater.Inflate(array, 0, array.Length); i > 0; i = inflater.Inflate(array, 0, array.Length))
  100. {
  101. memoryStream.Write(array, 0, i);
  102. }
  103. byte[] buffer = memoryStream.GetBuffer();
  104. memoryStream.Close();
  105. result = buffer;
  106. }
  107. }
  108. }
  109. catch (Exception innerException)
  110. {
  111. throw new Exception("解压缩程序出错!", innerException);
  112. }
  113. return result;
  114. }
  115. /// <summary>
  116. /// Inflate解压
  117. /// </summary>
  118. /// <param name="zipStream"></param>
  119. /// <returns></returns>
  120. public static Stream Inflate(Stream zipStream)
  121. {
  122. byte[] input = ZipHelper.StreamToBytes(zipStream);
  123. byte[] bytes = ZipHelper.Inflate(input);
  124. return ZipHelper.BytesToStream(bytes);
  125. }
  126. #endregion
  127. #endregion
  128. #region GZip压缩
  129. /// <summary>
  130. /// GZip压缩
  131. /// </summary>
  132. /// <param name="srcStream"></param>
  133. /// <param name="output"></param>
  134. public static void GZipCompress(Stream srcStream, Stream output)
  135. {
  136. ZipHelper.GZipCompress(srcStream, 6, output);
  137. }
  138. /// <summary>
  139. /// GZip压缩
  140. /// </summary>
  141. /// <param name="srcStream"></param>
  142. /// <param name="compressLevel">压缩品质级别(0~9)</param>
  143. /// <param name="output"></param>
  144. public static void GZipCompress(Stream srcStream, int compressLevel, Stream output)
  145. {
  146. if (compressLevel < 1 || compressLevel > 9)
  147. {
  148. throw new Exception(string.Format("您指定的压缩级别 {0} 不在有效的范围(1-9)内", compressLevel));
  149. }
  150. srcStream.Position = 0L;
  151. GZipOutputStream gZipOutputStream = new GZipOutputStream(output);
  152. gZipOutputStream.SetLevel(compressLevel);
  153. try
  154. {
  155. int i = 4096;
  156. byte[] buffer = new byte[i];
  157. while (i > 0)
  158. {
  159. i = srcStream.Read(buffer, 0, i);
  160. gZipOutputStream.Write(buffer, 0, i);
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. throw new Exception("GZip压缩出错:" + ex.Message);
  166. }
  167. srcStream.Close();
  168. gZipOutputStream.Finish();
  169. }
  170. /// <summary>
  171. /// GZip解压
  172. /// </summary>
  173. /// <param name="zipStream"></param>
  174. /// <param name="outputStream"></param>
  175. public static void GZipDeCompress(Stream zipStream, Stream outputStream)
  176. {
  177. GZipInputStream gZipInputStream = new GZipInputStream(zipStream);
  178. try
  179. {
  180. int i = 4096;
  181. byte[] buffer = new byte[i];
  182. while (i > 0)
  183. {
  184. i = gZipInputStream.Read(buffer, 0, i);
  185. outputStream.Write(buffer, 0, i);
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. throw new Exception("GZip解压缩出错:" + ex.Message);
  191. }
  192. zipStream.Close();
  193. gZipInputStream.Close();
  194. }
  195. #endregion
  196. #region BZip2压缩
  197. /// <summary>
  198. /// BZip2压缩
  199. /// </summary>
  200. /// <param name="inStream"></param>
  201. /// <param name="outStream"></param>
  202. /// <param name="blockSize"></param>
  203. public static void BZip2Compress(Stream inStream, Stream outStream, int blockSize)
  204. {
  205. BZip2.Compress(inStream, outStream, blockSize);
  206. }
  207. /// <summary>
  208. /// BZip2解压
  209. /// </summary>
  210. /// <param name="inStream"></param>
  211. /// <param name="outStream"></param>
  212. public static void BZip2Decompress(Stream inStream, Stream outStream)
  213. {
  214. BZip2.Decompress(inStream, outStream);
  215. }
  216. #endregion
  217. private static byte[] StreamToBytes(Stream stream)
  218. {
  219. byte[] array = new byte[stream.Length];
  220. stream.Seek(0L, SeekOrigin.Begin);
  221. stream.Read(array, 0, array.Length);
  222. stream.Close();
  223. return array;
  224. }
  225. private static Stream BytesToStream(byte[] bytes)
  226. {
  227. return new MemoryStream(bytes);
  228. }
  229. private static void StreamToFile(Stream stream, string fileName)
  230. {
  231. byte[] array = new byte[stream.Length];
  232. stream.Read(array, 0, array.Length);
  233. stream.Seek(0L, SeekOrigin.Begin);
  234. FileStream fileStream = new FileStream(fileName, FileMode.Create);
  235. BinaryWriter binaryWriter = new BinaryWriter(fileStream);
  236. binaryWriter.Write(array);
  237. binaryWriter.Close();
  238. fileStream.Close();
  239. }
  240. private static Stream FileToStream(string fileName)
  241. {
  242. FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
  243. byte[] array = new byte[fileStream.Length];
  244. fileStream.Read(array, 0, array.Length);
  245. fileStream.Close();
  246. return new MemoryStream(array);
  247. }
  248. }

相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载