Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. public static byte[] Compress(byte[] raw)
  2.         {
  3.             using (MemoryStream memory = new MemoryStream())
  4.             {
  5.                 using (GZipStream gzip = new GZipStream(memory,
  6.                     CompressionMode.Compress, true))
  7.                 {
  8.                     gzip.Write(raw, 0, raw.Length);
  9.                 }
  10.                 return memory.ToArray();
  11.             }
  12.         }
  13.        
  14.         static byte[] Decompress(byte[] gzip)
  15.         {
  16.             // Create a GZIP stream with decompression mode.
  17.             // ... Then create a buffer and write into while reading from the GZIP stream.
  18.             using (GZipStream stream = new GZipStream(new MemoryStream(gzip),
  19.                 CompressionMode.Decompress))
  20.             {
  21.                 const int size = 4096;
  22.                 byte[] buffer = new byte[size];
  23.                 using (MemoryStream memory = new MemoryStream())
  24.                 {
  25.                     int count = 0;
  26.                     do
  27.                     {
  28.                         count = stream.Read(buffer, 0, size);
  29.                         if (count > 0)
  30.                         {
  31.                             memory.Write(buffer, 0, count);
  32.                         }
  33.                     }
  34.                     while (count > 0);
  35.                     return memory.ToArray();
  36.                 }
  37.             }
  38.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement