zoukankan      html  css  js  c++  java
  • 压缩流

    压缩流

    支持DELPHI7及以上版本。

    unit zip;
    
    interface
    
    {$if CompilerVersion>= 21}
      {$define NEWZLib}
    {$IFEND}
    
    uses
      Classes, Zlib, SysUtils;
    
    type
    {$if CompilerVersion< 18.5}
      TBytes = array of Byte;
    {$IFEND}
    
      TZip = class(TObject)
      public
        /// <summary>
        ///   解压
        /// </summary>
        class procedure UnZipStream(const pvInStream, pvOutStream: TStream);
    
        /// <summary>
        ///   压缩
        /// </summary>
        class procedure ZipStream(const pvInStream, pvOutStream: TStream);
    
    
        class function verifyData(const buf; len:Cardinal): Cardinal;
        class function verifyStream(pvStream:TStream; len:Cardinal): Cardinal;
      end;
    
    implementation
    
    class procedure TZip.UnZipStream(const pvInStream, pvOutStream: TStream);
    var
      l:Integer;
    
    {$IFDEF POSIX}
    var
      lvBytes, lvOutBytes:TBytes;
    {$ELSE}
    var
      lvBytes:TBytes;
      OutBuf: Pointer;
      OutBytes: Integer;
    {$ENDIF}
    begin
      if pvInStream= nil then exit;
      l := pvInStream.Size;
      if l = 0 then Exit;
    {$IFDEF POSIX}
      SetLength(lvBytes, l);
      pvInStream.Position := 0;
      pvInStream.Read(lvBytes[0], pvInStream.Size);
      ZLib.ZDecompress(lvBytes, lvOutBytes);  //POSIX下,只支持该方式
      pvOutStream.Size := Length(lvOutBytes);
      pvOutStream.Position := 0;
      pvOutStream.Write(lvOutBytes[0], Length(lvOutBytes));
    {$ELSE}
      setLength(lvBytes, l);
      pvInStream.Position := 0;
      pvInStream.ReadBuffer(lvBytes[0], l);
      {$if defined(NEWZLib)}
      ZLib.ZDecompress(@lvBytes[0], l, OutBuf, OutBytes);
      {$ELSE}
      Zlib.DecompressBuf(@lvBytes[0], l, 0, OutBuf, OutBytes);
      {$ifend}
      try
        pvOutStream.Size := OutBytes;
        pvOutStream.Position := 0;
        pvOutStream.WriteBuffer(OutBuf^, OutBytes);
      finally
        FreeMem(OutBuf, OutBytes);
      end;
    {$ENDIF}
    end;
    
    class function TZip.verifyData(const buf; len: Cardinal): Cardinal;
    var
      i:Cardinal;
      p:PByte;
    begin
      i := 0;
      Result := 0;
      p := PByte(@buf);
      while i < len do
      begin
        Result := Result + p^;
        Inc(p);
        Inc(i);
      end;
    end;
    
    class function TZip.verifyStream(pvStream:TStream; len:Cardinal):
        Cardinal;
    var
      l, j:Cardinal;
      lvBytes:TBytes;
    begin
      SetLength(lvBytes, 1024);
    
      if len = 0 then
      begin
        j := pvStream.Size - pvStream.Position;
      end else
      begin
        j := len;
      end;
    
      Result := 0;
    
      while j > 0 do
      begin
        if j <1024 then l := j else l := 1024;
    
        pvStream.ReadBuffer(lvBytes[0], l);
    
        Result := Result + verifyData(lvBytes[0], l);
        Dec(j, l);
      end;
    end;
    
    class procedure TZip.ZipStream(const pvInStream, pvOutStream: TStream);
    {$IFDEF POSIX}
    var
      lvBytes, lvOutBytes:TBytes;
    {$ELSE}
    var
      lvInBuf: TBytes;
      OutBuf: Pointer;
      OutBytes: Integer;
    {$ENDIF}
    var
      l: Integer;
    begin
      if pvInStream= nil then exit;
      l := pvInStream.Size;
      if l = 0 then Exit;
    
    {$IFDEF POSIX}
      SetLength(lvBytes, pvInStream.Size);
      pvInStream.Position := 0;
      pvInStream.Read(lvBytes[0], pvInStream.Size);
      ZLib.ZCompress(lvBytes, lvOutBytes);                 // POSIX下只支持该中方式的压缩
      pvOutStream.Size := Length(lvOutBytes);
      pvOutStream.Position := 0;
      pvOutStream.Write(lvOutBytes[0], Length(lvOutBytes));
    {$ELSE}
      SetLength(lvInBuf, l);
      pvInStream.Position := 0;
      pvInStream.ReadBuffer(lvInBuf[0], l);
      {$if defined(NEWZLib)}
      ZLib.ZCompress(@lvInBuf[0], l, OutBuf, OutBytes);
      {$ELSE}
      ZLib.CompressBuf(@lvInBuf[0], l, OutBuf, OutBytes);
      {$ifend}
      try
        pvOutStream.Size := OutBytes;
        pvOutStream.Position := 0;
        pvOutStream.WriteBuffer(OutBuf^, OutBytes);
      finally
        FreeMem(OutBuf, OutBytes);
      end;
    {$ENDIF}
    
    
    end;
    
    end.
    

      

  • 相关阅读:
    [技巧] 使用Word2010直接编辑、发布博客→博客园cnblogs
    POJ 1201 Intervals【差分约束】
    HDU 2896 病毒侵袭【AC自动机】
    opengl中的gluOrtho2D【转】
    【转】x86和x64的含义和区别
    POJ 1704 Georgia and Bob【Nim博弈】
    POJ 1947 Rebuilding Roads【树状DP】
    POJ 3207/ POJ 3678 【2SAT】
    POJ 1067 取石子游戏【威佐夫博奕】
    apache+webdav的安装配置
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/11797094.html
Copyright © 2011-2022 走看看