unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
ZLib, JclStringConversions;
type
TForm2 =
class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
function Compress(aText:
string; aCompressionLevel: TZCompressionLevel=zcMax): UTF8String;
function Decompress(aText: UTF8String):
string;
implementation
{$R *.dfm}
function Compress(aText:
string; aCompressionLevel: TZCompressionLevel=zcMax): UTF8String;
var
strInput,
strOutput: TStringStream;
Zipper: TZCompressionStream;
s: UTF8String;
begin
Result:= '
';
if aText = '
'
then Exit;
s := WideStringToUTF8(aText);
strInput:= TStringStream.Create(s);
strOutput:= TStringStream.Create;
try
Zipper := TZCompressionStream.Create(strOutput, aCompressionLevel);
try
Zipper.CopyFrom(strInput, strInput.Size);
finally
Zipper.Free;
end;
Result := strOutput.DataString;
finally
strInput.Free;
strOutput.Free;
end;
end;
function Decompress(aText: UTF8String):
string;
var
strInput,
strOutput: TStringStream;
Unzipper: TZDecompressionStream;
s: AnsiString;
begin
Result:= '
';
if aText = '
'
then Exit;
strInput:= TStringStream.Create(aText);
strOutput:= TStringStream.Create;
try
Unzipper:= TZDecompressionStream.Create(strInput);
try
strOutput.CopyFrom(Unzipper, Unzipper.Size);
finally
Unzipper.Free;
end;
Result := UTF8ToWideString(strOutput.DataString);
finally
strInput.Free;
strOutput.Free;
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
Edit2.Text := Compress(Edit1.Text);
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
Edit3.Text := Decompress(Edit2.Text);
end;
end.