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分别进