Created
October 18, 2013 12:42
-
-
Save SuperRembo/7040951 to your computer and use it in GitHub Desktop.
Simple ZLIB compress class that wraps Ionic.Zlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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