PDA

View Full Version : unzip zip file contains files and folders with quazip



nhocjerry
29th October 2013, 03:24
I want to extract a zip file contains files and folders using quazip in Qt but I have not find a good solution yet. I have searched and just found the way to extract zip file with files only, not contains folders. Does anyone can help me? Thanks a lot!

ChrisW67
7th November 2013, 09:19
Ignore the spam above.

QuaZip gives you the full path along with the file name as you step through the archive. It is just a matter of recognising that and creating directories as you go.
This test file:


$ unzip -l test.zip
Archive: test.zip
Length Date Time Name
--------- ---------- ----- ----
0 11-07-2013 17:06 a/
0 11-07-2013 17:05 a/test1.txt
0 11-07-2013 17:05 a/test2.txt
0 11-07-2013 17:06 a/b/
0 11-07-2013 17:06 a/b/test3.txt
--------- -------
0 5 files

this code:


#include <QtCore>
#include <quazip/quazip.h>

int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);

QuaZip zip("test.zip");
if (zip.open(QuaZip::mdUnzip)) {
qDebug() << "Opened";

for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {
// do something
qDebug() << zip.getCurrentFileName();
}
if (zip.getZipError() == UNZ_OK) {
// ok, there was no error
}
}
return 0;
}

this result:


Opened
"a/"
"a/test1.txt"
"a/test2.txt"
"a/b/"
"a/b/test3.txt"

So, if the file name ends with '/' create a directory, otherwise create a file and extract the data into it.