用zlib 单元 的TCompressionStream,TDecompressionStream流
uses zlib;
function zip(instr:string;var outstr:string):boolean;
var
CS:TCompressionStream;
buffer
char;
MemoStream:TmemoryStream;
strStream:TstringStream;
begin
MemoStream:=TMemoryStream.Create;
strStream:=Tstringstream.Create('');
try
buffer:=pchar(instr);
// TCompressionLevel = (clNone, clFastest, clDefault, clMax);
Cs:=TCompressionStream.Create(clMax,MemoStream);
try
Cs.Write(buffer^,length(instr));
finally
Cs.Free;
end;
MemoStream.Position :=0;
MemoStream.SaveToStream(strStream);
outstr:=strStream.DataString;
result:=true;
finally
strstream.free;
MemoStream.Free;
end;
end;
function unzip(instr:string;var outstr:string):boolean;
var
c:integer;
DS:TDecompressionStream;
buffer
char;
outbuffer:array [1..512] of byte;
MemoStream:TmemoryStream;
strStream:TstringStream;
begin
MemoStream:=TMemoryStream.Create;
strStream:=Tstringstream.Create('');
DS:=TDecompressionStream.Create(MemoStream);
try
try
buffer:=pchar(instr);
MemoStream.Write(buffer^,length(instr));
MemoStream.Position :=0;
repeat
c:=DS.Read(outbuffer,512);
if c>0 then strStream.Write(outbuffer,c)
until c=0;
finally
ds.Free;
end;
outstr:=strStream.DataString;
result:=true;
finally
strstream.free;
MemoStream.Free;
end;
end;