ZipHelper 修正
来源:互联网 发布:php用户注册登录系统 编辑:程序博客网 时间:2024/04/30 06:00
在我前面的“压缩与解压缩 ZipHelper ”一文中提到了使用ICSharpCode.SharpZipLib.dll库的BZip2OutputStream和BZip2InputStream 来进行数据流的压缩。这几天在我的AgileIM的测试中发现使用BZip2OutputStream和BZip2InputStream 来进行压缩/解压缩并不可靠,有时会出现这样的情况:对A进行压缩得到B,然后对B解压缩,得到C的却与A不等,通常的情况是,C只是A的首部的一部分。我没有研究ICSharpCode.SharpZipLib的源码,所以还没发现是什么原因造成了这种情况。
幸运的是,我发现了一个更好的替代品--仍然是ICSharpCode.SharpZipLib类库中的一个类--BZip2,这个类的使用更方便,而且经过我的不完全测试,其运行的结果是可靠的。所以将ZipHelper的实现修正如下:
幸运的是,我发现了一个更好的替代品--仍然是ICSharpCode.SharpZipLib类库中的一个类--BZip2,这个类的使用更方便,而且经过我的不完全测试,其运行的结果是可靠的。所以将ZipHelper的实现修正如下:
1 public class ZipHelper
2 {
3 public static byte[] Zip(byte[] data)
4 {
5 return Zip(data ,0 ,data.Length) ;
6 }
7
8 public static byte[] Unzip(byte[] data)
9 {
10 return Unzip(data ,0 ,data.Length) ;
11 }
12
13 public static byte[] Zip(byte[] data ,int offset ,int size)
14 {
15 MemoryStream inStream = new MemoryStream(data ,offset ,size);
16 MemoryStream outStream = new MemoryStream() ;
17 BZip2.Compress(inStream ,outStream ,size) ;
18
19 byte[] result = outStream.ToArray() ;
20 inStream.Close() ;
21 outStream.Close() ;
22
23 return result ;
24 }
25
26 public static byte[] Unzip(byte[] data ,int offset ,int size)
27 {
28 MemoryStream inStream = new MemoryStream(data ,offset ,size);
29 MemoryStream outStream = new MemoryStream() ;
30 BZip2.Decompress(inStream ,outStream ) ;
31
32 byte[] result = outStream.ToArray() ;
33 inStream.Close() ;
34 outStream.Close() ;
35
36 return result ;
37 }
38 }
2 {
3 public static byte[] Zip(byte[] data)
4 {
5 return Zip(data ,0 ,data.Length) ;
6 }
7
8 public static byte[] Unzip(byte[] data)
9 {
10 return Unzip(data ,0 ,data.Length) ;
11 }
12
13 public static byte[] Zip(byte[] data ,int offset ,int size)
14 {
15 MemoryStream inStream = new MemoryStream(data ,offset ,size);
16 MemoryStream outStream = new MemoryStream() ;
17 BZip2.Compress(inStream ,outStream ,size) ;
18
19 byte[] result = outStream.ToArray() ;
20 inStream.Close() ;
21 outStream.Close() ;
22
23 return result ;
24 }
25
26 public static byte[] Unzip(byte[] data ,int offset ,int size)
27 {
28 MemoryStream inStream = new MemoryStream(data ,offset ,size);
29 MemoryStream outStream = new MemoryStream() ;
30 BZip2.Decompress(inStream ,outStream ) ;
31
32 byte[] result = outStream.ToArray() ;
33 inStream.Close() ;
34 outStream.Close() ;
35
36 return result ;
37 }
38 }
- ZipHelper 修正
- ZipHelper
- 压缩与解压缩 ZipHelper
- 压缩与解压缩 ZipHelper
- ZipHelper.cs帮助文件打包
- ZipHelper zip压缩工具包
- ZipHelper压缩解压缩
- 修正
- 修正
- 修正
- 修正CheckBox
- 修正1
- 电源修正
- 弯沉盆修正
- 修正信号量
- 数据修正
- 错误修正
- 修正conflict
- ESFramework介绍之(5)――消息分派器IMessageDispatcher
- ESFramework介绍之(4)――消息拦截器INetMessageHook
- ESFramework介绍之(3)――消息处理器和处理器工厂
- ESFramework介绍之(2)――网络通信消息NetMessage
- ESFramework介绍之(1)――网络通信消息协议接口IContract
- ZipHelper 修正
- iom16v.h
- AgileIM 源码公开
- 压缩与解压缩 ZipHelper
- AgileIM 即时通
- C#播放rm文件
- TCP服务器“拒绝服务攻击” 解决方案
- 线程安全的网络流
- 分布式数据库定时同步问题之--SqlCommand不可序列化