uses
zlib;
//将Src使用Zlib压缩后存入Dst当中
procedure PackStream(const Src:TStream; Dst:TStream);
var
CompStream: TCompressionStream;
begin
//增加“断言”以防止输入参数有误
Assert(Src <> Nil);
Assert(Dst <> Nil);
CompStream := TCompressionStream.Create(clDefault,Dst);
try
//将源数据的偏移转到首部
Src.Seek(0,soFromBeginning);
//使用CopyFrom将源数据输入到压缩流,以实现压缩
CompStream.CopyFrom(Src,0);
finally
CompStream.Free;
end;
end;
//将以zlib压缩的Src解压缩后存入Dst当中
procedure UnpackStream(const Src:TStream; Dst:TStream);
var
DecompStream: TDecompressionStream;
NewSize: Int64;
begin
//增加“断言”以防止输入参数有误
Assert(Src <> Nil);
Assert(Dst <> Nil);
DecompStream:= TDecompressionStream.Create(Src);
try
//将源数据的偏移转到首部
NewSize := Src.Seek(0, soFromEnd);
Src.Seek(0, soFromBeginning);
//使用CopyFrom将源数据输入到解压缩流,以实现解压缩
//并得到实际解压缩后的数据大小(NewSize)
//内部会使用AllocMem(System单元定义)对Dst进行内存重新分配
//所以,Dst的内存管理必须兼容AllocMem进行内存分配
NewSize := Dst.CopyFrom(DecompStream,NewSize);
//重新设置Dst的实际大小(已经在解压缩过程当中进行重新分配)
Dst.Size := NewSize;
finally
DecompStream.Free;
end;
end;
//测试代码
procedure Test;
var
SrcStream,PackedStream,UnpackedStream:TMemoryStream;
begin
SrcStream := TMemoryStream.Create;
try
SrcStream.LoadFromFile('c:\test.xml');
PackedStream := TMemoryStream.Create;
try
//压缩
PackStream(SrcStream, PackedStream);
PackedStream.Seek(0, soFromBeginning);
PackedStream.SaveToFile('c:\test_xml.pk');
UnpackedStream := TMemoryStream.Create;
try
//解压缩
UnpackStream(PackedStream, UnpackedStream);
UnpackedStream.Seek(0, soFromBeginning);
UnpackedStream.SaveToFile('c:\test_xml.xml');
finally
UnpackedStream.Free;
end;
finally
PackedStream.Free;
end;
finally
SrcStream.Free;
end;
end;
zlib;
//将Src使用Zlib压缩后存入Dst当中
procedure PackStream(const Src:TStream; Dst:TStream);
var
CompStream: TCompressionStream;
begin
//增加“断言”以防止输入参数有误
Assert(Src <> Nil);
Assert(Dst <> Nil);
CompStream := TCompressionStream.Create(clDefault,Dst);
try
//将源数据的偏移转到首部
Src.Seek(0,soFromBeginning);
//使用CopyFrom将源数据输入到压缩流,以实现压缩
CompStream.CopyFrom(Src,0);
finally
CompStream.Free;
end;
end;
//将以zlib压缩的Src解压缩后存入Dst当中
procedure UnpackStream(const Src:TStream; Dst:TStream);
var
DecompStream: TDecompressionStream;
NewSize: Int64;
begin
//增加“断言”以防止输入参数有误
Assert(Src <> Nil);
Assert(Dst <> Nil);
DecompStream:= TDecompressionStream.Create(Src);
try
//将源数据的偏移转到首部
NewSize := Src.Seek(0, soFromEnd);
Src.Seek(0, soFromBeginning);
//使用CopyFrom将源数据输入到解压缩流,以实现解压缩
//并得到实际解压缩后的数据大小(NewSize)
//内部会使用AllocMem(System单元定义)对Dst进行内存重新分配
//所以,Dst的内存管理必须兼容AllocMem进行内存分配
NewSize := Dst.CopyFrom(DecompStream,NewSize);
//重新设置Dst的实际大小(已经在解压缩过程当中进行重新分配)
Dst.Size := NewSize;
finally
DecompStream.Free;
end;
end;
//测试代码
procedure Test;
var
SrcStream,PackedStream,UnpackedStream:TMemoryStream;
begin
SrcStream := TMemoryStream.Create;
try
SrcStream.LoadFromFile('c:\test.xml');
PackedStream := TMemoryStream.Create;
try
//压缩
PackStream(SrcStream, PackedStream);
PackedStream.Seek(0, soFromBeginning);
PackedStream.SaveToFile('c:\test_xml.pk');
UnpackedStream := TMemoryStream.Create;
try
//解压缩
UnpackStream(PackedStream, UnpackedStream);
UnpackedStream.Seek(0, soFromBeginning);
UnpackedStream.SaveToFile('c:\test_xml.xml');
finally
UnpackedStream.Free;
end;
finally
PackedStream.Free;
end;
finally
SrcStream.Free;
end;
end;