using System.IO;
using System.IO.Compression;
public static class Compression
{
public static byte[] Compress(byte[] data)
{
using (var ms = new MemoryStream())
{
using (var gzip = new GZipStream(ms, CompressionLevel.Optimal))
{
gzip.Write(data, 0, data.Length);
}
data = ms.ToArray();
}
return data;
}
public static byte[] Decompress(byte[] data)
{
// the trick is to read the last 4 bytes to get the length
// gzip appends this to the array when compressing
var lengthBuffer = new byte[4];
Array.Copy(data, data.Length - 4, lengthBuffer, 0, 4);
int uncompressedSize = BitConverter.ToInt32(lengthBuffer, 0);
var buffer = new byte[uncompressedSize];
using (var ms = new MemoryStream(data))
{
using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
{
gzip.Read(buffer, 0, uncompressedSize);
}
}
return buffer;
}
}
Thursday, June 25, 2015
C# byte [ ] Array Compress and Decompress with GZipStream
A small utility class for compressing and decompressing byte [ ] array in C#
Subscribe to:
Post Comments (Atom)
Casinos in the UK - How to find good games - GrizzGo
ReplyDeleteSo, what do we mean by “casinos air jordan 18 retro red suede online free shipping in the UK”? air jordan 18 retro online shop to find a casino and live casino 한게임 포커 apk games on a air jordan 18 retro red to me mobile phone device air jordan 18 retro red suede clearance in 2021.