C#序列化/反序列化对象.将对象写入文件,从文件读取对象.
作者:C/S框架网  发布日期:2011-07-16 00:38:22
C#序列化/反序列化对象.将对象写入文件,从文件读取对象.


C#序列化/反序列化对象.将对象写入文件,从文件读取对象.


using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.IO.Compression;
using System.Windows.Forms;

namespace Vjsdn_Tester
{
   public class TestZipObject
   {
      /// <summary>
      /// 测试简单序列化/反序列化对象
      /// </summary>
      public void Test()
      {
         SerilizabledObject obj = new SerilizabledObject();
         byte[] bs = ZipObject.CompressionObject(obj);
         object o = ZipObject.DecompressionObject(bs); //还原,ok
         MessageBox.Show((o as SerilizabledObject).getName());
      }
      
      /// <summary>
      /// 序列化对象,将对象写入文件,然后还原.
      /// </summary>
      public void TestReadWriteFile()
      {
         SerilizabledObject obj = new SerilizabledObject();
         byte[] bs = ZipObject.CompressionObject(obj);
         
         //将数组写入文件
         Stream writer = new FileStream("E:\\test.shape", FileMode.Create, FileAccess.Write, FileShare.Write);
         writer.Write(bs, 0, bs.Length);
         writer.Flush();
         writer.Close();
         
         //读取文件,先写入数组,再从数组转换为对象.Easy!
         FileStream fs = File.Open("E:\\test.shape", FileMode.Open);
         byte[] bss = new byte[fs.Length];
         int i = fs.Read(bss, 0, (int)fs.Length);
         
         object o = ZipObject.DecompressionObject(bss); //还原,ok
         MessageBox.Show((o as SerilizabledObject).getName());
      }
   }
   
   /// <summary>
   /// 测试用对象
   /// </summary>
   [Serializable]
   public class SerilizabledObject
   {
      string id = "id001";
      string name = "www.vjsdn.com";
      
      public SerilizabledObject() { }
      
      public string getName()
      {
         return this.name;
      }
   }
   
   /// <summary>
   /// 压缩解压object
   /// </summary>
   public class ZipObject
   {
      #region 压缩解压object
      
      public static byte[] CompressionObject(object DataOriginal)
      {
         if (DataOriginal == null) return null;
         BinaryFormatter bFormatter = new BinaryFormatter();
         MemoryStream mStream = new MemoryStream();
         bFormatter.Serialize(mStream, DataOriginal);
         byte[] bytes = mStream.ToArray();
         MemoryStream oStream = new MemoryStream();
         DeflateStream zipStream = new DeflateStream(oStream, CompressionMode.Compress);
         zipStream.Write(bytes, 0, bytes.Length);
         zipStream.Flush();
         zipStream.Close();
         return oStream.ToArray();
      }
      
      public static object DecompressionObject(byte[] bytes)
      {
         if (bytes == null) return null;
         MemoryStream mStream = new MemoryStream(bytes);
         mStream.Seek(0, SeekOrigin.Begin);
         DeflateStream unZipStream = new DeflateStream(mStream, CompressionMode.Decompress, true);
         object dsResult = null;
         BinaryFormatter bFormatter = new BinaryFormatter();
         dsResult = (object)bFormatter.Deserialize(unZipStream);
         return dsResult;
      }
      #endregion
   }
}



测试代码:

private void button7_Click(object sender, EventArgs e)
{
   new TestZipObject().Test(); // 测试简单序列化/反序列化对象
   new TestZipObject().TestReadWriteFile(); // 序列化对象,将对象写入文件,然后还原.
}


上一篇 下一篇