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

时间:2012-11-12 09:33:41

标签: blackberry-10

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

2 个答案:

答案 0 :(得分: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.
                }
            }
        }

答案 1 :(得分:2)

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

此处示例函数使用quazip解压缩文件以将文件解压缩到/ data / folder

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;

}

请确保更新您的专业文件以包含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
}
}