自己解决了。。。sign....
这是我解决的方案,连借用Zlib单元中的压缩功能。
unit UntDataStream;
interface
uses Types, SysUtils,Zlib,Classes,DBClient, ADODB, Provider;
procedure CompressStream(var CompressedStream:TMemoryStream);
function CompressString(SourceString:String):TMemoryStream;
procedure UnCompressStream(var CompressedStream: TMemoryStream);
function UnCompressString(var CompressedStream:TMemoryStream):String;
procedure getClientDataSet(var clDataSet:TClientDataSet;CompressedStream:TMemoryStream);
function getMemoryStreamData(Qry:TAdoQuery):TMemoryStream;
overload;
function getMemoryStreamData(tSp:TADOStoredProc):TMemoryStream;
overload;
implementation
procedure CompressStream(var CompressedStream:TMemoryStream);
var
SourceStream: TCompressionStream;
DestStream: TMemoryStream;
Count: Integer;
begin
Count := CompressedStream.Size;
DestStream := TMemoryStream.Create;
SourceStream:=TCompressionStream.Create(clDefault, DestStream);
Try
CompressedStream.SaveToStream(SourceStream);
SourceStream.Free;
CompressedStream.Clear;
CompressedStream.WriteBuffer(Count, SizeOf(Count));
CompressedStream.CopyFrom(DestStream, 0);
finally
DestStream.Free;
end;
end;
function CompressString(SourceString:String):TMemoryStream;
var
StringStream:TStringStream;
CompressedStream:TMemoryStream;
begin
StringStream:=TStringStream.Create(SourceString);
CompressedStream:=TMemoryStream.Create;
try
CompressedStream.LoadFromStream(StringStream);
CompressStream(CompressedStream);
Result := CompressedStream;
except
Result := NIL;
end;
StringStream.Free;
//CompressedStream.Free;
end;
procedure UnCompressStream(var CompressedStream: TMemoryStream);
var
SourceStream: TDecompressionStream;
DestStream: TMemoryStream;
Buffer: PChar;
Count: Integer;
begin
CompressedStream.Position :=0;
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);
SourceStream.Free;
DestStream.Free;
end;
end;
function UnCompressString(var CompressedStream:TMemoryStream):String;
var
StringStream:TStringStream;
begin
StringStream:=TStringStream.Create('');
try
UnCompressStream(CompressedStream);
StringStream.CopyFrom(CompressedStream,0);
Result := StringStream.DataString;
except
Result := '';
end;
StringStream.Free;
end;
procedure getClientDataSet(var clDataSet:TClientDataSet;CompressedStream:TMemoryStream);
var
tmpStream:TMemoryStream;
begin
tmpStream := TMemoryStream.Create;
try
CompressedStream.Position := 0;
tmpStream.CopyFrom(CompressedStream,0);
tmpStream.Position := 0;
UnCompressStream(tmpStream);
tmpStream.Position := 0;
clDataSet.LoadFromStream(tmpStream);
finally
tmpStream.Free;
end;
end;
function getMemoryStreamData(Qry:TAdoQuery):TMemoryStream;
overload;
var
DataProvider:TDataSetProvider;
clDataSet:TClientDataSet;
CompressedStream: TMemoryStream;
begin
CompressedStream := TMemoryStream.Create;
DataProvider := TDataSetProvider.Create(NIL);
clDataSet := TClientDataSet.Create(NIL);
try
DataProvider.DataSet := Qry;
if not Qry.Active then
Qry.Open;
clDataSet.Data := DataProvider.Data;
clDataSet.SaveToStream(CompressedStream);
CompressedStream.Position := 0;
CompressStream(CompressedStream);
CompressedStream.Position := 0;
Result := CompressedStream;
finally
DataProvider.Free;
clDataSet.Free;
end;
end;
function getMemoryStreamData(tSp:TADOStoredProc):TMemoryStream;
overload;
var
DataProvider:TDataSetProvider;
clDataSet:TClientDataSet;
CompressedStream: TMemoryStream;
begin
CompressedStream := TMemoryStream.Create;
DataProvider := TDataSetProvider.Create(NIL);
clDataSet := TClientDataSet.Create(NIL);
try
DataProvider.DataSet := tSp;
if not tSp.Active then
tSp.Open;
clDataSet.Data := DataProvider.Data;
clDataSet.SaveToStream(CompressedStream);
CompressedStream.Position := 0;
CompressStream(CompressedStream);
CompressedStream.Position := 0;
Result := CompressedStream;
finally
DataProvider.Free;
clDataSet.Free;
end;
end;
end.
这样子可以不结贴吗?