Results 1 to 5 of 5

Thread: Qt file decompression

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

  2. 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.