c++qtquazip

How can I zip a directory/folder with quazip?


I have a directory with files and folders that I would like to zip. I'm using the qt-project quazip for it. So I thought I write a function that packs all content of a directory including the filestructure.

How can I create the folder in the zip-file? I tried it with QuaZipNewInfo but couldn't make it work.

For example I want to zip the tmp-folder with this content:

tmp/1.txt
tmp/folder1/2.txt
tmp/folder1/3.txt
tmp/folder2/4.txt
tmp/folder2/folder3/5.txt

What I get after extracting the file with a common archive-tool (Archive Utility) is this:

tmp/1.txt
tmp/2.txt
tmp/3.txt
tmp/4.txt
tmp/5.txt

This is what I have so far:

void Exporter::zipFilelist(QFileInfoList& files, QuaZipFile& outFile, QFile& inFile, QFile& inFileTmp)
{
    char c;
    foreach(QFileInfo file, files) {
        if(file.isDir()  && file.fileName() != "." && file.fileName() != "..") {
            QFileInfoList infoList = QDir(file.filePath()).entryInfoList();
            zipFilelist(infoList, outFile, inFile, inFileTmp);
        }
        if(file.isFile()) {
            inFileTmp.setFileName(file.fileName());
            inFile.setFileName(file.filePath());

            if(!inFile.open(QIODevice::ReadOnly)) {
                qDebug() << "testCreate(): inFile.open(): " << inFile.errorString().toLocal8Bit().constData();
            }
            QuaZipNewInfo info(inFileTmp.fileName(), inFile.fileName());
            if(!outFile.open(QIODevice::WriteOnly, info)) {
                qDebug() << "testCreate(): outFile.open(): " << outFile.getZipError();
            }
            while(inFile.getChar(&c)&&outFile.putChar(c)) ;
            if(outFile.getZipError()!=UNZ_OK) {
                qDebug() << "testCreate(): outFile.putChar(): %d"<< outFile.getZipError();
            }

            outFile.close();
            if(outFile.getZipError()!=UNZ_OK) {
                qDebug() << "testCreate(): outFile.close(): %d"<< outFile.getZipError();
            }
            inFile.close();
        }
    }
}

And this is how I call the function:

QFileInfoList files = QDir(sourceFolder).entryInfoList();
QFile inFile;
QFile inFileTmp;
QuaZipFile outFile(&zip);
zipFilelist(files, outFile, inFile, inFileTmp);

Solution

  • I don't get any error. When I want to unzip the file it doesn't extract the folders (because I probably don't pack them into the zip!?). So I get all files of all subfolders unziped into one folder.

    It seems that in your function you were recursively getting the files in the folders, but not the folders themselves. Try creating a folder to zip the files into when you recurse into looking for the files in the subdirectory.

    You may want to look into this answer: https://stackoverflow.com/a/2598649/1819900

    How about the utilities provided by QuaZip? http://quazip.sourceforge.net/classJlCompress.html