www.pudn.com > SerializationMobile.rar > Form1.cs, change:2008-12-03,size:6756b


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Xml.Serialization; 
using System.IO; 
using System.Collections; 
 
using BusinessEntities; 
using ComponentAce.Compression.Libs.zlib; 
using System.Security.Cryptography; 
 
namespace SerializationMobile 
{ 
    public partial class Form1 : Form 
    { 
        private Customer customer; 
        private Customer[] customers; 
        private byte[] xmlBuffer; 
        private byte[] binBuffer; 
 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private Customer NewCustomer() 
        { 
            Customer customer = new Customer(); 
            customer.ID = new Random().Next(); 
            customer.Name = "Customer" + customer.ID; 
            customer.Phone = Guid.NewGuid().ToString(); 
            customer.Address = Guid.NewGuid().ToString(); 
 
            return customer; 
        } 
 
        private Customer[] NewCustomers() 
        { 
            ArrayList list = new ArrayList(); 
            for (int i = 1; i < 5000; i++) 
            { 
                list.Add(NewCustomer()); 
            } 
 
            return list.ToArray(typeof(Customer)) as Customer[]; 
        } 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            Cursor.Current = Cursors.WaitCursor; 
 
            customer = NewCustomer(); 
            customers = NewCustomers(); 
 
            Cursor.Current = Cursors.Default; 
        } 
 
        private void btnXmlSer_Click(object sender, EventArgs e) 
        { 
            using (MemoryStream ms = new MemoryStream()) 
            { 
                Cursor.Current = Cursors.WaitCursor; 
                DateTime startTime = DateTime.Now; 
 
                XmlSerializer ser = new XmlSerializer(typeof(Customer)); 
                ser.Serialize(ms, customers); 
 
 
 
                DateTime endTime = DateTime.Now; 
 
                xmlBuffer = ms.ToArray(); 
 
                if (checkBox1.Checked) 
                    xmlBuffer = CompressBytes(xmlBuffer); 
 
                txtResult.Text = Encoding.ASCII.GetString(xmlBuffer, 0, xmlBuffer.Length); 
                Cursor.Current = Cursors.Default; 
 
                MessageBox.Show(string.Format("字节数:{0}\n耗时:{1}", xmlBuffer.Length, (endTime - startTime))); 
            } 
        } 
 
        private void btnXmlDeser_Click(object sender, EventArgs e) 
        { 
            using (MemoryStream ms = new MemoryStream(xmlBuffer)) 
            { 
                Cursor.Current = Cursors.WaitCursor; 
                DateTime startTime = DateTime.Now; 
 
                XmlSerializer ser = new XmlSerializer(typeof(Customer)); 
                customers = ser.Deserialize(ms) as Customer[]; 
 
                DateTime endTime = DateTime.Now; 
                Cursor.Current = Cursors.Default; 
 
                MessageBox.Show(string.Format("耗时:{0}", (endTime - startTime))); 
            } 
        } 
 
        private void btnBinarySer_Click(object sender, EventArgs e) 
        { 
            using (MemoryStream ms = new MemoryStream()) 
            { 
                Cursor.Current = Cursors.WaitCursor; 
                DateTime startTime = DateTime.Now; 
 
                CompactFormatter.CompactFormatter ser = new CompactFormatter.CompactFormatter(); 
                ser.Serialize(ms, customers); 
 
                DateTime endTime = DateTime.Now; 
 
                binBuffer = ms.ToArray(); 
                txtResult.Text = ""; 
                Cursor.Current = Cursors.Default; 
 
                MessageBox.Show(string.Format("字节数:{0}\n耗时:{1}", binBuffer.Length, (endTime - startTime))); 
            } 
        } 
 
        private void btnBinaryDeser_Click(object sender, EventArgs e) 
        { 
            using (MemoryStream ms = new MemoryStream(binBuffer)) 
            { 
                Cursor.Current = Cursors.WaitCursor; 
                DateTime startTime = DateTime.Now; 
 
                CompactFormatter.CompactFormatter ser = new CompactFormatter.CompactFormatter(); 
                customers = ser.Deserialize(ms) as Customer[]; 
 
                DateTime endTime = DateTime.Now; 
 
                Cursor.Current = Cursors.Default; 
 
                MessageBox.Show(string.Format("耗时:{0}", (endTime - startTime))); 
            } 
        } 
 
        public byte[] CompressBytes(byte[] SourceByte) 
        { 
            try 
            { 
                MemoryStream stmInput = new MemoryStream(SourceByte); 
                Stream stmOutPut = CompressStream(stmInput); 
                byte[] bytOutPut = new byte[stmOutPut.Length]; 
                stmOutPut.Position = 0; 
                stmOutPut.Read(bytOutPut, 0, bytOutPut.Length); 
                return bytOutPut; 
            } 
            catch 
            { 
                return null; 
            } 
        } 
 
        public  Stream CompressStream(Stream SourceStream) 
        { 
            try 
            { 
                MemoryStream stmOutTemp = new MemoryStream(); 
                ZOutputStream outZStream = new ZOutputStream(stmOutTemp, zlibConst.Z_DEFAULT_COMPRESSION); 
                CopyStream(SourceStream, outZStream); 
                outZStream.finish(); 
                return stmOutTemp; 
            } 
            catch 
            { 
                return null; 
            } 
        } 
 
        private static void CopyStream(Stream input, Stream output) 
        { 
            byte[] buffer = new byte[10000]; 
            int len; 
            while ((len = input.Read(buffer, 0, buffer.Length)) > 0) 
            { 
                output.Write(buffer, 0, len); 
            } 
            output.Flush(); 
        } 
 
        private void button1_Click(object sender, EventArgs e) 
        { 
             
            OpenFileDialog op = new OpenFileDialog(); 
            op.ShowDialog(); 
            string path = op.FileName; 
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); 
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 
            byte[] md5byte = md5.ComputeHash(fs); 
            int i, j; 
            string md5str = string.Empty; 
            foreach (byte b in md5byte) 
            { 
                i = Convert.ToInt32(b); 
                j = i >> 4; 
                md5str += (Convert.ToString(j, 16)); 
                j = ((i << 4) & 0x00ff) >> 4; 
                md5str += (Convert.ToString(j, 16)); 
            } 
            txtResult.Text = md5str; 
 
        } 
 
    } 
       
}