Skip to content

Instantly share code, notes, and snippets.

@Al-Muhandis
Last active Sep 9, 2021
Embed
What would you like to do?
Compressing and decompressing. String input and output
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
uses
ZStream
;
const BUF_SIZE = 10000;
function CompressString(const S: String): String;
var
CompStream: TCompressionStream;
DestStream: TMemoryStream;
aStringStream: TStringStream;
begin
DestStream := TMemoryStream.Create;
aStringStream:=TStringStream.Create(EmptyStr);
try
CompStream := TCompressionStream.Create(clDefault, DestStream, True);
CompStream.Write(S[1], Length(S));
CompStream.Free;
aStringStream.LoadFromStream(DestStream);
Result:=aStringStream.DataString;
finally
aStringStream.Free;
DestStream.Free;
end;
end;
function DeCompressString(const S: String): String;
var
DeCompStream: Tdecompressionstream;
DestStream: TMemoryStream;
aStringStream: TStringStream;
begin
DestStream := TMemoryStream.Create;
aStringStream:=TStringStream.Create(S);
try
DeCompStream := Tdecompressionstream.Create(aStringStream, True);
SetLength(Result, BUF_SIZE);
SetLength(Result, DeCompStream.read(Result[1], BUF_SIZE));
finally
aStringStream.Free;
DeCompStream.Free;
DestStream.Free;
end;
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text:=DeCompressString(CompressString(Edit1.Text));
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment