Uses ZLib;
//------------------------------------------------------------------------------
// 司马华鹏
//压缩流
procedure myCompressStream(var CompressedStream: TMemoryStream;const CompressionLevel: TCompressionLevel);
var
SourceStream: TCompressionStream;
DestStream: TMemoryStream;
Count: Integer;
begin
Count := CompressedStream.Size;
//获得流的原始尺寸
DestStream := TMemoryStream.Create;
Try
SourceStream:=TCompressionStream.Create(CompressionLevel, DestStream);
Try
CompressedStream.SaveToStream(SourceStream);//SourceStream中保存着原始的流
finally
SourceStream.Free;
//将原始流进行压缩, DestStream中保存着压缩后的流
end;
CompressedStream.Clear;
CompressedStream.WriteBuffer(Count, SizeOf(Count));//写入原始的尺寸
CompressedStream.CopyFrom(DestStream, 0);//写入经过压缩的流
finally
DestStream.Free;
end;
end;
//------------------------------------------------------------------------------
// 司马华鹏
//还原被压缩流
procedure myUnCompressStream(const CompressedStream:TMemoryStream;
var DeCompressedStream:TMemoryStream);
var
SourceStream: TDecompressionStream;
Buffer: PChar;
Count: Integer;
begin
CompressedStream.ReadBuffer(Count, SizeOf(Count));//从被压缩的流中读出原始的尺寸
GetMem(Buffer, Count);//根据尺寸大小为将要读入的原始流分配内存
try
SourceStream := TDecompressionStream.Create(CompressedStream);
Try
SourceStream.ReadBuffer(Buffer^, Count);
//将被压缩的流解压缩,然后存入 Buffer内存块中
DeCompressedStream.WriteBuffer(Buffer^, Count);//将原始图像流保存至 DestStream流
DeCompressedStream.Position := 0;//复位流指针
finally
SourceStream.Free;
end;
finally
FreeMem(Buffer);
end;
end;