Results 1 to 10 of 10

Thread: Uncompress zlib buffer

  1. #1
    Join Date
    Feb 2016
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Uncompress zlib buffer

    Hello guys!
    I am beginning developer in Qt arrived from Delphi community.
    In Delphi i used Zlib library for compressing and decompressing buffers allocated in memory.
    Now in my Qt project i need to decompress buffer represented by QByteArray, previously compressed by Zlib library.

    I try to use
    #include <QtZlib/zlib.h>

    For example here is my zlib packed buffer

    RawDataLen = 50; // length if initial data buffer
    CompressedDataLen = 24; // length of zlib compressed data buffer
    unsigned char buf[24] = {120, 218, 99, 96, 0, 1, 70, 24, 96, 66, 0, 102, 24, 96, 65, 0, 86, 32, 0, 0, 8, 52, 0, 123}; // Zlib compressed buffer

    unsigned long int compressBufLength = CompressedDataLen;
    unsigned long int uncompressLength = RawDataLen;

    int uncompressValue = uncompress(uncompressBuf,&uncompressLength,buf,com pressBufLength); // here i am trying to decompress buffer

    But resulted uncompressValue value is 0
    What is wrong ?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Uncompress zlib buffer

    Use QuaZIP

  3. #3
    Join Date
    Feb 2016
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Uncompress zlib buffer

    Us realized QuaZip is good when you work with files on disk. Instead of it i must process buffer in memory.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Uncompress zlib buffer

    Quote Originally Posted by abshaev View Post
    But resulted uncompressValue value is 0
    Which is good, no?
    That is the value of Z_OK, which uncompress returns on success.

    Cheers,
    _

    P.S.: you could also use qUncompress() if you prepend the expected target size as four additional bytes (see documentation of qUncompress)

  5. #5
    Join Date
    Feb 2016
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Uncompress zlib buffer

    i have succeded by following code:
    Qt Code:
    1. #include <QtZlib/zlib.h>
    2.  
    3. unsigned char *compressBuf = new unsigned char[CompressedDataLen];
    4. memcpy(compressBuf, buffer, CompressedDataLen);
    5. unsigned char *uncompressBuf = new unsigned char[RawDataLen];
    6. unsigned long int compressBufLength = CompressedDataLen;
    7. unsigned long int uncompressLength = RawDataLen;
    8. int uncompressValue = uncompress(uncompressBuf, &uncompressLength, compressBuf, compressBufLength);
    9. if (uncompressValue != Z_OK)
    10. {
    11. OutErrorMessage("Uncompression error code " + QString::number(uncompressValue), false);
    12. }
    13. RawData = QByteArray::fromRawData(reinterpret_cast<char*>(uncompressBuf), uncompressLength);
    To copy to clipboard, switch view to plain text mode 

    Resulted decompressed buffer lies in uncompressBuf and in RawData

    Thank you !
    Last edited by anda_skoa; 18th February 2016 at 18:39. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Uncompress zlib buffer

    Why do you copy "buffer" instead of using it as input for uncompress directly?

    You probably also want to use a properly sized QByteArray as the uncompressBuffer, unless you really want to do all that memory handling manually.

    Cheers,
    _

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Uncompress zlib buffer

    Quote Originally Posted by abshaev View Post
    Us realized QuaZip is good when you work with files on disk. Instead of it i must process buffer in memory.
    What a problem ? QuaZIP is working with QIODevice so You can use QBuffer.

  8. #8
    Join Date
    Feb 2016
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Uncompress zlib buffer

    Quote Originally Posted by anda_skoa View Post
    Why do you copy "buffer" instead of using it as input for uncompress directly?

    You probably also want to use a properly sized QByteArray as the uncompressBuffer, unless you really want to do all that memory handling manually.

    Cheers,
    _
    Would you please help me to use QByteArray?
    When i write:

    buffer.resize(CompressedDataLen);
    in.readRawData(buffer.data(), buffer.size());
    // unsigned char *compressBuf = new unsigned char[CompressedDataLen];
    // memcpy(compressBuf, buffer, CompressedDataLen);
    unsigned char *uncompressBuf = new unsigned char[RawDataLen];
    unsigned long int compressBufLength = CompressedDataLen;
    unsigned long int uncompressLength = RawDataLen;
    // int uncompressValue = uncompress(uncompressBuf, &uncompressLength, compressBuf, compressBufLength);
    int uncompressValue = uncompress(uncompressBuf, &uncompressLength, buffer.data(), compressBufLength);

    Error - invalid conversion from 'char' to 'const z_Bytef*' is arrived

    also i want that output buffer be represented by QByteArray instead of char buffer

    QByteArray RawData;
    int uncompressValue = uncompress(RawData.data(), &uncompressLength, buffer.data(), compressBufLength);

    How to do it correctly?
    Excuse me again, i am currently like a young child in Qt ))


    Added after 8 minutes:


    Will i have differences in including QuaZip library during moving from windows machine to linux and back ?
    Last edited by abshaev; 19th February 2016 at 10:47.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Uncompress zlib buffer

    Quote Originally Posted by abshaev View Post
    Error - invalid conversion from 'char' to 'const z_Bytef*' is arrived
    Strange, as QByteArray::data() returns char* not char.
    But you can try casting to what you had before, i.e. unsigned char*
    Qt Code:
    1. int uncompressValue = uncompress(uncompressBuf, &uncompressLength, static_cast<unsigned char*>(buffer.data()), compressBufLength);
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by abshaev View Post
    also i want that output buffer be represented by QByteArray instead of char buffer
    Right, that is what I meant

    Quote Originally Posted by abshaev View Post
    QByteArray RawData;
    int uncompressValue = uncompress(RawData.data(), &uncompressLength, buffer.data(), compressBufLength);
    That will obviously not work since RawData is empty.
    You first have to resize it to uncompressedLength, just like you initial did with the manually allocated buffer.

    Cheers,
    _

  10. #10
    Join Date
    Feb 2016
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Uncompress zlib buffer

    I did like that:

    RawData.resize(RawDataLen);
    int uncompressValue = uncompress((Bytef*)RawData.data(), &uncompressLength, (Bytef*)buffer.data(), (uint)CompressedDataLen);

    Resulting value of uncompressValue is 0

    However i observe some hangups after decompressing procedure. Sometimes Qt-creator crashes after it.
    Decompressing data length is about 4 Mb

Similar Threads

  1. linux libpng warning: zlib version error
    By akos.maroy in forum Qt Programming
    Replies: 0
    Last Post: 28th April 2014, 21:31
  2. Replies: 1
    Last Post: 1st October 2013, 11:09
  3. Replace -qt-zlib version from 1.2.3 to 1.2.5
    By sandor in forum Installation and Deployment
    Replies: 1
    Last Post: 28th April 2011, 09:41
  4. LZW (.Z files, uncompress/compress in linux) library?
    By falconium in forum Qt Programming
    Replies: 3
    Last Post: 18th April 2011, 00:07
  5. Compress and Uncompress large files in QTcpSocket
    By vishesh in forum Qt Programming
    Replies: 4
    Last Post: 26th June 2007, 23:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.