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的实现修正如下:
 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     }
原创粉丝点击