用一個函數即實現流的壓縮和解壓,使用方法:
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下的壓縮和解壓功能