Results 1 to 5 of 5

Thread: Qt file decompression

  1. #1
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    1
    Qt products
    Qt4

    Default Qt file decompression

    How to decompress a gzip file created by Qt/zlib ?
    I wrote a chunk of code to handle the decompression, it creates the output file but nothing is written to it. Can someone help me get the original file as is before compression ?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt file decompression

    Qt qCompress does not produce, and qUncompress does not uncompress, gzip files.
    If you have used zlib directly to produce a compressed file then you use the opposite zlib functions to decompress it.

    How about you show us a small, compilable program that creates this file and then fails to decompress it, then we might have some idea what you actually done.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt file decompression

    If you know the size of the original data, you can decompress a zlib compressed file using qCompress by prepending the data buffer with the expected size as a 32-bit unsigned big-endian integer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Mar 2012
    Posts
    13
    Thanks
    1
    Qt products
    Qt4

    Default Re: Qt file decompression

    QFile inFile("x.x");
    inFile.Open(QIODevice::ReadOnly);
    QByteArray ba=inFile.readAll();

    QFile file("xx.abc");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);
    out<<qCompress(ba);

    Decompress:

    QFile inFile("C:\\xx.abc");
    inFile.open(QIODevice::ReadOnly);
    QByteArray ba=inFile.readAll();

    QFile file("C:\\y.abc");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);
    out<<qUncompress(ba);


    I am using zlib header for this

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt file decompression

    Out of order...
    Quote Originally Posted by CodeHunt View Post
    I am using zlib header for this
    You are not using any zlib function directly here. qCompress and qUnCompress using the zlib library but do not produce the same output as a zlib stream. Ultimately it is irrelevant because your problem lies elsewhere.

    Qt Code:
    1. QFile inFile("x.x");
    2. inFile.Open(QIODevice::ReadOnly);
    3. QByteArray ba=inFile.readAll();
    4.  
    5. QFile file("xx.abc");
    6. file.open(QIODevice::WriteOnly);
    7. QDataStream out(&file);
    8. out<<qCompress(ba);
    9.  
    10. // Decompress:
    11.  
    12. QFile inFile("C:\\xx.abc");
    13. inFile.open(QIODevice::ReadOnly);
    14. QByteArray ba=inFile.readAll();
    15.  
    16. QFile file("C:\\y.abc");
    17. file.open(QIODevice::WriteOnly);
    18. QDataStream out(&file);
    19. out<<qUncompress(ba);
    To copy to clipboard, switch view to plain text mode 
    You are misusing QDataStream. If you write a file with QDataStream then you need to read it with QDataStream; you do not do this. Secondly, QDataStream is a serialisation mechanism for data structures, not an arbitrary binary writer. You should use QIODevice::write() and QIODevice::read() or readAll() directly for this application.

    Qt Code:
    1. #include <QtCore>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QCoreApplication app(argc, argv);
    6.  
    7. QFile origFile("xx.txt");
    8. if (origFile.open(QIODevice::ReadOnly)) {
    9. QByteArray ba = origFile.readAll();
    10. origFile.close();
    11.  
    12. QFile file("xx.abc");
    13. if (file.open(QIODevice::WriteOnly)) {
    14. qint64 count = file.write(qCompress(ba));
    15. qDebug() << "Wrote" << count << "compressed bytes";
    16. file.close();
    17. }
    18. }
    19.  
    20. //Decompress:
    21.  
    22. QFile inFile("xx.abc");
    23. if (inFile.open(QIODevice::ReadOnly)) {
    24. QByteArray ba = inFile.readAll();
    25.  
    26. QFile file("yy.txt");
    27. if (file.open(QIODevice::WriteOnly)) {
    28. qint64 count = file.write(qUncompress(ba));
    29. qDebug() << "Wrote" << count << "uncompressed bytes";
    30. file.close();
    31. }
    32. }
    33.  
    34. return 0;
    35. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to ChrisW67 for this useful post:

    CodeHunt (27th April 2012)

Similar Threads

  1. Replies: 3
    Last Post: 1st November 2010, 16:33
  2. Replies: 0
    Last Post: 21st July 2010, 10:32
  3. Replies: 4
    Last Post: 9th May 2010, 16:18
  4. Replies: 3
    Last Post: 28th March 2009, 15:37
  5. Replies: 3
    Last Post: 25th May 2007, 07:49

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.