【问题标题】:Unzip files downloaded from server解压从服务器下载的文件
【发布时间】:2012-11-12 09:33:41
【问题描述】:

您好,我正在开发一个应用程序来从服务器下载附件并使用 Blackberry 10 Cascades(QNX Momentics IDE) 读取这些文件。我已下载附件,但附件是 .Zip 文件。如何解压缩文件夹?谁有样品请分享?

【问题讨论】:

    标签: blackberry-10


    【解决方案1】:

    您可以使用 quazip 库来解压缩存档,这里是 quazip 移植用于 Blackberry 10 级联 https://github.com/hakimrie/quazip

    这里使用 quazip 解压缩文件的示例函数将文件提取到 /data/ 文件夹中

    bool ZipUtils::extractArchive(QString m_filename) {
    // check if file exists
    QFile file(m_filename);
    if (!file.exists()){
        qDebug() << "file is not exists gan";
        return false;
    }
    
    bool result = true;
    QuaZip *m_zip = new QuaZip(m_filename);
    
    QString dataFolder = QDir::homePath();
    QString bookname = m_filename.split("/").last().split(".").first();
    
    QString dest = dataFolder + "/" + bookname;
    QDir dir(dest);
    if (!dir.exists()) {
        // create destination folder
        dir.mkpath(".");
    }
    
    qDebug() << "destination folder: " + dest;
    
    m_zip->open(QuaZip::mdUnzip);
    
    if (!m_zip) {
        return false;
    }
    QuaZipFile *currentFile = new QuaZipFile(m_zip);
    int entries = m_zip->getEntriesCount();
    int current = 0;
    for (bool more = m_zip->goToFirstFile(); more; more =
            m_zip->goToNextFile()) {
        ++current;
        // if the entry is a path ignore it. Path existence is ensured separately.
        if (m_zip->getCurrentFileName().split("/").last() == "")
            continue;
    
        QString outfilename = dest + "/" + m_zip->getCurrentFileName();
        QFile outputFile(outfilename);
        // make sure the output path exists
        if (!QDir().mkpath(QFileInfo(outfilename).absolutePath())) {
            result = false;
            //emit logItem(tr("Creating output path failed"), LOGERROR);
            qDebug() << "[ZipUtil] creating output path failed for:"
                    << outfilename;
            break;
        }
        if (!outputFile.open(QFile::WriteOnly)) {
            result = false;
            //emit logItem(tr("Creating output file failed"), LOGERROR);
            qDebug() << "[ZipUtil] creating output file failed:" << outfilename;
            break;
        }
        currentFile->open(QIODevice::ReadOnly);
        outputFile.write(currentFile->readAll());
        if (currentFile->getZipError() != UNZ_OK) {
            result = false;
            //emit logItem(tr("Error during Zip operation"), LOGERROR);
            qDebug() << "[ZipUtil] QuaZip error:" << currentFile->getZipError()
                    << "on file" << currentFile->getFileName();
            break;
        }
        currentFile->close();
        outputFile.close();
    
        //emit logProgress(current, entries);
    }
    
    return result;
    

    }

    请确保更新您的 pro 文件以包含 quazip 库(假设您的项目和 quazip 项目在同一个工作区/文件夹中):

    INCLUDEPATH += ../src  ../../quazip/src/
    SOURCES += ../src/*.cpp
    HEADERS += ../src/*.hpp ../src/*.h
    LIBS += -lbbsystem
    LIBS   += -lbbdata
    LIBS += -lz
    
    lupdate_inclusion {
        SOURCES += ../assets/*.qml
    }
    
    device {
    CONFIG(release, debug|release) {
        DESTDIR = o.le-v7
        LIBS += -Bstatic -L../../quazip/arm/o.le-v7 -lquazip -Bdynamic
    }
    CONFIG(debug, debug|release) {
        DESTDIR = o.le-v7-g
        LIBS += -Bstatic -L../../quazip/arm/o.le-v7-g -lquazip -Bdynamic    
    }
    }
    
    simulator {
    CONFIG(release, debug|release) {
        DESTDIR = o
        LIBS += -Bstatic -L../../quazip/x86/o-g/ -lquazip -Bdynamic     
    }
    CONFIG(debug, debug|release) {
        DESTDIR = o-g
        LIBS += -Bstatic -L../../quazip/x86/o-g/ -lquazip -Bdynamic
    }
    }
    

    【讨论】:

    • 如何将这个 quazip 库添加到我的项目中
    • 确保将 quazip 项目与您的应用项目放在同一个工作区(即文件夹)中,而不是像这样编辑您的 application.pro 文件:INCLUDEPATH += ../src ../.. /quazip/src/ LIBS += -lz // 因为 quazip 依赖于 zlib //像这样更新你的设备和模拟器构建设置: device { CONFIG(release, debug|release) { DESTDIR = o.le-v7 LIBS += -Bstatic -L../../quazip/arm/o.le-v7 -lquazip -Bdynamic } CONFIG(debug, debug|release) { DESTDIR = o.le-v7-g LIBS += -Bstatic -L. ./../quazip/arm/o.le-v7-g -lquazip -Bdynamic } }
    • 请检查我的答案,它已更新为示例 pro 文件。
    【解决方案2】:

    我使用了来自OSDaB Project 的与PKZIP 2.0 兼容的存档处理程序,它可以很好地完成这项工作。他们提供 Zip 和 UnZip 类。您还需要通过将 -lz 添加到 .pro 文件中的 LIBS 变量来包含与已安装压缩库的链接:

    LIBS += -lz
    

    示例代码:

            UnZip unzip;
            UnZip::ErrorCode ec = unzip.openArchive(fileName);
            if (ec != UnZip::Ok) {
                emit errorString(fileName + " could not open archive.");
            } else {
                QList<UnZip::ZipEntry> fileNames = unzip.entryList();
    
                ec = unzip.extractAll(dirName);
                if (ec != UnZip::Ok) {
                    emit errorString(
                            newFileName + " could not extract data to "
                                    + dirName);
                } else {
                    UnZip::ZipEntry file;
                    foreach(file, fileNames) {
                        // do something with file if needed.
                    }
                }
            }
    

    【讨论】:

    猜你喜欢
    • 2016-09-06
    • 2017-08-21
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多