C# 使用sharpziplib写的一个可以压缩和解压缩文件夹的类

   /// <summary>
     /// 自定义读取流(只读)
     /// </summary>
     internal  class  CusStreamReader : Stream
     {
         long  _endPosition; //结束位置
         Stream innerStream;
         /// <summary>
         /// 参数为当前流的断点
         /// </summary>
         public  event  Action< long > Reading;
 
         /// <summary>
         /// 直接使用原始流。
         /// </summary>
         /// <param name="stream">原始流</param>
         public  CusStreamReader(Stream stream)
         {
             this .innerStream = stream;
             _endPosition = stream.Length;
         }
         /// <summary>
         /// 使用流当前位置,指定长度初始化自定义流
         /// </summary>
         /// <param name="stream">原始流</param>
         /// <param name="count">使用长度</param>
         public  CusStreamReader(Stream stream,  long  count)
         {
             this .innerStream = stream;
             _endPosition = stream.Position + count;
             if  (_endPosition > stream.Length)
                 _endPosition = stream.Length;
         }
         /// <summary>
         /// 指定初始位置、长度初始化自定义流
         /// </summary>
         /// <param name="stream">原始流</param>
         /// <param name="offset">初始位置</param>
         /// <param name="count">使用长度</param>
         public  CusStreamReader(Stream stream,  long  offset,  long  count)
         {
             stream.Position = offset > stream.Length ? stream.Length : offset;
             this .innerStream = stream;
             _endPosition = offset + count;
             if  (_endPosition > stream.Length)
                 _endPosition = stream.Length;
         }
         /// <summary>
         /// 从自定义流读取指定长度到array,但是不超过初始化时设定的长度。
         /// </summary>
         /// <returns>读取的字节数</returns>
         public  override  int  Read( byte [] array,  int  offset,  int  count)
         {
             int  readcount = 0;
             if  (Position + count >  this ._endPosition)
                 readcount = innerStream.Read(array, offset, ( int )( this ._endPosition - Position));
             else
                 readcount = innerStream.Read(array, offset, count);
             if  (Reading !=  null )
                 Reading(Position);
             return  readcount;
         }
         /// <summary>
         /// 从自定义流读取一个字节,但是不超过初始化时设定的长度。
         /// </summary>
         /// <returns>读取的字节,未找到则返回-1</returns>
         public  override  int  ReadByte()
         {
             if  (Position >=  this ._endPosition)
                 return  -1;
             else
                 return  base .ReadByte();
         }
 
         public  override  bool  CanRead
         {
             get  return  innerStream.CanRead; }
         }
 
         public  override  bool  CanSeek
         {
             get  return  false ; }
         }
 
         public  override  bool  CanWrite
         {
             get  return  false ; }
         }
 
         public  override  void  Flush()
         {
             throw  new  NotImplementedException();
         }
 
         /// <summary>
         /// 自定义流剩余长度。
         /// </summary>
         public  override  long  Length
         {
             get  return  _endPosition - innerStream.Position; }
         }
 
         /// <summary>
         /// 自定义流位置,返回原始流的位置
         /// </summary>
         public  override  long  Position
         {
             get
             {
                 return  innerStream.Position;
             }
             set
             {
                 throw  new  NotImplementedException();
             }
         }
 
         public  override  long  Seek( long  offset, SeekOrigin origin)
         {
             throw  new  NotImplementedException();
         }
 
         public  override  void  SetLength( long  value)
         {
             throw  new  NotImplementedException();
         }
 
         public  override  void  Write( byte [] buffer,  int  offset,  int  count)
         {
             throw  new  NotImplementedException();
         }
     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值