CompressHelper.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.IO.Compression;
  7. using EasyDevCore.Common.Security;
  8. namespace EasyDevCore.Common
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public enum CompressMethod
  14. {
  15. /// <summary>
  16. /// The deflate
  17. /// </summary>
  18. Deflate,
  19. /// <summary>
  20. /// The G zip
  21. /// </summary>
  22. GZip
  23. }
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. public class CompressHelper
  28. {
  29. /// <summary>
  30. /// Compresses the data.
  31. /// </summary>
  32. /// <param name="data">The data.</param>
  33. /// <param name="method">The method.</param>
  34. /// <returns></returns>
  35. public static byte[] CompressData(byte[] data, CompressMethod method = CompressMethod.Deflate)
  36. {
  37. using (var inputStream = new MemoryStream(data))
  38. using (var compressStream = new MemoryStream())
  39. {
  40. using (var compressor = (method == CompressMethod.Deflate ? (Stream)(new DeflateStream(compressStream, CompressionMode.Compress)) : (Stream)(new GZipStream(compressStream, CompressionMode.Compress))))
  41. {
  42. inputStream.CopyTo(compressor);
  43. }
  44. return compressStream.ToArray();
  45. }
  46. }
  47. /// <summary>
  48. /// Compresses the data.
  49. /// </summary>
  50. /// <param name="data">The data.</param>
  51. /// <param name="password">The password to encrypt data.</param>
  52. /// <param name="algorithm">The algorithm.</param>
  53. /// <param name="method">The method.</param>
  54. /// <returns></returns>
  55. public static byte[] CompressData(byte[] data, string password, SymmetricAlgorithmMethod algorithm = SymmetricAlgorithmMethod.DESCryptoServiceProvider, CompressMethod method = CompressMethod.Deflate)
  56. {
  57. return EncryptionHelper.EncryptData(password, algorithm, CompressData(data, method));
  58. }
  59. /// <summary>
  60. /// Decompress data
  61. /// </summary>
  62. /// <param name="data">The data.</param>
  63. /// <param name="method">The method.</param>
  64. /// <returns></returns>
  65. public static byte[] DecompressData(byte[] data, CompressMethod method = CompressMethod.Deflate)
  66. {
  67. using (var inputStream = new MemoryStream(data))
  68. using (var decompressStream = new MemoryStream())
  69. {
  70. using (var decompressor = (method == CompressMethod.Deflate ? (Stream)(new DeflateStream(inputStream, CompressionMode.Decompress)) : (Stream)(new GZipStream(inputStream, CompressionMode.Decompress))))
  71. {
  72. decompressor.CopyTo(decompressStream);
  73. }
  74. return decompressStream.ToArray();
  75. }
  76. }
  77. /// <summary>
  78. /// Des the compress data.
  79. /// </summary>
  80. /// <param name="data">The data.</param>
  81. /// <param name="password">The password to decrypt data.</param>
  82. /// <param name="algorithm">The algorithm.</param>
  83. /// <param name="method">The method.</param>
  84. /// <returns></returns>
  85. public static byte[] DecompressData(byte[] data, string password, SymmetricAlgorithmMethod algorithm = SymmetricAlgorithmMethod.DESCryptoServiceProvider, CompressMethod method = CompressMethod.Deflate)
  86. {
  87. return DecompressData(EncryptionHelper.DecryptData(password, algorithm, data), method);
  88. }
  89. /// <summary>
  90. /// Compresses the string.
  91. /// </summary>
  92. /// <param name="value">The value.</param>
  93. /// <param name="method">The method.</param>
  94. /// <param name="encoding">The encoding.</param>
  95. /// <returns></returns>
  96. public static string CompressString(string value, Encoding encoding, CompressMethod method = CompressMethod.Deflate)
  97. {
  98. return Encoding.GetEncoding(850).GetString(CompressData(encoding.GetBytes(value), method));
  99. }
  100. /// <summary>
  101. /// Compresses the string.
  102. /// </summary>
  103. /// <param name="value">The value.</param>
  104. /// <param name="password">The password to encrypt data.</param>
  105. /// <param name="encoding">The encoding.</param>
  106. /// <param name="method">The method.</param>
  107. /// <returns></returns>
  108. public static string CompressString(string value, string password, Encoding encoding, CompressMethod method = CompressMethod.Deflate)
  109. {
  110. return Encoding.GetEncoding(850).GetString(CompressData(encoding.GetBytes(value), password: password, method: method));
  111. }
  112. /// <summary>
  113. /// Compresses the string.
  114. /// </summary>
  115. /// <param name="value">The value.</param>
  116. /// <param name="method">The method.</param>
  117. /// <returns></returns>
  118. public static string CompressString(string value, CompressMethod method = CompressMethod.Deflate)
  119. {
  120. return Encoding.GetEncoding(850).GetString(CompressData(ConvertHelper.ToByteArray(value), method));
  121. }
  122. /// <summary>
  123. /// Compresses the string.
  124. /// </summary>
  125. /// <param name="value">The value.</param>
  126. /// <param name="password">The password to encrypt.</param>
  127. /// <param name="method">The method.</param>
  128. /// <returns></returns>
  129. public static string CompressString(string value, string password, CompressMethod method = CompressMethod.Deflate)
  130. {
  131. return Encoding.GetEncoding(850).GetString(CompressData(ConvertHelper.ToByteArray(value), password: password, method: method));
  132. }
  133. /// <summary>
  134. /// Decompresses the string.
  135. /// </summary>
  136. /// <param name="compressValue">The compress value.</param>
  137. /// <param name="method">The method.</param>
  138. /// <param name="encoding">The encoding.</param>
  139. /// <returns></returns>
  140. public static string DecompressString(string compressValue, Encoding encoding, CompressMethod method = CompressMethod.Deflate)
  141. {
  142. return encoding.GetString(DecompressData(Encoding.GetEncoding(850).GetBytes(compressValue), method));
  143. }
  144. /// <summary>
  145. /// Decompresses the string.
  146. /// </summary>
  147. /// <param name="compressValue">The compress value.</param>
  148. /// <param name="password">The password.</param>
  149. /// <param name="encoding">The encoding.</param>
  150. /// <param name="method">The method.</param>
  151. /// <returns></returns>
  152. public static string DecompressString(string compressValue, string password, Encoding encoding, CompressMethod method = CompressMethod.Deflate)
  153. {
  154. return encoding.GetString(DecompressData(Encoding.GetEncoding(850).GetBytes(compressValue), password: password, method: method));
  155. }
  156. /// <summary>
  157. /// Decompresses the string.
  158. /// </summary>
  159. /// <param name="value">The value.</param>
  160. /// <param name="method">The method.</param>
  161. /// <returns></returns>
  162. public static string DecompressString(string value, CompressMethod method = CompressMethod.Deflate)
  163. {
  164. return ConvertHelper.ToString(DecompressData(Encoding.GetEncoding(850).GetBytes(value), method));
  165. }
  166. /// <summary>
  167. /// Decompresses the string.
  168. /// </summary>
  169. /// <param name="value">The value.</param>
  170. /// <param name="password">The password.</param>
  171. /// <param name="method">The method.</param>
  172. /// <returns></returns>
  173. public static string DecompressString(string value, string password, CompressMethod method = CompressMethod.Deflate)
  174. {
  175. return ConvertHelper.ToString(DecompressData(Encoding.GetEncoding(850).GetBytes(value), password: password, method: method));
  176. }
  177. }
  178. }