unit Forms.MainForm;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls;
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
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
System.NetEncoding,
System.ZLib;
procedure TForm1.CompressButtonClick( Sender: TObject );
begin
CompressedEdit.Text := CompressString( InputEdit.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,
0 );
finally
LZipper.Free;
end;
LOutputBytes := LOutput.Bytes;
SetLength(
LOutputBytes,
LOutput.Size );
Result := TNetEncoding.Base64.EncodeBytesToString( LOutputBytes );
finally
LInput.Free;
LOutput.Free;
end;
end;
procedure TForm1.DecompressButtonClick( Sender: TObject );
begin
DecompressedEdit.Text := DecompressString( CompressedEdit.Text );
end;
function TForm1.DecompressString(
const AStr:
string ):
string;
var
LInputBytes: TBytes;
LInput : TBytesStream;
LZipper : TZDecompressionStream;
LOutput : TStringStream;
begin
LInput :=
nil;
LOutput :=
nil;
try
LInputBytes := TNetEncoding.Base64.DecodeStringToBytes( AStr );
LInput := TBytesStream.Create( LInputBytes );
LOutput := TStringStream.Create(
string.Empty,
TEncoding.UTF8,
False );
LZipper := TZDecompressionStream.Create( LInput );
try
LOutput.CopyFrom(
LZipper,
0 );
finally
LZipper.Free;
end;
Result := LOutput.DataString;
finally
LInput.Free;
LOutput.Free;
end;
end;
end.