unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
ZLib, EncdDecd;
type
TForm1 =
class(TForm)
InputEdit: TEdit;
CompressedEdit: TEdit;
DecompressedEdit: TEdit;
CompressButton: TButton;
DecompressButton: TButton;
procedure CompressButtonClick(Sender: TObject);
procedure DecompressButtonClick(Sender: TObject);
private
function CompressString(
const AStr:
string ):
string;
function DecompressString(
const AStr:
string ):
string;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CompressButtonClick(Sender: TObject);
begin
CompressedEdit.Text := CompressString( InputEdit.Text );
end;
procedure TForm1.DecompressButtonClick(Sender: TObject);
begin
DecompressedEdit.Text := DecompressString( CompressedEdit.Text );
end;
function TForm1.CompressString(
const AStr:
string ):
string;
var
LInput : TStringStream;
LZipper : TZCompressionStream;
LOutput : TBytesStream;
LOutputBytes: TBytes;
begin
LInput :=
nil;
LOutput :=
nil;
try
LInput := TStringStream.Create(
AStr,
TEncoding.UTF8,
False );
LOutput := TBytesStream.Create;
LZipper := TZCompressionStream.Create(
TCompressionLevel.clMax,
LOutput );
try
LZipper.CopyFrom(
LInput,
-1 );
finally
LZipper.Free;
end;
LOutputBytes := LOutput.Bytes;
SetLength(
LOutputBytes,
LOutput.Size );
//Result := TNetEncoding.Base64.EncodeBytesToString( LOutputBytes );
Result := EncodeBase64(LOutputBytes, Length(LOutputBytes));
finally
LInput.Free;
LOutput.Free;
end;
end;
function TForm1.DecompressString(
const AStr:
string ):
string;
var
LInputBytes: TBytes;
LInput : TBytesStream;
LZipper : TZDecompressionStream;
LOutput : TStringStream;
begin
LInput :=
nil;
LOutput :=
nil;
try
LInputBytes := DecodeBase64( AStr );
LInput := TBytesStream.Create( LInputBytes );
LOutput := TStringStream.Create(
'
',
TEncoding.UTF8,
False );
LZipper := TZDecompressionStream.Create( LInput );
try
LOutput.CopyFrom(
LZipper,
-1 );
finally
LZipper.Free;
end;
Result := LOutput.DataString;
finally
LInput.Free;
LOutput.Free;
end;
end;
end.