2012-11-12 68 views
1

嗨,我正在開發一個應用程序,從服務器下載附件,並使用Blackberry 10 Cascades(QNX Momentics IDE)讀取這些文件。我已經下載了附件,但附件是一個.Zip文件。我怎樣才能解壓該文件夾?有沒有人有樣品請分享?解壓縮從服務器上下載的文件

回答

2

您可以使用quazip庫解壓縮檔案這裏quazip移植黑莓10個級聯 https://github.com/hakimrie/quazip

這裏樣本函數使用quazip提取一個文件到/數據解壓縮文件/文件夾

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 
} 
} 
+0

如何將此quazip庫添加到我的項目 – pranavjayadev

+0

確保您將quazip項目與您的應用程序項目放在同一個工作區(即。文件夾)中,而不是編輯您的應用程序。親文件是這樣的: INCLUDEPATH + = ../src ../../quazip/src/ LIBS + = -lz //因爲quazip依賴於zlib的 //更新設備和模擬器構建設置這樣的: 設備{ \t CONFIG(release,debug | release){ \t \t DESTDIR = o.le-V7 \t \t LIBS + = -Bstatic -L ../../ quazip /臂/ o.le-V7 -lquazip -Bdynamic \t} \t CONFIG(調試,調試|釋放){ \t \t DESTDIR = o.le-V7-克 \t \t LIBS + = -Bstatic -L ../../ quazip /臂/ o.le-V7-克-lquazip -Bdynamic \t \t } } – hakim

+0

請檢查我的答案,它已經更新了示例pro文件。 – hakim

2

我使用了OSDaB Project的PKZIP 2.0兼容存檔處理程序,它的工作非常好。他們提供Zip和UnZip類。你還需要在你的.pro文件中添加-lz到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. 
       } 
      } 
     } 
+0

但是,如何才能使用這個 – pranavjayadev

+0

下載源文件並將它們複製到您的項目中,或將它們構建到一個庫中。然後,您可以使用他們網站上的示例代碼。我會將另一個例子粘貼到我的答案中。 – Richard

+0

我的意思是我該如何將圖書館添加到我的項目中。 – pranavjayadev

相關問題