流的解压及压缩(代码精简高效)
2006-02-04 13:47:38 来源:WEB开发网用一个函数即实现流的压缩和解压,使用方法:
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下的压缩和解压功能
更多精彩
赞助商链接