Results 1 to 8 of 8

Thread: Open zip with QuaZip from Qbuffer variable

  1. #1
    Join Date
    Sep 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Open zip with QuaZip from Qbuffer variable

    Hi! I'm trying to open a zip file that is inside a reply from my server.
    I have that zip inside a QBuffer and I'm trying to open it with QuaZip, but it's not working...
    I want to do something like this:

    QByteArray msg = reply->readAll();
    QBuffer buffer(&msg);
    buffer.open(QIODevice::WriteOnly);
    buffer.write(msg.data(), msg.size());
    buffer.seek(3);
    buffer.close();
    QString json_var = extractAll_fomVar(&buffer);

    I can unzip a file in disk but I don't want to create that file... I want to use it in memory...

    I have this error:
    QuaZip::goToFirstFile(): ZIP is not open in mdUnzip mode
    Invalid JSON...

    How can I do that?

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

    Default Re: Open zip with QuaZip from Qbuffer variable

    Why are you writing into the buffer?

    I would have expected that you just open it for reading and pass the pointer to the extractAll_fomVar() function.

    Right now you are overwriting the data with itself and then handing a closed buffer to your function.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    vitor13almeida (26th September 2016)

  4. #3
    Join Date
    Sep 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Open zip with QuaZip from Qbuffer variable

    To create a QuaZip var I can use the path to my zip file or a QIODevice var...
    So I wrote my data from QBytesArray to a QBuffer var.
    Inside extractAll_fomVar() I open buffer for reading

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

    Default Re: Open zip with QuaZip from Qbuffer variable

    Aside from your unnecessary writing of the same data again, does it work if you save the "msg" into a file and pass an unopened QFile to that?

    Cheers,
    _

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

    vitor13almeida (26th September 2016)

  7. #5
    Join Date
    Sep 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Open zip with QuaZip from Qbuffer variable

    QString filename="list.zip";
    QFile zipfile(filename);
    zipfile.open(QIODevice::WriteOnly);
    QDataStream out(&zipfile);
    out << msg;
    zipfile.close();
    zipfile.deleteLater();


    If I do something like this, I can save my file and then I can open it, read its content and unzip it.
    But I don't want to save this zip in disk to read it again... I just need to receive it and open it to unzip a file...


    Added after 25 minutes:


    I might have found the answer here: http://qtshanoir.gforge.inria.fr/htm...c6c0e2040a2158
    but i'll test it
    Last edited by vitor13almeida; 26th September 2016 at 17:34.

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

    Default Re: Open zip with QuaZip from Qbuffer variable

    Quote Originally Posted by vitor13almeida View Post
    QString filename="list.zip";
    QFile zipfile(filename);
    zipfile.open(QIODevice::WriteOnly);
    QDataStream out(&zipfile);
    out << msg;
    zipfile.close();
    zipfile.deleteLater();
    And then you are passing "zipfile" to "extractAll_fomVar"?

    Cheers,
    _

  9. #7
    Join Date
    Sep 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Default Re: Open zip with QuaZip from Qbuffer variable

    I needed to do this:

    Qt Code:
    1. QString dstFileCopy;
    2. QuaZip zip(ioDevice);
    3. zip.open(QuaZip::mdUnzip);
    4. // first, we need some information about archive itself
    5. QString comment = zip.getComment();
    6. qDebug() << "comment: " << comment;
    7. // and now we are going to access files inside it
    8. QuaZipFile file(&zip);
    9. for(bool more=zip.goToFirstFile(); more; more=zip.goToNextFile()) {
    10. file.open(QIODevice::ReadOnly);
    11. QByteArray ba = file.readAll();
    12. file.close();
    13. dstFileCopy = ba.data();
    14. file.close();
    15. }
    16. zip.close();
    17. return dstFileCopy;
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 26th September 2016 at 21:04. Reason: missing [code] tags

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

    Default Re: Open zip with QuaZip from Qbuffer variable

    Ok, so I take that is the content of extractAll_fomVar().

    And you pass the "zipfile" to that? I.e. after you have written to it and closed it again?

    Cheers,
    _

Similar Threads

  1. Can't open password protected zip file using QuaZip
    By Rajesh.Rathod in forum Qt Programming
    Replies: 9
    Last Post: 14th October 2016, 14:09
  2. Unable to open zipped file which is zipped with Quazip
    By zgulser in forum Qt Programming
    Replies: 0
    Last Post: 12th October 2012, 19:14
  3. Size Of QBuffer Not Correct?
    By justin123 in forum Qt Programming
    Replies: 2
    Last Post: 20th April 2011, 03:10
  4. QBuffer::readLine()
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 23rd April 2010, 21:57
  5. QBuffer bytesWritten() problem
    By QAlex in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2009, 10:05

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.