CompressUtil.cs 930 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO.Compression;
  7. using System.IO;
  8. namespace Test.TestPress
  9. {
  10. public class CompressUtil
  11. {
  12. public static byte[] GetBytes(string str)
  13. {
  14. byte[] data = Encoding.UTF8.GetBytes(str.ToString());
  15. return data;
  16. }
  17. public static byte[] GZipCompress(byte[] data)
  18. {
  19. byte[] result;
  20. using (MemoryStream ms = new MemoryStream())
  21. {
  22. using (GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true))
  23. {
  24. compressedzipStream.Write(data, 0, data.Length);
  25. compressedzipStream.Close();
  26. }
  27. result= ms.ToArray();
  28. }
  29. return result;
  30. }
  31. }
  32. }