CompressUtil.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * 文件: CompressUtil.cs
  3. * 由SharpDevelop创建。
  4. * 作者: gwang
  5. *
  6. * 功能: 描述
  7. * 版本:
  8. * 1.0.0 Created by gwang - 2016/5/14 16:06
  9. */
  10. using System;
  11. using System.IO;
  12. using System.IO.Compression;
  13. using System.Linq;
  14. using System.Text;
  15. namespace CSharpUtil.Net
  16. {
  17. /// <summary>
  18. /// 压缩工具类- 注意:由于优化能力有限,本工具限制了待压缩数据的大小不超过1MB.
  19. /// </summary>
  20. /// <exception cref="DataTooLargeException"></exception>
  21. public static class CompressUtil
  22. {
  23. /// <summary>
  24. /// 自定义异常
  25. /// </summary>
  26. public class DataTooLargeException : Exception
  27. {
  28. public DataTooLargeException()
  29. : base("传入的数据过大(不接受超过1M的数据)!")
  30. {
  31. }
  32. }
  33. /// <summary>
  34. /// 数据最大长度不宜超过10M
  35. /// </summary>
  36. const int MaxDataLenght = 1024 * 1024 * 10;
  37. /// <summary>
  38. /// 利用deflate算法压缩字节数组
  39. /// </summary>
  40. /// <param name="data">字节数组(不宜超过1M)</param>
  41. /// <param name="compressMode">压缩模式(压缩/解压)</param>
  42. /// <exception cref="OutOfMemoryException">数据不建议超过1M</exception>
  43. /// <returns></returns>
  44. public static byte[] Deflate(byte[] data, CompressionMode compressMode = CompressionMode.Compress)
  45. {
  46. if (data.Length > MaxDataLenght)
  47. {
  48. throw new DataTooLargeException();
  49. }
  50. else
  51. {
  52. byte[] buffer = null;
  53. using (var outms = new MemoryStream())
  54. {
  55. using (var df = new DeflateStream(outms, compressMode, true))
  56. {
  57. df.Write(data, 0, data.Length);
  58. }
  59. outms.Position = 0;
  60. buffer = outms.ToArray();
  61. }
  62. return buffer;
  63. }
  64. }
  65. /// <summary>
  66. /// 解压deflate数据
  67. /// </summary>
  68. /// <param name="data">被压缩的内容</param>
  69. /// <exception cref="OutOfMemoryException">数据不建议超过1M</exception>
  70. /// <returns></returns>
  71. public static byte[] InFlate(byte[] data)
  72. {
  73. // todo: 这种写法在system.io.compress模式下是支持的.
  74. return Deflate(data, CompressionMode.Decompress);
  75. }
  76. /// <summary>
  77. /// 解压deflate压缩的字符串数据
  78. /// </summary>
  79. /// <param name="data"></param>
  80. /// <param name="encoder">编码</param>
  81. /// <returns>还原到字符串</returns>
  82. public static string InFlate(byte[] data, Encoding encoder)
  83. {
  84. using (var inputStream = new MemoryStream(data))
  85. using (var gzip = new DeflateStream(inputStream, CompressionMode.Decompress))
  86. using (var reader = new StreamReader(gzip, encoder))
  87. {
  88. return reader.ReadToEnd();
  89. }
  90. }
  91. /// <summary>
  92. /// 解压缩
  93. /// </summary>
  94. /// <param name="sm">原始文件流</param>
  95. /// <returns>还原后的字节数组</returns>
  96. public static byte[] InFlate(Stream sm)
  97. {
  98. using (var outms = new MemoryStream())
  99. using (var de = new DeflateStream(sm, CompressionMode.Decompress, true))
  100. {
  101. var buf = new byte[1024];
  102. int len;
  103. while ((len = de.Read(buf, 0, buf.Length)) > 0)
  104. {
  105. outms.Write(buf, 0, len);
  106. }
  107. return outms.ToArray();
  108. }
  109. }
  110. /// <summary>
  111. /// string=>utf8_bytes=>deflate=>base64_encode
  112. /// </summary>
  113. /// <param name="data"></param>
  114. /// <returns></returns>
  115. public static string zb64encode(string data)
  116. {
  117. var bytes = Encoding.UTF8.GetBytes(data);
  118. return Convert.ToBase64String(Deflate(bytes));
  119. }
  120. /// <summary>
  121. /// string=>base64_decode=>inflate=>utf8_stringfrombytes
  122. /// </summary>
  123. /// <param name="data"></param>
  124. /// <returns></returns>
  125. public static string zb64decode(string data)
  126. {
  127. return Encoding.UTF8.GetString(InFlate(Convert.FromBase64String(data)));
  128. }
  129. }
  130. }