使用SharpZipLib压缩序列化对象

如题


测试代码如下:



string str_org = "aaaaaaaaaabbbbbbbbbbbbcccccccccdddddddd";
            System.IO.MemoryStream m = new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(str_org));
            m.Position = 0;
            System.IO.MemoryStream zip;
            QouShui.DLL.Message.Serializes.Serialize(m, out zip);
            Console.WriteLine(m.Length);
            Console.WriteLine(zip.Length);
            object o;
            zip.Position = 0;
            QouShui.DLL.Message.Serializes.DeSerialize(zip, out  o);
            Console.WriteLine(System.Text.Encoding.ASCII.GetString( (o as System.IO.MemoryStream).ToArray()));





using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Compression;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace QouShui.DLL.Message
{
    public class Serializes
    {
        /// <summary>
        /// 序列化任意对象

        /// </summary>
        /// <param name="graphy">对象</param>
        /// <param name="stream">序列化后的数据流</param>
        /// <returns></returns>
        public static bool Serialize(object graphy, out System.IO.MemoryStream stream)
        {
            stream = new System.IO.MemoryStream();

            try
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                sf.Serialize(stream, graphy);
                //压缩数据
                System.IO.MemoryStream mem = new MemoryStream();
                using (ZipOutputStream ZipStream = new ZipOutputStream(mem))
                {
                    ZipEntry ZipEntry = new ZipEntry("ZippedFile");
                    ZipStream.PutNextEntry(ZipEntry);
                    ZipStream.SetLevel(6);
                    ZipStream.Write(stream.ToArray(), 0, (int)stream.Length);
                    ZipStream.Finish();
                    stream.SetLength(0);
                    stream.Write(mem.ToArray(), 0, (int)mem.Length);
                    ZipStream.Close();
                }
                
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 反序列化流为对象
        /// </summary>
        /// <param name="stream">流</param>
        /// <param name="graphy">对象</param>
        /// <returns></returns>
        public static bool DeSerialize(System.IO.Stream stream, out object graphy)
        {
            graphy = null;
            if (stream.Position < stream.Length)
            {

                byte[] bs = new byte[stream.Length - stream.Position];
                int i = stream.Read(bs, (int)stream.Position, (int)bs.Length);
                return DeSerialize(bs, out graphy);
            }
            else
                return false;

        }

        public static bool DeSerialize(byte[] data, out object graphy)
        {
            graphy = null;
            try
            {
                System.IO.MemoryStream unzipStrem = new MemoryStream();
                using (ZipInputStream s = new ZipInputStream(new System.IO.MemoryStream(data)))
                {
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        int size = 2048;
                        byte[] bs = new byte[2048];
                        while (true)
                        {
                            size = s.Read(bs, 0, bs.Length);
                            if (size == 0)
                                break;
                            unzipStrem.Write(bs, 0, size);
                        }
                    }

                    s.Close();
                }
                unzipStrem.Position  = 0;
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sa = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                graphy = sa.Deserialize(unzipStrem);
                return true;

            }
            catch
            {
                return false;
            }
        }

    }
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值