How can I zip a directory/folder with quazip?

为君一笑 提交于 2019-12-07 08:21:44

问题


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);

回答1:


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




回答2:


When creating the QuaZipNewInfo object, specify the path and file name to your file as you want to store it in the zip as the first argument, and the path and file name to your file on disk as the second argument. Example:

Adding C:/test/myFile.txt as test/myFile.txt in zip:

QuaZipNewInfo("test/myFile.txt", "C:/test/myFile.txt")




回答3:


In order to create a folder in your zip file, you need to create an empty file with a name ending with "/". The answer does not include the listing of files/folders but focuses on creating folders in zip file.

QDir sourceRootDir("/path/to/source/folder");

QStringList sourceFilesList; // list of path relative to source root folder
sourceFilesList << "relativePath.txt" << "folder" << "folder/relativePath";

QualZip zip("path/to/zip.zip");

if(!zip.open(QuaZip::mdCreate)){
  return false;
}

QuaZipFile outZipFile(&zip);

// Copy file and folder to zip file

foreach (const QString &sourceFilePath, sourceFilesList) {

    QFileInfo sourceFI(sourceRootDir.absoluteFilePath(sourceFilePath));


    // FOLDER (this is the part that interests you!!!)
    if(sourceFI.isFolder()){
        QString sourceFolderPath = sourceFilePath;
        if(!sourceFolderPath.endsWith("/")){
            sourceFolderPath.append("/");
        }

        if(!outZipFile.open(QIODevice::WriteOnly, QuaZipNewInfo(sourceFolderPath, sourceFI.absoluteFilePath()))){
            return false;
        }
        outZipFile.close();


    // FILE
    } else if(sourceFI.isFile()){

        QFile inFile(sourceFI.absoluteFilePath());
        if(!inFile.open(QIODevice::ReadOnly)){
            zip.close();
            return false;
        }

        // Note: since relative, source=dst
        if(!outZipFile.open(QIODevice::WriteOnly, QuaZipNewInfo(sourceFilePath, sourceFI.absoluteFilePath()))){
            inFile.close();
            zip.close();
            return false;
        }

        // Copy
        qDebug() << "         copy start";
        QByteArray buffer;
        int chunksize = 256; // Whatever chunk size you like
        buffer = inFile.read(chunksize);
            while(!buffer.isEmpty()){
            qDebug() << "         copy " << buffer.count();
            outZipFile.write(buffer);
            buffer = inFile.read(chunksize);
        }

        outZipFile.close();
        inFile.close();
    } else {
        // Probably simlink, ignore
    }
}
zip.close();
return true;


来源:https://stackoverflow.com/questions/13953528/how-can-i-zip-a-directory-folder-with-quazip

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!