uses Zlib;
//压缩
procedure DoCompress(var CompressedStream: TMemoryStream);
var
SourceStream: TCompressionStream;
DestStream: TMemoryStream;
Count: int64;
Begin
CompressedStream.Position := 0;
Count := CompressedStream.Size;
if Count=0 then //错误
Exit;
DestStream := TMemoryStream.Create;
SourceStream:=TCompressionStream.Create(clMax, DestStream);
Try
CompressedStream.SaveToStream(SourceStream);
SourceStream.Free;
CompressedStream.Clear;
CompressedStream.WriteBuffer(Count, SizeOf(Count));
CompressedStream.CopyFrom(DestStream, 0);
finally
DestStream.Free;
end;
CompressedStream.Position := 0;
end;
//解压缩
procedure DoUncompress(var UnCompressedStream: TMemoryStream);
var
Count: int64;
SourceStream: TDecompressionStream;
Buffer: pchar;
begin
UnCompressedStream.Position:=0;
UnCompressedStream.ReadBuffer(Count, sizeof(Count));
if count<0 then
begin
UnCompressedStream.Position:=0;
exit;
end;
SourceStream := TDecompressionStream.Create(UnCompressedStream);
Getmem(Buffer, Count);
SourceStream.Read(Buffer^, Count);
freeandnil(SourceStream);
UnCompressedStream.Clear;
UnCompressedStream.WriteBuffer(Buffer^,Count);
UnCompressedStream.Position:=0;
freemem(Buffer);
end;