C
coolsundy
Unregistered / Unconfirmed
GUEST, unregistred user!
DLL部分代码:用了ZLib单元
function CompressStream (aSource, aTarget: TStream): Single;
var
comprStream: TCompressionStream;
begin
comprStream := TCompressionStream.Create(
clFastest, aTarget);
try
comprStream.CopyFrom(aSource, aSource.Size);
Result := comprStream.CompressionRate;
finally
comprStream.Free;
end;
end;
procedure DecompressStream (aSource, aTarget: TStream) ;
var
decompStream: TDecompressionStream;
nRead: Integer;
Buffer: array [0..1023] of Char;
begin
aSource.Position:=0;
decompStream := TDecompressionStream.Create(aSource);
try
repeat
nRead := decompStream.Read(Buffer, 1024);
aTarget.Write (Buffer, nRead);
until nRead = 0;
finally
decompStream.Free;
end;
end;
//压缩方法:
function CompressStr(FStr:String;out CompressSize:Integer)Char;stdcall;
var
StringStrm: TStringStream;
CompressStrm: TMemoryStream;
DataStrm: TMemoryStream;
OldLength:Integer;
begin
StringStrm:=TStringStream.Create(FStr);
OldLength:=Length(FStr);
try
CompressStrm:=TMemoryStream.Create;
DataStrm:= TMemoryStream.Create;
try
CompressStream(StringStrm,CompressStrm);
CompressStrm.Position:=0;
DataStrm.Write(OldLength,SizeOf(OldLength));
DataStrm.Write(CompressStrm,CompressStrm.Size);
DataStrm.Position:=0;
StrCopy(Result,DataStrm.Memory);
CompressSize:=DataStrm.Size;
finally
CompressStrm.Free;
end;
finally
StringStrm.Free;
end;
end;
//解压缩方法:
function DecompressStr(FStrChar;CompressSize:Integer):String;stdcall;
var
StringStrm: TStringStream;
MemoStrm,DataStrm: TStream;
begin
StringStrm:=TStringStream.Create('');
try
DataStrm:=TMemoryStream.Create;
MemoStrm:=TMemoryStream.Create;
try
DataStrm.WriteBuffer(FStr,CompressSize);
DataStrm.Position:=SizeOf(Integer);
MemoStrm.CopyFrom(DataStrm,DataStrm.Size-SizeOf(Integer));
DecompressStream(MemoStrm,StringStrm);
Result:=StringStrm.DataString;
finally
MemoStrm.Free;
DataStrm.Free;
end;
finally
StringStrm.Free;
end;
end;
exports
CompressStr name 'CompressStr',
DecompressStr name 'DecompressStr';
程序调用部分:
function CompressStr(FStr:widestring;out CompressSize:Integer)char;stdcall;external 'SZlibDll.dll' name 'CompressStr';
function DecompressStr(FStrChar;CompressSize:Integer):widestring;stdcall;external 'SZlibDll.dll' name 'DecompressStr';
procedure TForm1.Button1Click(Sender: TObject);
var comPCharChar;
FSize:Integer;
begin
memo3.Lines.Clear;
comPChar:=CompressStr(memo2.Lines.Text,FSize);
memo3.Lines.Text:=String(comPChar);
memo1.Lines.Clear;
memo1.Lines.Text:=DecompressStr(comPChar,FSize);
end;
function CompressStream (aSource, aTarget: TStream): Single;
var
comprStream: TCompressionStream;
begin
comprStream := TCompressionStream.Create(
clFastest, aTarget);
try
comprStream.CopyFrom(aSource, aSource.Size);
Result := comprStream.CompressionRate;
finally
comprStream.Free;
end;
end;
procedure DecompressStream (aSource, aTarget: TStream) ;
var
decompStream: TDecompressionStream;
nRead: Integer;
Buffer: array [0..1023] of Char;
begin
aSource.Position:=0;
decompStream := TDecompressionStream.Create(aSource);
try
repeat
nRead := decompStream.Read(Buffer, 1024);
aTarget.Write (Buffer, nRead);
until nRead = 0;
finally
decompStream.Free;
end;
end;
//压缩方法:
function CompressStr(FStr:String;out CompressSize:Integer)Char;stdcall;
var
StringStrm: TStringStream;
CompressStrm: TMemoryStream;
DataStrm: TMemoryStream;
OldLength:Integer;
begin
StringStrm:=TStringStream.Create(FStr);
OldLength:=Length(FStr);
try
CompressStrm:=TMemoryStream.Create;
DataStrm:= TMemoryStream.Create;
try
CompressStream(StringStrm,CompressStrm);
CompressStrm.Position:=0;
DataStrm.Write(OldLength,SizeOf(OldLength));
DataStrm.Write(CompressStrm,CompressStrm.Size);
DataStrm.Position:=0;
StrCopy(Result,DataStrm.Memory);
CompressSize:=DataStrm.Size;
finally
CompressStrm.Free;
end;
finally
StringStrm.Free;
end;
end;
//解压缩方法:
function DecompressStr(FStrChar;CompressSize:Integer):String;stdcall;
var
StringStrm: TStringStream;
MemoStrm,DataStrm: TStream;
begin
StringStrm:=TStringStream.Create('');
try
DataStrm:=TMemoryStream.Create;
MemoStrm:=TMemoryStream.Create;
try
DataStrm.WriteBuffer(FStr,CompressSize);
DataStrm.Position:=SizeOf(Integer);
MemoStrm.CopyFrom(DataStrm,DataStrm.Size-SizeOf(Integer));
DecompressStream(MemoStrm,StringStrm);
Result:=StringStrm.DataString;
finally
MemoStrm.Free;
DataStrm.Free;
end;
finally
StringStrm.Free;
end;
end;
exports
CompressStr name 'CompressStr',
DecompressStr name 'DecompressStr';
程序调用部分:
function CompressStr(FStr:widestring;out CompressSize:Integer)char;stdcall;external 'SZlibDll.dll' name 'CompressStr';
function DecompressStr(FStrChar;CompressSize:Integer):widestring;stdcall;external 'SZlibDll.dll' name 'DecompressStr';
procedure TForm1.Button1Click(Sender: TObject);
var comPCharChar;
FSize:Integer;
begin
memo3.Lines.Clear;
comPChar:=CompressStr(memo2.Lines.Text,FSize);
memo3.Lines.Text:=String(comPChar);
memo1.Lines.Clear;
memo1.Lines.Text:=DecompressStr(comPChar,FSize);
end;