Last active
September 9, 2021 18:24
-
-
Save Al-Muhandis/665d8b12180dfc24cb07d3eccf91a169 to your computer and use it in GitHub Desktop.
Compressing and decompressing. String input and output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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