Compress/uncompress
namespace Qxado.IO
{
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
/// <summary>
/// ???/?????????
/// </summary>
public class CompressHelper
{
#region ICompress ???
public byte[] Compress(object obj)
{
if (obj == null) return new byte[0];
MemoryStream ms = new MemoryStream();
DeflateStream zip = new DeflateStream(ms, CompressionMode.Compress, true);
try
{
BinaryFormatter serializer = new BinaryFormatter();
serializer.Serialize(zip, obj);
zip.Close();
byte[] ary = ms.ToArray();
ms.Close();
return ary;
}
finally
{
zip.Close();
ms.Close();
}
}
public object DeCompress(byte[] bytes)
{
if (bytes == null) return null;
if (bytes.Length == 0) return null;
MemoryStream ms = new MemoryStream(bytes);
DeflateStream UnZip = new DeflateStream(ms, CompressionMode.Decompress);
try
{
BinaryFormatter serializer = new BinaryFormatter();
object obj = serializer.Deserialize(UnZip);
UnZip.Close();
ms.Close();
return obj;
}
finally
{
UnZip.Close();
ms.Close();
}
}
#endregion
}
}
Related examples in the same category