Michael Van Canneyt wroteCall Compressor.flush first, only then all data will be in dest.
This is done automatically when it is destroyed:
There is no Compressor.Flush. But freeing it seams to work.
Now I am having trouble using TDecompressor. Should I use CopyFrom from another stream to get the decompressed data? This crashes the app. Also, CopyFrom expects the size, but I have no size ... I just want everything!
program stringcompressor;
{$apptype console}
uses
Classes, SysUtils, zstream;
const
Str_Original_String = 'The string to be compressed';
var
Compressor: TCompressionStream;
Decompressor: TDecompressionStream;
StringStream: TStringStream;
CompressedData: TMemoryStream;
i: Integer;
begin
WriteLn('The string to be compressed is: ', Str_Original_String);
StringStream := TStringStream.Create(Str_Original_String);
CompressedData := TMemoryStream.Create();
Compressor := TCompressionStream.Create(clDefault, CompressedData);
Decompressor := TDecompressionStream.Create(CompressedData);
{ Compress the string }
Compressor.CopyFrom(StringStream, StringStream.Size);
Compressor.Free;
{ Write the compressed string to the console }
Write('The compressed data is: ');
CompressedData.Position := 0;
for i := 0 to CompressedData.Size - 1 do
Write(IntToHex(CompressedData.ReadByte, 2));
WriteLn('');
{ Decompress the string }
StringStream.Size := 0;
StringStream.CopyFrom(Decompressor, Decompressor.Size); // <---- crash
WriteLn('The string after being compressed and decompressed is: ',
StringStream.DataString);
Decompressor.Free;
CompressedData.Free;
StringStream.Free;
end.