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:
{$APPTYPE CONSOLE}
uses
SysUtils, Classes, ZLib, EncdDecd;
function CompressAndEncodeString(const Str: string): string;
var
Utf8Stream: TStringStream;
Compressed: TMemoryStream;
Base64Stream: TStringStream;
begin
Utf8Stream := TStringStream.Create(Str, TEncoding.UTF8);
try
Compressed := TMemoryStream.Create;
try
ZCompressStream(Utf8Stream, Compressed);
Compressed.Position := 0;
Base64Stream := TStringStream.Create('', TEncoding.ASCII);
try
EncodeStream(Compressed, Base64Stream);
Result := Base64Stream.DataString;
finally
Base64Stream.Free;
end;
finally
Compressed.Free;
end;
finally
Utf8Stream.Free;
end;
end;
function DecodeAndDecompressString(const Str: string): string;
var
Utf8Stream: TStringStream;
Compressed: TMemoryStream;
Base64Stream: TStringStream;
begin
Base64Stream := TStringStream.Create(Str, TEncoding.ASCII);
try
Compressed := TMemoryStream.Create;
try
DecodeStream(Base64Stream, Compressed);
Compressed.Position := 0;
Utf8Stream := TStringStream.Create('', TEncoding.UTF8);
try
ZDecompressStream(Compressed, Utf8Stream);
Result := Utf8Stream.DataString;
finally
Utf8Stream.Free;
end;
finally
Compressed.Free;
end;
finally
Base64Stream.Free;
end;
end;
var
EncodedAndCompressed: AnsiString;
begin
EncodedAndCompressed := CompressAndEncodeString('ZLib, Utf-8, compression test');
Writeln(EncodedAndCompressed);
Writeln(DecodeAndDecompressString(EncodedAndCompressed));
Readln;
end.
Output
eJyL8slM0lEILUnTtdBRSM7PLShKLS7OzM9TKEktLgEAjjwKMA== ZLib, Utf-8, compression test
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.