[fpc-pascal] How to use TCompressionStream?

Michael Van Canneyt michael at freepascal.org
Sat Jun 21 19:27:19 CEST 2008



On Sat, 21 Jun 2008, Felipe Monteiro de Carvalho wrote:

> Hello,
> 
> I am trying to use TCompressionStream to compress a string. But the
> destination stream is empty. Here is by code, thanks:
> 
> program stringcompressor;
> 
> {$apptype console}
> 
> uses
>   Classes, SysUtils, zstream;
> 
> var
>   Compressor: TCompressionStream;
>   Source: TStringStream;
>   Dest: TMemoryStream;
>   i: Integer;
> begin
>   Source := TStringStream.Create('The string to be compressed');
>   Dest := TMemoryStream.Create();
>   Compressor := TCompressionStream.Create(clDefault, Dest);
>   try
>     Compressor.CopyFrom(Source, Source.Size);

Call Compressor.flush first, only then all data will be in dest. 
This is done automatically when it is destroyed:

So it should also work if you destroy compressor prior to examining the result.


Michael.

> 
>     Write('The result is: ');
>     Dest.Position := 0;
>     for i := 0 to Dest.Size - 1 do
>       IntToHex(Dest.ReadByte, 2);
>     WriteLn('');
>   finally
>     Compressor.Free;
>     Dest.Free;
>     Source.Free;
>   end;
> end.
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
> 



More information about the fpc-pascal mailing list