首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解压缩从服务器下载的文件

解压缩从服务器下载的文件
EN

Stack Overflow用户
提问于 2012-11-12 17:33:41
回答 2查看 1.3K关注 0票数 1

嗨,我正在开发一个应用程序从服务器下载附件,并使用黑莓10级联(QNX Momentics )读取这些文件。我已经下载了附件,但附件是一个.Zip文件。如何解压文件夹?有没有人有样品请分享?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-13 14:41:02

你可以使用quazip库来解压这个压缩文件,这里的quazip portforBlackbery10cascades https://github.com/hakimrie/quazip

下面是使用quazip解压文件到/data/文件夹的示例函数

代码语言:javascript
复制
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项目在同一工作区/文件夹中):

代码语言:javascript
复制
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
}
}
票数 2
EN

Stack Overflow用户

发布于 2012-11-12 21:28:42

我使用了来自OSDaB Project的PKZIP2.0兼容归档处理程序,它做得很好。它们提供Zip和UnZip类。您还需要通过将-lz添加到.pro文件中的LIBS变量来包含到已安装压缩库的链接:

代码语言:javascript
复制
LIBS += -lz

示例代码:

代码语言:javascript
复制
        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.
                }
            }
        }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13341234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档