I all, I remember you my question about ZLib problems.... Delphi XE and ZLib Problems
David Heffernan put me on the way with his excellent answer (Thanks again @David)...
Summarizing the answer ... "This flow for the compressor looks like this: string -> UTF-8 bytes -> compressed bytes -> base64 string. Obviously you reverse the arrows to decompress."
I don't know I must post it in the same post or i must append a new question like this...
Well, I was working last weekend...
I followed the flow string -> UTF-8 bytes -> compressed bytes -> base64 string
This is the compress function and it works...
function CompressStrToCode64Str(const aText: string; aCompressionLevel: TZCompressionLevel): string;
var
strStreamIN,
strStreamOUT: TStringStream;
begin
result := '';
/// Putting the string received to a Stream.
strStreamIN := TStringStream.Create(aText, TEncoding.UTF8);
try
/// Creating the output stream for compression.
strStreamOUT := TStringStream.Create('', TEncoding.UTF8);
try
/// Compressing streamIN to streamOUT
ZCompressStream(strStreamIN, strStreamOUT, aCompressionLevel);
/// Encoding to base64 for string handling.
result := string(EncodeBase64(strStreamOUT, strStreamOUT.Size));
finally
strStreamOUT.Free;
end;
finally
strStreamIN.Free;
end;
end;
and this is the Uncompress functions... but it doesn't works... (returns empty string)
function TForm1.Code64StrToUncompressStr(Const aText: string): string;
var
strStreamIN,
strStreamOUT: TStringStream;
data: TBytes;
begin
result := '';
/// Creating the input stream.
strStreamIN := TStringStream.Create('', TEncoding.UTF8);
try
/// Decoding base64 of received string to a TBytes
data := DecodeBase64(ansistring(aText));
/// Putting the TBytes to a Stream
strStreamIN.Write(data[0], Length(data));
/// Creating uncompressed stream
strStreamOUT := TStringStream.Create('', TEncoding.UTF8);
try
/// Decompressing streamIN to StreamOUT
ZDeCompressStream(strStreamIN, strStreamOUT);
result := strStreamOUT.DataString;
finally
strStreamOUT.Free;
end;
finally
strStreamIN.Free;
end;
end;
Some idea why doesn't work the uncompress function. It returns an empty string. TIA for your patience.
This is what I had in mind:
Output
As you can see, by the time you include base64, it is not really compression. It might be compression with a larger input string. That's why I think you are better to transmit binary, as I explained in your previous question.