public static class Zip
{
public static byte[] Decompress(byte[] zippedData)
{
byte[] decompressedData = null;
using (MemoryStream outputStream = new MemoryStream())
{
using (MemoryStream inputStream = new MemoryStream(zippedData))
{
using (GZipStream zip = new GZipStream(inputStream, CompressionMode.Decompress))
{
zip.CopyTo(outputStream);
}
}
decompressedData = outputStream.ToArray();
}
return decompressedData;
}
public static byte[] Compress(byte[] plainData)
{
byte[] compressesData = null;
using (MemoryStream outputStream = new MemoryStream())
{
using (GZipStream zip = new GZipStream(outputStream, CompressionMode.Compress))
{
zip.Write(plainData, 0, plainData.Length);
}
//Dont get the MemoryStream data before the GZipStream is closed
//since it doesn’t yet contain complete compressed data.
//GZipStream writes additional data including footer information when its been disposed
compressesData = outputStream.ToArray();
}
return compressesData;
}
}
And MS Test
[TestClass]
public class ZipTEST
{
[TestMethod]
public void TestMethod1()
{
string text = "Hello World";
byte [] data = System.Text.Encoding.UTF8.GetBytes(text);
byte [] compressedData = Zip.Compress(data);
byte [] decompressedData = Zip.Decompress(compressedData);
string textAfterComDecompress = System.Text.Encoding.UTF8.GetString(decompressedData);
Assert.AreEqual<string>(text, textAfterComDecompress);
}
}
Žiadne komentáre:
Zverejnenie komentára