compression problem on wince - zlib & memorystream lazarus / 35-Windows CE ---------------------------------------------------------------------- AlerMeta · 2010-05-21 13:50 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 · 2010-05-21 18:00 > 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 · 2010-05-21 20:50 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 · 2010-05-26 15:35 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 · 2010-05-27 01:51 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 · 2010-05-27 04:39 Quote from: Laksen on May 27, 2010, 03:51:59 amAfter 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 · 2010-05-27 12:18 Quote from: JuhaManninen on May 27, 2010, 06:39:06 amThanks 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 · 2010-07-09 10:46 Sorry for long time. Thanks for answers. This working.