C#中使用SharpZipLib进行解压缩并使用ProtoBuf进行反序列化
生活随笔
收集整理的這篇文章主要介紹了
C#中使用SharpZipLib进行解压缩并使用ProtoBuf进行反序列化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
C#中將list使用ProtoBuf進行序列化并使用SharpZipLib進行壓縮:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99941079
ICSharpCode.SharpZipLib.dll 下載:
https://download.csdn.net/download/badao_liumang_qizhi/11586902
實現
在上面文章實現序列化和壓縮的基礎上,再進行解壓縮和反序列化。
在form窗體上拖拽Button,雙擊進入其點擊事件。
private void button10_Click(object sender, EventArgs e){DateTime begin = DateTime.Now;Console.WriteLine(" ProtoBuf 解壓縮并反序列化開始:" + begin.ToString("yyyy-MM-dd HH:mm:ss"));try{int bufferSize = 40960;//打開文件FileStream fs = File.OpenRead(@"E:\testdata1\Record4.zip");?//設置文件流的位置fs.Position = 0;//通過ZIpInputStream得到zip文件的對象????????????????????????????????????????????????ZipInputStream zipInputStream = new ZipInputStream(fs, bufferSize);//通過zip.getNextEntry()來得到ZipEntry對象zipInputStream.GetNextEntry();//定義數據緩沖byte[] buffer = new byte[bufferSize];//定義讀取位置??????????????????int offset = 0;//定義內存流???????????????????????????????MemoryStream ms = new MemoryStream();????????????????????while ((offset = zipInputStream.Read(buffer, 0, buffer.Length)) != 0){//解壓后的數據寫入內存流ms.Write(buffer, 0, offset);????????????????????????????????????????????}//設置內存流的位置ms.Position = 0;//ProtoBuf反序列化this.requestList = ProtoBuf.Serializer.Deserialize<List<Request>>(ms);????????????????ms.Close();ms.Dispose();//關閉流fs.Close();//釋放對象????????????????????????????????????fs.Dispose();???????????????????????????????????????????????????????????????}catch (Exception ex){Console.WriteLine(ex.Message);}DateTime end = DateTime.Now;TimeSpan ts = end - begin;Console.WriteLine(" ProtoBuf 解壓縮并反序列化結束" + end.ToString("yyyy-MM-dd HH:mm:ss"));Console.WriteLine("共花費" + ts.TotalSeconds);foreach (Request p in requestList){Console.WriteLine("Id:" + p.id + "密碼:" + p.password);}}效果
?
總結
以上是生活随笔為你收集整理的C#中使用SharpZipLib进行解压缩并使用ProtoBuf进行反序列化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#中使用SharpZipLib进行解压
- 下一篇: C#中使用二进制和ProtoBuf分别进