Utility.Compression.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System;
  8. using System.IO;
  9. namespace GameFramework
  10. {
  11. public static partial class Utility
  12. {
  13. /// <summary>
  14. /// 压缩解压缩相关的实用函数。
  15. /// </summary>
  16. public static partial class Compression
  17. {
  18. private static ICompressionHelper s_CompressionHelper = null;
  19. /// <summary>
  20. /// 设置压缩解压缩辅助器。
  21. /// </summary>
  22. /// <param name="compressionHelper">要设置的压缩解压缩辅助器。</param>
  23. public static void SetCompressionHelper(ICompressionHelper compressionHelper)
  24. {
  25. s_CompressionHelper = compressionHelper;
  26. }
  27. /// <summary>
  28. /// 压缩数据。
  29. /// </summary>
  30. /// <param name="bytes">要压缩的数据的二进制流。</param>
  31. /// <returns>压缩后的数据的二进制流。</returns>
  32. public static byte[] Compress(byte[] bytes)
  33. {
  34. if (bytes == null)
  35. {
  36. throw new GameFrameworkException("Bytes is invalid.");
  37. }
  38. return Compress(bytes, 0, bytes.Length);
  39. }
  40. /// <summary>
  41. /// 压缩数据。
  42. /// </summary>
  43. /// <param name="bytes">要压缩的数据的二进制流。</param>
  44. /// <param name="compressedStream">压缩后的数据的二进制流。</param>
  45. /// <returns>是否压缩数据成功。</returns>
  46. public static bool Compress(byte[] bytes, Stream compressedStream)
  47. {
  48. if (bytes == null)
  49. {
  50. throw new GameFrameworkException("Bytes is invalid.");
  51. }
  52. return Compress(bytes, 0, bytes.Length, compressedStream);
  53. }
  54. /// <summary>
  55. /// 压缩数据。
  56. /// </summary>
  57. /// <param name="bytes">要压缩的数据的二进制流。</param>
  58. /// <param name="offset">要压缩的数据的二进制流的偏移。</param>
  59. /// <param name="length">要压缩的数据的二进制流的长度。</param>
  60. /// <returns>压缩后的数据的二进制流。</returns>
  61. public static byte[] Compress(byte[] bytes, int offset, int length)
  62. {
  63. using (MemoryStream compressedStream = new MemoryStream())
  64. {
  65. if (Compress(bytes, offset, length, compressedStream))
  66. {
  67. return compressedStream.ToArray();
  68. }
  69. else
  70. {
  71. return null;
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// 压缩数据。
  77. /// </summary>
  78. /// <param name="bytes">要压缩的数据的二进制流。</param>
  79. /// <param name="offset">要压缩的数据的二进制流的偏移。</param>
  80. /// <param name="length">要压缩的数据的二进制流的长度。</param>
  81. /// <param name="compressedStream">压缩后的数据的二进制流。</param>
  82. /// <returns>是否压缩数据成功。</returns>
  83. public static bool Compress(byte[] bytes, int offset, int length, Stream compressedStream)
  84. {
  85. if (s_CompressionHelper == null)
  86. {
  87. throw new GameFrameworkException("Compressed helper is invalid.");
  88. }
  89. if (bytes == null)
  90. {
  91. throw new GameFrameworkException("Bytes is invalid.");
  92. }
  93. if (offset < 0 || length < 0 || offset + length > bytes.Length)
  94. {
  95. throw new GameFrameworkException("Offset or length is invalid.");
  96. }
  97. if (compressedStream == null)
  98. {
  99. throw new GameFrameworkException("Compressed stream is invalid.");
  100. }
  101. try
  102. {
  103. return s_CompressionHelper.Compress(bytes, offset, length, compressedStream);
  104. }
  105. catch (Exception exception)
  106. {
  107. if (exception is GameFrameworkException)
  108. {
  109. throw;
  110. }
  111. throw new GameFrameworkException(Text.Format("Can not compress with exception '{0}'.", exception), exception);
  112. }
  113. }
  114. /// <summary>
  115. /// 压缩数据。
  116. /// </summary>
  117. /// <param name="stream">要压缩的数据的二进制流。</param>
  118. /// <returns>压缩后的数据的二进制流。</returns>
  119. public static byte[] Compress(Stream stream)
  120. {
  121. using (MemoryStream compressedStream = new MemoryStream())
  122. {
  123. if (Compress(stream, compressedStream))
  124. {
  125. return compressedStream.ToArray();
  126. }
  127. else
  128. {
  129. return null;
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// 压缩数据。
  135. /// </summary>
  136. /// <param name="stream">要压缩的数据的二进制流。</param>
  137. /// <param name="compressedStream">压缩后的数据的二进制流。</param>
  138. /// <returns>是否压缩数据成功。</returns>
  139. public static bool Compress(Stream stream, Stream compressedStream)
  140. {
  141. if (s_CompressionHelper == null)
  142. {
  143. throw new GameFrameworkException("Compressed helper is invalid.");
  144. }
  145. if (stream == null)
  146. {
  147. throw new GameFrameworkException("Stream is invalid.");
  148. }
  149. if (compressedStream == null)
  150. {
  151. throw new GameFrameworkException("Compressed stream is invalid.");
  152. }
  153. try
  154. {
  155. return s_CompressionHelper.Compress(stream, compressedStream);
  156. }
  157. catch (Exception exception)
  158. {
  159. if (exception is GameFrameworkException)
  160. {
  161. throw;
  162. }
  163. throw new GameFrameworkException(Text.Format("Can not compress with exception '{0}'.", exception), exception);
  164. }
  165. }
  166. /// <summary>
  167. /// 解压缩数据。
  168. /// </summary>
  169. /// <param name="bytes">要解压缩的数据的二进制流。</param>
  170. /// <returns>解压缩后的数据的二进制流。</returns>
  171. public static byte[] Decompress(byte[] bytes)
  172. {
  173. if (bytes == null)
  174. {
  175. throw new GameFrameworkException("Bytes is invalid.");
  176. }
  177. return Decompress(bytes, 0, bytes.Length);
  178. }
  179. /// <summary>
  180. /// 解压缩数据。
  181. /// </summary>
  182. /// <param name="bytes">要解压缩的数据的二进制流。</param>
  183. /// <param name="decompressedStream">解压缩后的数据的二进制流。</param>
  184. /// <returns>是否解压缩数据成功。</returns>
  185. public static bool Decompress(byte[] bytes, Stream decompressedStream)
  186. {
  187. if (bytes == null)
  188. {
  189. throw new GameFrameworkException("Bytes is invalid.");
  190. }
  191. return Decompress(bytes, 0, bytes.Length, decompressedStream);
  192. }
  193. /// <summary>
  194. /// 解压缩数据。
  195. /// </summary>
  196. /// <param name="bytes">要解压缩的数据的二进制流。</param>
  197. /// <param name="offset">要解压缩的数据的二进制流的偏移。</param>
  198. /// <param name="length">要解压缩的数据的二进制流的长度。</param>
  199. /// <returns>解压缩后的数据的二进制流。</returns>
  200. public static byte[] Decompress(byte[] bytes, int offset, int length)
  201. {
  202. using (MemoryStream decompressedStream = new MemoryStream())
  203. {
  204. if (Decompress(bytes, offset, length, decompressedStream))
  205. {
  206. return decompressedStream.ToArray();
  207. }
  208. else
  209. {
  210. return null;
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// 解压缩数据。
  216. /// </summary>
  217. /// <param name="bytes">要解压缩的数据的二进制流。</param>
  218. /// <param name="offset">要解压缩的数据的二进制流的偏移。</param>
  219. /// <param name="length">要解压缩的数据的二进制流的长度。</param>
  220. /// <param name="decompressedStream">解压缩后的数据的二进制流。</param>
  221. /// <returns>是否解压缩数据成功。</returns>
  222. public static bool Decompress(byte[] bytes, int offset, int length, Stream decompressedStream)
  223. {
  224. if (s_CompressionHelper == null)
  225. {
  226. throw new GameFrameworkException("Compressed helper is invalid.");
  227. }
  228. if (bytes == null)
  229. {
  230. throw new GameFrameworkException("Bytes is invalid.");
  231. }
  232. if (offset < 0 || length < 0 || offset + length > bytes.Length)
  233. {
  234. throw new GameFrameworkException("Offset or length is invalid.");
  235. }
  236. if (decompressedStream == null)
  237. {
  238. throw new GameFrameworkException("Decompressed stream is invalid.");
  239. }
  240. try
  241. {
  242. return s_CompressionHelper.Decompress(bytes, offset, length, decompressedStream);
  243. }
  244. catch (Exception exception)
  245. {
  246. if (exception is GameFrameworkException)
  247. {
  248. throw;
  249. }
  250. throw new GameFrameworkException(Text.Format("Can not decompress with exception '{0}'.", exception), exception);
  251. }
  252. }
  253. /// <summary>
  254. /// 解压缩数据。
  255. /// </summary>
  256. /// <param name="stream">要解压缩的数据的二进制流。</param>
  257. /// <returns>是否解压缩数据成功。</returns>
  258. public static byte[] Decompress(Stream stream)
  259. {
  260. using (MemoryStream decompressedStream = new MemoryStream())
  261. {
  262. if (Decompress(stream, decompressedStream))
  263. {
  264. return decompressedStream.ToArray();
  265. }
  266. else
  267. {
  268. return null;
  269. }
  270. }
  271. }
  272. /// <summary>
  273. /// 解压缩数据。
  274. /// </summary>
  275. /// <param name="stream">要解压缩的数据的二进制流。</param>
  276. /// <param name="decompressedStream">解压缩后的数据的二进制流。</param>
  277. /// <returns>是否解压缩数据成功。</returns>
  278. public static bool Decompress(Stream stream, Stream decompressedStream)
  279. {
  280. if (s_CompressionHelper == null)
  281. {
  282. throw new GameFrameworkException("Compressed helper is invalid.");
  283. }
  284. if (stream == null)
  285. {
  286. throw new GameFrameworkException("Stream is invalid.");
  287. }
  288. if (decompressedStream == null)
  289. {
  290. throw new GameFrameworkException("Decompressed stream is invalid.");
  291. }
  292. try
  293. {
  294. return s_CompressionHelper.Decompress(stream, decompressedStream);
  295. }
  296. catch (Exception exception)
  297. {
  298. if (exception is GameFrameworkException)
  299. {
  300. throw;
  301. }
  302. throw new GameFrameworkException(Text.Format("Can not decompress with exception '{0}'.", exception), exception);
  303. }
  304. }
  305. }
  306. }
  307. }