使用SharpZipLib压缩序列化对象
来源:互联网 发布:raphael js api 编辑:程序博客网 时间:2024/06/05 11:17
如题
测试代码如下:
string str_org = "aaaaaaaaaabbbbbbbbbbbbcccccccccdddddddd"; System.IO.MemoryStream m = new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(str_org)); m.Position = 0; System.IO.MemoryStream zip; QouShui.DLL.Message.Serializes.Serialize(m, out zip); Console.WriteLine(m.Length); Console.WriteLine(zip.Length); object o; zip.Position = 0; QouShui.DLL.Message.Serializes.DeSerialize(zip, out o); Console.WriteLine(System.Text.Encoding.ASCII.GetString( (o as System.IO.MemoryStream).ToArray()));
using System;using System.Collections.Generic;using System.Text;using System.IO.Compression;using System.IO;using ICSharpCode.SharpZipLib.Zip;namespace QouShui.DLL.Message{ public class Serializes { /// <summary> /// 序列化任意对象 /// </summary> /// <param name="graphy">对象</param> /// <param name="stream">序列化后的数据流</param> /// <returns></returns> public static bool Serialize(object graphy, out System.IO.MemoryStream stream) { stream = new System.IO.MemoryStream(); try { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); sf.Serialize(stream, graphy); //压缩数据 System.IO.MemoryStream mem = new MemoryStream(); using (ZipOutputStream ZipStream = new ZipOutputStream(mem)) { ZipEntry ZipEntry = new ZipEntry("ZippedFile"); ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(6); ZipStream.Write(stream.ToArray(), 0, (int)stream.Length); ZipStream.Finish(); stream.SetLength(0); stream.Write(mem.ToArray(), 0, (int)mem.Length); ZipStream.Close(); } return true; } catch { return false; } } /// <summary> /// 反序列化流为对象 /// </summary> /// <param name="stream">流</param> /// <param name="graphy">对象</param> /// <returns></returns> public static bool DeSerialize(System.IO.Stream stream, out object graphy) { graphy = null; if (stream.Position < stream.Length) { byte[] bs = new byte[stream.Length - stream.Position]; int i = stream.Read(bs, (int)stream.Position, (int)bs.Length); return DeSerialize(bs, out graphy); } else return false; } public static bool DeSerialize(byte[] data, out object graphy) { graphy = null; try { System.IO.MemoryStream unzipStrem = new MemoryStream(); using (ZipInputStream s = new ZipInputStream(new System.IO.MemoryStream(data))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { int size = 2048; byte[] bs = new byte[2048]; while (true) { size = s.Read(bs, 0, bs.Length); if (size == 0) break; unzipStrem.Write(bs, 0, size); } } s.Close(); } unzipStrem.Position = 0; System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sa = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); graphy = sa.Deserialize(unzipStrem); return true; } catch { return false; } } }}
- 使用SharpZipLib压缩序列化对象
- 使用SharpZipLib实现zip压缩
- 使用SharpZipLib实现zip压缩
- 使用SharpZipLib实现zip压缩
- 使用SharpZipLib实现zip压缩
- 使用SharpZipLib实现zip压缩
- SharpZipLib 的使用(压缩,解压)
- 使用SharpZipLib制作压缩解压缩程序
- C#使用SharpZipLib实现zip压缩
- c#使用SharpZipLib压缩和解压缩文件
- C#解压或压缩文件夹<使用SharpZipLib>
- 使用SharpZipLib.dll动态库实现带密码压缩
- 使用ICSharpCode.SharpZipLib对文件进行压缩或解压
- C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
- 在C#中使用SharpZipLib压缩解压缩zip文件
- 使用SharpZipLib压缩打包多个内存中的文件
- C#使用ICSharpCode.SharpZipLib对文件进行压缩
- C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
- Jxl将Excel中的数据写入数据库--->现成code
- 【PHP性能优化学习】之xhprof安装&&使用
- 中国将首次倡导粮食干部职工体验24小时饥饿-粮食局-24小时饥饿-世界粮食日
- The practice of programming 读书笔记 (一)
- 无法打开物理文件 XXX.mdf"。操作系统错误
- 使用SharpZipLib压缩序列化对象
- 禁止某几个文件用ARC模式编译
- 改了n遍后的结果~~
- 四川渠县政府将小学操场拍卖出售 学生错峰放学-四川-操场-拍卖
- 消息耦合还是接口耦合
- Linux2.6 DM9000驱动模编译为模块
- 框架类的 键盘监控 消退
- uml 简介
- 乔布斯为什么要用Objective-C作为iOS平台编程语言?