Recent

Author Topic: compression problem on wince - zlib & memorystream  (Read 10023 times)

AlerMeta

  • Newbie
  • Posts: 4
compression problem on wince - zlib & memorystream
« on: May 21, 2010, 03:50:08 pm »
Hi,

Pls help me.

Code: [Select]
uses zstream....

const
  cZIP_BUFFER_SIZE=1024;

var
  mCsomag: TStrings;
  mMSin,mMSout: TMemoryStream;

  mZipstream: TCompressionStream;
  Buf : PByte;
  I,Count,NewCount : Integer;

begin
  mCsomag:= TStringlist.Create;
  mMSin := TMemoryStream.Create;
  mMSout:= TMemoryStream.Create;
  try
    mCsomag.Append('abcdefghijklmnopqsrt');
    mCsomag.SaveToStream(mMSin);
    Buf:=GetMem(cZIP_BUFFER_SIZE);
    Try
      mZipstream := TCompressionStream.Create(clDefault, mMSout, true);
      try
        Repeat
          Count:=mMSin.Read(Buf^,cZIP_BUFFER_SIZE);
          NewCount:=Count;
          While (NewCount>0) do
            NewCount:=NewCount-mZipstream.Write(Buf^,NewCount);
        Until (Count=0);

        mMSout.SaveToFile(Application.Location+'out.txt');  // WRONG: 0 byte size !!!!!

      finally
        mZipstream.Free;
      end;
    Finally
      FreeMem(Buf);
    end;
  finally
    mMSin.Free;
    mMSout.Free;
    mCsomag.Free;
  end;

This code working on wince, but out.txt size: 0 byte ! Why ?
I use lazarus v0.9.28.2.



JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3634
  • I like bugs.
Re: compression problem on wince - zlib & memorystream
« Reply #1 on: May 21, 2010, 08:00:40 pm »
> This code working on wince, but out.txt size: 0 byte ! Why ?

I don't know exactly.
  Count:=mMSin.Read(Buf^,cZIP_BUFFER_SIZE);
returns zero for some reason.

The following code works:

Code: [Select]
var
  mCsomag: TStrings;
  mMSout: TFileStream;
  mZipstream: TCompressionStream;
  S: String;
begin
  mCsomag:= TStringlist.Create;
  mMSout:= TFileStream.Create(Application.Location+'out.z', fmCreate);
  mZipstream := TCompressionStream.Create(clDefault, mMSout, true);
  try
    mCsomag.Append('abcdefghijklmnopqsrt');
    mCsomag.Append('1234567890');
    S:=mCsomag.Text;
    mZipstream.Write(Pointer(S)^, Length(S));
  finally
    mZipstream.Free;
    mMSout.Free;
    mCsomag.Free;
  end;

Note that 'out.txt' was a misleading name for output file because it is compressed.
'out.z' is better.


Regards,
Juha

AlerMeta

  • Newbie
  • Posts: 4
Re: compression problem on wince - zlib & memorystream
« Reply #2 on: May 21, 2010, 10:50:04 pm »
Thx for your reply. But my code only a test. Final goal: Tmemorystream to Tmemorystream

1. create tmemorystream from stringlist, tbitmap (from camera), database data (from query), etc.
2. compressing in memory (zlib, etc.)
3. create tmemorystream (compressed data)

4. insert to blobfield (sqlite db)
5. scheduled send compressed data over tcp/ip to server (lnet components)

------------------------------------------
S:=mCsomag.Text;
mZipstream.Write(Pointer(S)^, Length(S));
------------------------------------------
Not only text content. Photos, and other binary data.

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3541
Re: compression problem on wince - zlib & memorystream
« Reply #3 on: May 26, 2010, 05:35:56 pm »
I think you will have to debug this. You can either use the IDE debugger or put various DebugLn function calls from unit LCLProc to Print the state of various variables in various points in the program. Run it in the PDA and in your desktop and check where the variables are different for both. Takes time but totally doable.

Laksen

  • Hero Member
  • *****
  • Posts: 617
    • J-Software
Re: compression problem on wince - zlib & memorystream
« Reply #4 on: May 27, 2010, 03:51:59 am »
After you call mCsomag.SaveToStream(mMSin); it's pointing at the last byte written. So when you try to read it again it won't read anything

You need to call mMSin.Seek(0, soFromBeginning); after mCsomag.SaveToStream(mMSin);

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3634
  • I like bugs.
Re: compression problem on wince - zlib & memorystream
« Reply #5 on: May 27, 2010, 06:39:06 am »
After you call mCsomag.SaveToStream(mMSin); it's pointing at the last byte written. So when you try to read it again it won't read anything

You need to call mMSin.Seek(0, soFromBeginning); after mCsomag.SaveToStream(mMSin);

Thanks Laksen for finding the error!
Streams can be little tricky to use.

Juha

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3541
Re: compression problem on wince - zlib & memorystream
« Reply #6 on: May 27, 2010, 02:18:56 pm »
Thanks Laksen for finding the error!
Streams can be little tricky to use.

If you speak german, I recommend the Lazarus book. There is a good explanation about streams there.

AlerMeta

  • Newbie
  • Posts: 4
Re: compression problem on wince - zlib & memorystream
« Reply #7 on: July 09, 2010, 12:46:29 pm »
Sorry for long time.

Thanks for answers. This working.