PDA

View Full Version : [SOLVED] QByteArray append data in loop "wrong" end size



Talei
29th April 2010, 14:15
Hello,
I have problem with appending dato into QByteArray from another QByteArray.
What I want to do is do decompress file, zlib, but in my Decompress() function I can only decompress as much as bufferSize (int) data, so I need to divide my compressed size into smaller chunks, not greater then bufferSize.
I do this as fallows, and I know that probably I should use QDataStram, but for the simplicity sake I use QByteArray.


while( !ba.isEmpty() ){

QByteArray tmp = ba.left( 262144 );

if( ba.size() <= 262144 ){
ba.remove( 0, ba.size() );
}else{
ba.remove( 0, 262144 );
}

baUncompre.append( Decompress( tmp ) );

}
Then I write baUncompre to hdd.
The problem is that baUncompre have wrong size. I know that Decompress() works fine.
I tried baUncompre .resize( (int)uncompressedSize ); but that resize QBA not to the unompressed size but uncompressedSize +some more.

Can someone shed some light where is error in my thinking.

Best regards.

EDIT: I checked and loop is fine, error is that append appends more then decompressed size or lees (depends if I resize baUncompre or not).

EDIT: Sorry for garbage post, error was in Decompress() function.

norobro
29th April 2010, 17:10
Hi Talei,
You've obviously solved your previous problem with qUncompress from this thread (http://www.qtcentre.org/threads/30031). I am interested in this. I read the zlib and gzip docs and tried modifying code out of zpipe.c to inflate a gzip stream but nothing that I tried worked.

Mind explaining what you've done or attaching some code?

Thanks,
Norm

Talei
29th April 2010, 18:15
If I correctly understand you question, You want to decode gzip stream, so code posted in thread that You linked (my last post, last code snippet ) do the trick.

Personally, as stated at that thread http://www.qtcentre.org/threads/30031, I don't really know why qUncompress don't do the job. From inflate.c in qt It should decompress gzip.
Try to use debug on qUncompress and trace zlib stream to see where the error is (I tried doing so, but failed to pinpoint where the error is ).

AFAIK the "problem" is in inflateInit() and inflateInit2(). First one is used to initialize zlib stream, secound one can handle also gzip/zip. In my last snippet code in thread that was posted I used inflateInit2 and it works ( also fix while loop because it's incorrect). Or mess around with windowBits size, I use -8 and, if I remember correctly, one of this value didn't work (-15 or -8) for my gzip stream. If I understand correctly (I can be wrong on this one), windowBits is actually a header size for gzip/zip stream, and that amount of bytes are ignored by inflate, so depending on the original compressed stream that can vary.

Look here for more inormation: http://www.zlib.net/manual.html for
ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); and
ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, int windowBits));

Best luck

EDIT: I saw also, browsing qt src, that they use zlib for one of the *network classes (can't remember name thought) and not qUncomrpess, so probably it can't handle gzip after all (I dunno why).

norobro
29th April 2010, 19:11
I missed that. I guess I wasn't following the thread as closely as I thought.

Your code works great. Turns out the problem with my code was that I was setting windowBits to a positive integer (15). When -8 is passed to inflateinit2, which I picked up from your code, it works.

Thanks,
Norm