zoukankan      html  css  js  c++  java
  • ZipHelper 修正

        在我前面的“压缩与解压缩 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     }
  • 相关阅读:
    (办公)写代码的任务完成后,编写接口文档
    (办公)eclipse连接github cannot open git-upload-pack(git-receive-pack)
    (办公)Spring boot(系列)的返回json封装类
    (办公)重新选择一个开发工具Eclipse
    (办公)项目结构
    (生活)整理电脑文件夹.
    (后台)jxl.read.biff.BiffException: Unable to recognize OLE stream
    (其他)导入密钥
    (后端)根据查询语句修改的update语句
    考研打卡_Day017
  • 原文地址:https://www.cnblogs.com/zhuweisky/p/346312.html
Copyright © 2011-2022 走看看