【问题标题】:How can I zip a directory/folder with quazip?如何使用 quazip 压缩目录/文件夹?
【发布时间】:2012-12-19 13:32:31
【问题描述】:

我有一个目录,其中包含我想要压缩的文件和文件夹。我正在使用 qt-project quazip。所以我想我写了一个函数来打包一个目录的所有内容,包括文件结构。

如何在 zip 文件中创建文件夹?我用 QuaZipNewInfo 尝试过,但无法正常工作。

例如,我想用以下内容压缩 tmp 文件夹:

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

使用通用存档工具(存档实用程序)提取文件后得到的是:

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

这是我目前所拥有的:

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

这就是我调用函数的方式:

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

【问题讨论】:

  • 不工作时的错误信息是什么?
  • 我没有收到任何错误。当我想解压缩文件时,它不会提取文件夹(因为我可能不会将它们打包到 zip 中!?)。所以我将所有子文件夹的所有文件解压缩到一个文件夹中。

标签: c++ qt quazip


【解决方案1】:

我没有收到任何错误。当我想解压缩文件时,它不会提取文件夹(因为我可能不会将它们打包到 zip 中!?)。因此,我将所有子文件夹的所有文件解压缩到一个文件夹中。

似乎在您的函数中,您递归地获取了文件夹中的文件,但不是文件夹本身。当您递归查找子目录中的文件时,请尝试创建一个文件夹以将文件压缩到其中。

你可能想看看这个答案: https://stackoverflow.com/a/2598649/1819900

QuaZip 提供的实用程序怎么样? http://quazip.sourceforge.net/classJlCompress.html

【讨论】:

  • 抱歉,恐怕我不明白你的问题。我不是用我自己的实现而是用任何其他的压缩工具解压它,所以我对解压过程没有任何影响。
  • 啊,好吧。我懂了。我重新阅读了这个问题,我正在弄清楚:)
  • 抱歉不清楚,我用一个例子更新了问题
  • 已编辑。顺便说一句,“我担心我不明白你的问题。”你的意思是我不明白你的问题;D
  • “尝试创建一个文件夹以将文件压缩到...”这正是问题所在:) 我该怎么做?我想我提出了这个无法理解的问题。
【解决方案2】:

创建 QuaZipNewInfo 对象时,将文件的路径和文件名指定为您希望将其存储在 zip 中的第一个参数,并将磁盘上文件的路径和文件名指定为第二个参数。示例:

在 zip 中添加 C:/test/myFile.txt 作为 test/myFile.txt:

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

【讨论】:

    【解决方案3】:

    为了在您的 zip 文件中创建一个文件夹,您需要创建一个名称以“/”结尾的空文件。答案不包括文件/文件夹列表,而是侧重于在 zip 文件中创建文件夹。

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多