PDA

View Full Version : Open zip with QuaZip from Qbuffer variable



vitor13almeida
26th September 2016, 12:50
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?

anda_skoa
26th September 2016, 14:21
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,
_

vitor13almeida
26th September 2016, 14:57
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

anda_skoa
26th September 2016, 15:42
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,
_

vitor13almeida
26th September 2016, 16:34
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/html/classQuaZipFile.html#ad31592e0e8a9eaa009c6c0e2040a 2158
but i'll test it

anda_skoa
26th September 2016, 17:05
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,
_

vitor13almeida
26th September 2016, 17:24
I needed to do this:



QString dstFileCopy;
QuaZip zip(ioDevice);
zip.open(QuaZip::mdUnzip);
// first, we need some information about archive itself
QString comment = zip.getComment();
qDebug() << "comment: " << comment;
// and now we are going to access files inside it
QuaZipFile file(&zip);
for(bool more=zip.goToFirstFile(); more; more=zip.goToNextFile()) {
file.open(QIODevice::ReadOnly);
QByteArray ba = file.readAll();
file.close();
dstFileCopy = ba.data();
file.close();
}
zip.close();
return dstFileCopy;

anda_skoa
26th September 2016, 20:03
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,
_