DataSet GZip/Deflate Serializer


namespace Test
{
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;
    using System.Data;
    using System;
    using Microshaoft;
    [WebService(Namespace="http://www.microshaoft.com/")]
    public class Service1Class : WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public byte[] GetData()
        {
            DataSet ds = new DataSet();
            //populate dataset to do
            return DataSetSerializer.DataSetToGZipBytes(ds);
        }
        static void Main()
        {
            //
            // TODO: 在此处添加代码以启动应用程序
            //
            byte[] data = DataSetSerializer.DataSetToGZipBytes(new DataSet());
            Console.WriteLine(data.Length);
            DataSet ds = DataSetSerializer.GZipBytesToDataSet(data);
            Console.WriteLine("Hello World");
            Console.WriteLine(Environment.Version.ToString());
        }
    }
}

namespace Microshaoft
{
    using System;
    using System.IO;
    using System.Data;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    public class DataSetSerializer
    {
        //Server
        static public byte[] DataSetToGZipBytes(DataSet dataSet)
        {
            MemoryStream ms= new MemoryStream();
            IFormatter f = new BinaryFormatter();
            dataSet.RemotingFormat = SerializationFormat.Binary;
            f.Serialize(ms, dataSet);
            byte[] data = ms.ToArray();
            ms.Close();
            ms.Dispose();
            ms = null;
            return CompressHelper.GZipCompress(data);
        }
        //Client
        static public DataSet GZipBytesToDataSet(byte[] data)
        {
            data = CompressHelper.GZipDecompress(data);
            MemoryStream ms = new MemoryStream(data);
            IFormatter f = new BinaryFormatter();
            DataSet dataSet = f.Deserialize(ms) as DataSet;
            ms.Close();
            ms.Dispose();
            ms = null;
            return dataSet;
        }
    }
}
namespace Microshaoft
{
    using System;
    using System.Text;
    using System.IO;
    using System.IO.Compression;
    /// <summary>
    /// Crypto 的摘要说明。
    /// </summary>
    public class CompressHelper
    {
        private static byte[] ReadStreamToBytes(Stream stream)
        {
            byte[] buffer = new byte[64 * 1024];
            MemoryStream ms = new MemoryStream();
            int r = 0;
            int l = 0;
            long position = -1;
            if (stream.CanSeek)
            {
                position = stream.Position;
                stream.Position = 0;
            }
            while (true)
            {
                r = stream.Read(buffer, 0, buffer.Length);
                if (r > 0)
                {
                    l += r;
                    ms.Write(buffer, 0, r);
                }
                else
                {
                    break;
                }
            }
            byte[] bytes = new byte[l];
            ms.Position = 0;
            ms.Read(bytes, 0, (int) l);
            ms.Close();
            ms = null;
            if (position >= 0)
            {
                stream.Position = position;
            }
            return bytes;
        }
        public static byte[] GZipCompress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true);
            stream.Write(data, 0, data.Length);
            stream.Close();
            stream.Dispose();
            stream = null;
            byte[] buffer = ReadStreamToBytes(ms);
            ms.Close();
            ms.Dispose();
            ms = null;
            return buffer;
        }
        public static byte[] GZipDecompress(byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);
            GZipStream stream = new GZipStream(ms, CompressionMode.Decompress);
            byte[] buffer = ReadStreamToBytes(stream);
            ms.Close();
            ms.Dispose();
            ms = null;
            stream.Close();
            stream.Dispose();
            stream = null;
            return buffer;
        }
        public static byte[] DeflateCompress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true);
            stream.Write(data, 0, data.Length);
            stream.Close();
            stream.Dispose();
            stream = null;
            byte[] buffer = ReadStreamToBytes(ms);
            ms.Close();
            ms.Dispose();
            ms = null;
            return buffer;
        }
        public static byte[] DeflateDecompress(byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);
            DeflateStream stream = new DeflateStream(ms, CompressionMode.Decompress);
            byte[] buffer = ReadStreamToBytes(stream);
            ms.Close();
            ms.Dispose();
            ms = null;
            stream.Close();
            stream.Dispose();
            stream = null;
            return buffer;
        }
    }
}

你可能感兴趣的:(Serialize)