流的解压及压缩(代码精简高效)

类别:Delphi 点击:0 评论:0 推荐:

用一个函数即实现流的压缩和解压,使用方法:

ZIP(源流,目标流,True/False)TRUE为压缩,FALSE为解压

函数如下:

function ZIP(mInputStream: TStream; mOutputStream: TStream;Compress:BOOL): BOOL;
var
  I: Integer;
  p:pchar;
  TCS:TCompressionStream;
  TDS:TDecompressionStream;
begin
 result:=false;
  if not (Assigned(mInputStream) and Assigned(mOutputStream)) then Exit;
 try
  if Compress then TCS:=TCompressionStream.Create(clMax,mOutputStream) else TDS:=TDecompressionStream.Create(mOutputStream);//其中的clMax表示压缩级别,可以更改,值是下列参数之一:clNone, clFastest, clDefault, clMax
  getMem(p,mInputStream.size);
  mInputStream.ReadBuffer(p^,mInputStream.Size );
  if Compress then TCS.WriteBuffer(p^,mInputStream.Size ) else TDS.WriteBuffer(p^,mInputStream.Size );  //关键代码:写入流
 finally
  if Compress then TCS.Free else TDS.Free;
  FreeMem(p);
 end;
 result:=true;
end;

 

一定先uses Zlib这个单元,它提供了DELPHI下的压缩和解压功能

本文地址:http://com.8s8s.com/it/it5087.htm