Skip to content

Instantly share code, notes, and snippets.

@SuperRembo
Created October 18, 2013 12:42
Show Gist options
  • Save SuperRembo/7040951 to your computer and use it in GitHub Desktop.
Save SuperRembo/7040951 to your computer and use it in GitHub Desktop.
Simple ZLIB compress class that wraps Ionic.Zlib
using System.IO;
using System.Text;
using Ionic.Zlib;
namespace Example
{
public interface ICompressor
{
byte[] Compress(string text);
string Decompress(byte[] bytes);
}
public class ZlibCompressor : ICompressor
{
public byte[] Compress(string text)
{
using (var input = new MemoryStream(Encoding.UTF8.GetBytes(text)))
using (var output = new MemoryStream())
{
using (var zlib = new ZlibStream(output, CompressionMode.Compress))
input.CopyTo(zlib);
return output.ToArray();
}
}
public string Decompress(byte[] bytes)
{
using (var input = new MemoryStream(bytes))
using (var output = new MemoryStream())
{
using (var zlib = new ZlibStream(input, CompressionMode.Decompress))
zlib.CopyTo(output);
return Encoding.UTF8.GetString(output.ToArray());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment