ZLib 压缩和解压流

 

//流压缩
procedure CompressStream(InStream, OutStream: TStream; ACompressLevel: TCompressionLevel);
var
  CmpStream: TCompressionStream;
begin
  InStream.Position := 0;
  CmpStream := TCompressionStream.Create(ACompressLevel, OutStream);
  try
    CmpStream.CopyFrom(InStream, 0);
    OutStream.Position := 0;
  finally
    CmpStream.Free;
  end;
end;

//流解压
procedure DeCompressStream(InStream, OutStream: TStream);
var
  DeCmpStream: TDeCompressionStream;
  Buf: array[1..512] of Byte;
  nSrcCount: Integer;
begin
  InStream.Position := 0;
  DeCmpStream := TDeCompressionStream.Create(InStream);
  try
    repeat
      nSrcCount := DeCmpStream.Read(Buf, sizeof(Buf));
      if nSrcCount > 0 then
        OutStream.Write(Buf, nSrcCount);
    until(nSrcCount = 0);
    OutStream.Position := 0;
  finally
    DeCmpStream.Free;
  end;
end;
posted @ 2010-05-25 08:40  shuaixf  阅读(574)  评论(0编辑  收藏  举报