C#中将list使用ProtoBuf进行序列化并使用SharpZipLib进行压缩
生活随笔
收集整理的這篇文章主要介紹了
C#中将list使用ProtoBuf进行序列化并使用SharpZipLib进行压缩
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
C#中使用ProtoBuf提高序列化速度對比二進制序列化:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99850052
ICSharpCode.SharpZipLib.dll 下載:
https://download.csdn.net/download/badao_liumang_qizhi/11586902
C#中將list進行序列化并使用SharpZipLib進行壓縮:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99940095
實現
參照第一篇文章引進ProBuf,然后下載ICSharpCode.SharpZipLib.dll,再參照第三篇文章
引用ICSharpCode。
在窗體上拖拽Button按鈕,然后雙擊進點擊事件。
private void button8_Click(object sender, EventArgs e){DateTime begin = DateTime.Now;Console.WriteLine("ProtoBuf壓縮開始" + begin.ToString("yyyy-MM-dd HH:mm:ss"));//初始化數據--創建10000個對象listfor (int i = 0; i < 10000; i++){requestList.Add(new Request() {id=i,password="密碼"+i});}try{//創建內存流對象MemoryStream ms = new MemoryStream();//序列化對象ProtoBuf.Serializer.Serialize<List<Request>>(ms, this.requestList);//把內存流對象寫入字節數組byte[] buffer = ms.ToArray();//關閉內存流對象????ms.Close();//釋放資源????????????????????????ms.Dispose();????????????????????????????????????????????????????????????//創建文件FileStream fs = File.Create(@"E:\testdata1\Record4.zip");//創建zip輸出流ZipOutputStream zipOutputStream = new ZipOutputStream(fs, buffer.Length);//ZipEntry用于表示Zip文件條目 --將會在壓縮文件中創建Record4.data文件ZipEntry entry = new ZipEntry("Record4.data");//將其放進壓縮文件中zipOutputStream.PutNextEntry(entry);//將字節數組寫入文件zipOutputStream.Write(buffer, 0, buffer.Length);zipOutputStream.Finish();zipOutputStream.Close();zipOutputStream.Dispose();//關閉流fs.Close();//釋放對象????????????????????????fs.Dispose();???????????????????????????????????????????????????????}catch (Exception ex){Console.WriteLine(ex.Message);}DateTime end = DateTime.Now;TimeSpan ts = end - begin;Console.WriteLine("ProBuf壓縮結束" + end.ToString("yyyy-MM-dd HH:mm:ss"));Console.WriteLine("共花費 " + ts.TotalSeconds);}效果
?
將其解壓
?
總結
以上是生活随笔為你收集整理的C#中将list使用ProtoBuf进行序列化并使用SharpZipLib进行压缩的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#中将list进行序列化并使用Shar
- 下一篇: C#中使用SharpZipLib进行解压