GZIPHelper.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Text;
  6. namespace Taobao.Top.Link.Util
  7. {
  8. /// <summary>zip helper compatible with java version
  9. /// </summary>
  10. public class GZIPHelper
  11. {
  12. public static byte[] Zip(byte[] value)
  13. {
  14. using (var stream = new MemoryStream())
  15. using (var zip = new GZipStream(stream, CompressionMode.Compress))
  16. {
  17. zip.Write(value, 0, value.Length);
  18. zip.Close();
  19. return stream.ToArray();
  20. }
  21. }
  22. public static byte[] Unzip(byte[] value)
  23. {
  24. using (var stream = new MemoryStream(value))
  25. using (var zip = new GZipStream(stream, CompressionMode.Decompress))
  26. using (var unzip = new MemoryStream())
  27. {
  28. var buffer = new byte[1024];
  29. var r = 0;
  30. while ((r = zip.Read(buffer, 0, buffer.Length)) > 0)
  31. unzip.Write(buffer, 0, r);
  32. return unzip.ToArray();
  33. }
  34. }
  35. }
  36. }