without using INDY you can do it directly in code as follows
uses
EncdDecd, zlib;
function CompressAndEncode(const input: string): string;
var
fCompressedStream: TMemoryStream;
fStreamCompressor: TCompressionStream;
fStringStream: TStringStream;
begin
fCompressedStream := TMemoryStream.Create;
try
fStreamCompressor := TCompressionStream.Create(clDefault, fCompressedStream);
try
fStreamCompressor.Write(input[1], length(input));
finally
FreeAndNil(fStreamCompressor);
end;
fStringStream := TStringStream.create('');
try
fCompressedStream.Position := 0;
EncodeStream(fCompressedStream, fStringStream);
Result := fStringStream.datastring;
finally
FreeAndNil(fStringStream);
end;
finally
FreeAndNil(fCompressedStream);
end;
end;
function DecodeAndUnCompress(const input: string): string;
var
fWorkStream: TMemoryStream;
fEncodedStream: TMemoryStream;
fStreamDeCompressor: TDeCompressionStream;
fBufLen: integer;
fBuffer: array[0..16383] of byte;
begin
fWorkStream := TMemoryStream.create;
try
fWorkStream.size := length(input);
move(input[1], fWorkStream.memory^, fWorkStream.size);
fWorkStream.position := 0;
fEncodedStream := TMemoryStream.create;
try
DecodeStream(fWorkStream, fEncodedStream);
fWorkStream.size := 0;
fEncodedStream.position := 0;
fStreamDeCompressor := TDeCompressionStream.create(fEncodedStream);
try
fBufLen := fStreamDeCompressor.Read(fBuffer, SizeOf(fBuffer));
while fBufLen > 0 do
begin
fWorkStream.Write(fBuffer, fBufLen);
fBufLen := fStreamDeCompressor.Read(fBuffer, SizeOf(fBuffer));
end;
finally
FreeAndNil(fStreamDeCompressor);
end;
finally
FreeAndnil(fEncodedStream);
end;
fWorkStream.position := 0;
SetLength(Result, fWorkStream.size);
move(fWorkStream.memory^, Result[1], fWorkStream.size);
finally
FreeAndNil(fWorkStream);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
bmp: TBitmap;
mStream: TMemoryStream;
i: Integer;
s, s64: string;
b: byte;
ts: TStringlist;
begin
if OpenPictureDialog1.execute then
begin
bmp := TBitmap.Create;
bmp.LoadFromFile(OpenPictureDialog1.FileName);
mStream := TMemoryStream.Create;
try
bmp.SaveToStream(mStream);
mStream.Position := 0;
for i := 0 to mStream.size do
begin
mStream.read(b,1);
s64 := s64 + char(b);
end;
s := CompressAndEncode(s64);
ts := TStringlist.create;
ts.add(s);
ts.SaveToFile('test.b64');
ts.free;
finally
bmp.Free;
mStream.Free;
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
bmp: TBitmap;
mStream: TMemoryStream;
s, s64: string;
begin
with TStringlist.create do
begin
loadfromfile('test.b64');
s64 := Text;
free;
end;
s := DecodeAndUnCompress(s64);
mStream := TMemoryStream.Create;
mStream.WriteBuffer(s[1],length(s));
bmp := TBitmap.Create;
mstream.Position := 0;
bmp.LoadFromStream(mstream);
// bmp.SaveToFile('test.bmp');
mstream.free;
Image1.Picture.Bitmap := bmp;
bmp.free;
end;