使用SharpZipLib压缩序列化对象

来源:互联网 发布:raphael js api 编辑:程序博客网 时间:2024/06/05 11:17

如题


测试代码如下:



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;            }        }    }}











原创粉丝点击