[fpc-pascal] GZip - Stream decompress only in memory

Michael Van Canneyt michael at freepascal.org
Mon Sep 5 07:50:32 CEST 2016



On Sun, 4 Sep 2016, Marcos Douglas wrote:

> Ok, it works now.
>
> For the records, I've used the lib http://www.gocher.me/GZIP (Silvio's
> tip) and the function Base64Decode from WST framework
> http://svn.code.sf.net/p/lazarus-ccr/svn/wst/trunk/
>
> For some reason the function DecodeBase64 from Synapse doesn't work.
> It receives an AnsiString. Instead, the Base64Decode function receives
> a TByteDynArray.
>
> The TDataStream class doesn't matter...
>
> So, the code below is ugly, but shows how it works now (before the refactoring).
>
> Thank you all for the help.
>
> === begin ===
>
> procedure TMainForm.Button2Click(Sender: TObject);
> var
>  Buf,Buf2: TMemoryStream;
>  B: TByteDynArray;
> begin
>  Buf := TMemoryStream.Create;
>  Buf2 := TMemoryStream.Create;
>  try
>    Buf.LoadFromFile('result.txt');
>    B := Base64Decode(TDataStream.New(Buf).AsString,
> [xoDecodeIgnoreIllegalChar]);
>    Buf.SaveToFile('bytes.txt');
>    Buf.Clear;
>    Buf.WriteBuffer(Pointer(B)^, Length(B));
>    if ZUncompressStream(Buf, Buf2) then
>      Buf2.SaveToFile('content.txt');

As far as I know these things can be done with all the FPC classes as well ?
The implementation in base64 unit can perfectly do everything in memory.
And ZUncompressStream can be done with TDecompressionStream ?

Var
   Dest : TFileStream;
   B : TBase64DecodingStream;
   Z : TDecompressionStream;

begin
   Z:=Nil;
   B:=Nil;
   Dest:=Nil;
   try
     B:=TBase64DecodingStream.Create(TFileStream.Create('src',fmOpenRead));
     B.SourceOwner:=True;
     Z:=TDecompressionStream.create(B);
     Z.SourceOwner:=True;
     Dest:=TFileStream.Create('result.txt',fmCreate);
     Dest.CopyFrom(Z,0);
   Finally
     Z.Free;
     Dest.Free;
   end;
end;

Michael.



More information about the fpc-pascal mailing list