引用TUTI:这个问题首先应该是在一个特定通讯程序使用怎么样的报文格式的问题,
其次才可能是一个寻求好的报文压缩算法的问题。
TUTI说的没错,可在以后有时间再慢慢来,先解决压缩的问题吧
DELPHI自带的ZLIB单元里有现成的,可用来压缩流.
如果你要压缩字符串,可用TMemoryStream装载它.
unit CompressProc;
interface
procedure Compress(var CompressedStream: TMemoryStream);
procedure UnCompress(var CompressedStream: TMemoryStream);
implementation
uses
zlib, Classes;
procedure Compress(var CompressedStream: TMemoryStream);
var
SourceStream: TCompressionStream;
DestStream: TMemoryStream;
Count: Integer;
begin
Count := CompressedStream.Size;
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;
end;
procedure UnCompress(var CompressedStream: TMemoryStream);
var
SourceStream: TDecompressionStream;
DestStream: TMemoryStream;
Buffer: PChar;
Count: integer;
begin
CompressedStream.Seek(0,soFrombegin
ning);
CompressedStream.ReadBuffer(Count, SizeOf(Count));
GetMem(Buffer, Count);
DestStream := TMemoryStream.Create;
SourceStream := TDecompressionStream.Create(CompressedStream);
Try
SourceStream.ReadBuffer(Buffer^, Count);
DestStream.WriteBuffer(Buffer^, Count);
DestStream.Position := 0;
//复位流指针
CompressedStream.LoadFromStream(DestStream);
finally
FreeMem(Buffer);
DestStream.Free;
end;
end;
end.