You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Text;
  6. namespace Apewer.Internals
  7. {
  8. internal class GzipHelper
  9. {
  10. /// <summary>对数据进行 GZip 压缩。</summary>
  11. public static byte[] ToGzip(byte[] bytes)
  12. {
  13. if (bytes == null) return Constant.EmptyBytes;
  14. if (bytes.Length == 0) return Constant.EmptyBytes;
  15. byte[] result;
  16. var output = new MemoryStream();
  17. var zip = new GZipStream(output, CompressionMode.Compress, true);
  18. zip.Write(bytes, 0, bytes.Length);
  19. zip.Close();
  20. zip.Dispose();
  21. result = output.ToArray();
  22. output.Close();
  23. output.Dispose();
  24. return result;
  25. }
  26. /// <summary>对数据进行 GZip 解压。</summary>
  27. public static byte[] FromGzip(byte[] bytes)
  28. {
  29. if (bytes == null) return Constant.EmptyBytes;
  30. if (bytes.Length == 0) return Constant.EmptyBytes;
  31. byte[] result;
  32. var input = new MemoryStream(bytes);
  33. var output = new MemoryStream();
  34. input.Position = 0;
  35. var zip = new GZipStream(input, CompressionMode.Decompress, true);
  36. StreamHelper.Read(zip, output, 64);
  37. result = output.ToArray();
  38. zip.Close();
  39. zip.Dispose();
  40. input.Close();
  41. input.Dispose();
  42. output.Close();
  43. output.Dispose();
  44. return result;
  45. }
  46. }
  47. }