序列化、壓縮、解壓縮、還原序列化對象

來源:互聯網
上載者:User
using System;using System.Collections.Generic;using System.Text;using System.IO;using System.IO.Compression;using System.Xml.Serialization;using System.Xml;using System.Data;using System.Runtime.Serialization.Formatters.Binary;using System.Runtime.Serialization;namespace JTK.IOHelper{    public class SerializerDeserialize    {        public static byte [] Serializer(object obj)        {            IFormatter formatter = new BinaryFormatter();//定義BinaryFormatter以序列化object對象              MemoryStream ms = new MemoryStream();//建立記憶體流對象              formatter.Serialize(ms, obj);//把object對象序列化到記憶體流              byte[] buffer = ms.ToArray();//把記憶體流對象寫入位元組數組              ms.Close();//關閉記憶體流對象              ms.Dispose();//釋放資源              MemoryStream msNew = new MemoryStream();            GZipStream gzipStream = new GZipStream(msNew, CompressionMode.Compress, true);//建立壓縮對象              gzipStream.Write(buffer, 0, buffer.Length);//把壓縮後的資料寫入檔案              gzipStream.Close();//關閉壓縮流,這裡要注意:一定要關閉,要不然解壓縮的時候會出現小於4K的檔案讀取不到資料,大於4K的檔案讀取不完整                          gzipStream.Dispose();//釋放對象              msNew.Close();            msNew.Dispose();            return msNew.ToArray();        }        /// <summary>          /// 還原序列化壓縮的object          /// </summary>          /// <param name="_filePath"></param>          /// <returns></returns>          public static object Deserialize(byte[] bytes)        {            MemoryStream msNew = new MemoryStream(bytes);            msNew.Position = 0;            GZipStream gzipStream = new GZipStream(msNew, CompressionMode.Decompress);//建立解壓對象              byte[] buffer = new byte[4096];//定義資料緩衝              int offset = 0;//定義讀取位置              MemoryStream ms = new MemoryStream();//定義記憶體流              while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)            {                ms.Write(buffer, 0, offset);//解壓後的資料寫入記憶體流              }            BinaryFormatter sfFormatter = new BinaryFormatter();//定義BinaryFormatter以還原序列化object對象              ms.Position = 0;//設定記憶體流的位置              object obj;            try            {                obj = (object)sfFormatter.Deserialize(ms);//還原序列化              }            catch            {                throw;            }            finally            {                ms.Close();//關閉記憶體流                  ms.Dispose();//釋放資源              }            gzipStream.Close();//關閉解壓縮流              gzipStream.Dispose();//釋放資源              msNew.Close();            msNew.Dispose();            return obj;        }      }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.