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