1

Hi I am developing an application to download an attachment from server and read those files using Blackberry 10 Cascades(QNX Momentics IDE) . I have downloaded the attachment but the attachment is a .Zip file. How can I unzip the folder? Does anyone have samples please share?

pranavjayadev
  • 937
  • 7
  • 31

2 Answers2

2

I used the PKZIP 2.0 compatible archive handler from the OSDaB Project, it does the job quite nicely. They provide Zip and UnZip classes. You also need to include linkage to the installed compression library by adding -lz to the LIBS variable in your .pro file:

LIBS += -lz

Sample code:

        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.
                }
            }
        }
Richard
  • 8,920
  • 2
  • 18
  • 24
  • Download the source files and copy them into your project, or build them into a library. You can then use the sample code from their website. I will paste another example into my answer. – Richard Nov 29 '12 at 17:55
  • i mean how can i add library into my project. – pranavjayadev Nov 30 '12 at 04:26
  • That is described in this article: http://supportforums.blackberry.com/t5/Cascades-Development/Library-projects-in-Cascades/m-p/1891995#M2899 and in the answer to this question: http://stackoverflow.com/questions/13373269/add-library-into-cascades – Richard Nov 30 '12 at 14:43
2

you can use quazip library for unzipping the archive, here quazip porting for Blackberry 10 cascades https://github.com/hakimrie/quazip

here sample function to unzip a file using quazip to extract a file into /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;

}

please make sure to update your pro file to include quazip library (assume your project & quazip project in the same workspace/folder):

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
}
}
hakim
  • 3,819
  • 3
  • 21
  • 25
  • how to add this quazip library into my project – pranavjayadev Nov 14 '12 at 04:53
  • make sure you put quazip project in the same workspace (ie. folder) with your app project, than edit your application.pro file like this: INCLUDEPATH += ../src ../../quazip/src/ LIBS += -lz // since quazip depend on zlib //update your device & simulator build setting like this: 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 } } – hakim Nov 20 '12 at 05:57
  • please check my answer, it has been updated with sample pro file. – hakim Nov 20 '12 at 06:05