#1 Re: Low level and performance » LZO and SynLZ compression units » 2014-05-10 06:55:29

Hi, I've seen that you mentioned SynCommons to use streams with SynLZ - and found the StreamSynLZ and StreamUnSynLZ functions, which work for me in my test app smile Only one question: what is this "magic number" usefull for?

I have tried to write / read directly to a filestream giving this magic number but it seems not to work?! It would reduce memory consumption, so it would be nice to use it. As a workaround I use 2 memory streams and store the stream size as a header in the filestream. Is there a way to use a filestream for StreamSynLZ / StreamUnSynLZ directly? (I only need the uncompressed stream internally)

My example app (in short form);

function TestWrite;
var
  SL: TStringList;
  msIN,msOUT: TMemoryStream;
  fs: TFileStream;
  sLen: Int64;
begin
  SL := TStringList.Create;
  msIN := TMemoryStream.Create;
  SL.Add('... many lines of blala here');
  ...
  SL.WriteToStream(msIN);
  msIN.Position := 0;
  SynCommons.StreamSynLZ(msIN,msOUT,4711);
  fs := TFileStream.Create('F:\TEST.SYN',fmCreate);
  sLen := msOUT.Size;
  fs.Write(sLen,SizeOf(sLen));
  fs.CopyFrom(msOUT,sLen);

  SL.Clear;
  msIN.Clear;
  msOUT.Clear;
  SL.Add('insert new text as second chunk...');
  SL.SaveToStream(msIN);
  msIN.Position := 0;
  SynCommons.StreamSynLZ(msIN,msOUT,0815);
  msOUT.Position := 0;
  sLen := msOUT.Size;
  fs.Write(sLen,SizeOf(sLen));
  fs.CopyFrom(msOUT,sLen);
  fs.Free;
  msIN.Free;
  msOUT.Free;
  SL.Free;
end;

function TestRead;
var
  SL: TStringList;
  msIN,msOUT: TMemoryStream;
  sLen: Int64;
  fs: TFileStream;
begin
  SL := TStringList.Create;
  msIN := TMemoryStream.Create;
  fs := TFileStream.Create('F:\TEST.SYN',fmOpenRead);
  fs.Read(sLen,SizeOf(sLen));
  msIN.CopyFrom(fs,sLen);
  msIN.Position := 0;
  msOUT := SynCommons.StreamUnSynLZ(msIN,4711);
  msOUT.Position := 0;
  SL.ReadFromStream(msOUT); // breakpoint to check data

  SL.Clear;
  msIN.Clear;
  msOUT.Free;

  fs.Read(sLen,SizeOf(sLen));
  msIN.CopyFrom(fs,sLen);
  msIN.Position := 0;
  msOUT := SynCommons.StreamUnSynLZ(msIN,0815);
  msOUT.Positon := 0;
  SL.ReadFromStream(msOUT); // Check data again

  SL.Free;
  fs.Free;
  msIN.Free;
  msOUT.Free;
end;

Thx in advance
Moehre

#2 Re: Low level and performance » LZO and SynLZ compression units » 2014-05-09 15:15:03

Hi, I'm trying to compress / decompress streams using the code snippet from this thread but always get an access violation in SynLZDecompress1pas in Delphi XE6 hmm Is there any other way to compress / decompress memory stream data with SynLZ or SynZIP? (I have downloaded the newest versions before testing).

Thx
Moehre

Board footer

Powered by FluxBB