嗨,我正在開發一個應用程序,從服務器下載附件,並使用Blackberry 10 Cascades(QNX Momentics IDE)讀取這些文件。我已經下載了附件,但附件是一個.Zip文件。我怎樣才能解壓該文件夾?有沒有人有樣品請分享?解壓縮從服務器上下載的文件
回答
您可以使用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
}
}
我使用了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.
}
}
}
但是,如何才能使用這個 – pranavjayadev
下載源文件並將它們複製到您的項目中,或將它們構建到一個庫中。然後,您可以使用他們網站上的示例代碼。我會將另一個例子粘貼到我的答案中。 – Richard
我的意思是我該如何將圖書館添加到我的項目中。 – pranavjayadev
- 1. 從服務器解壓縮文件http
- 2. 從golang服務器下載瀏覽器中的壓縮文件
- 3. 在服務器端解壓縮/解壓縮文件 - Worklight
- 4. 壓縮文件並從客戶端或服務器下載?
- 5. 從SFTP服務器下載壓縮文件
- 6. 從服務器壓縮excel文件下載
- 7. 無法正確地從服務器上下載創建的壓縮文件
- 8. 下載該壓縮文件時自動解壓縮文件
- 9. 解壓縮在asp.net服務器上的文件
- 10. 從解壓縮下載文件中禁用Safari瀏覽器
- 11. 安卓:解壓縮下載文件
- 12. iOS - 已下載NSURLSessionTask解壓縮文件
- 13. 下載並解壓縮XML文件
- 14. 在SFTP服務器上解壓縮文件
- 15. 從遠程服務器下載zip文件夾並從silverlight應用程序解壓縮文件夾
- 16. C++解密從服務器上下載的文件
- 17. 從Bash腳本文件下載文件並解壓縮
- 18. 解決:從服務器下載文件時下載站點
- 19. 從手機壓縮文件並將其上傳到服務器?
- 20. 解壓下載的gradle這個文件從自己的Nexus服務器
- 21. Java - 從url下載壓縮文件
- 22. 壓縮和解壓縮文本從html到服務器,反之亦然
- 23. Big JSON文件 - 如何壓縮服務器並在客戶端解壓縮?
- 24. Asynctask內的服務()或下載經理從服務器上下載文件
- 25. WebClient.DownloadFileAsync下載服務器上的文件
- 26. 下載解壓縮上傳clodinary
- 27. 如何從服務器下載後在三星bada中解壓縮一個壓縮目錄
- 28. 如何從URL下載zip文件並將其解壓到windows phone 8上。zip文件包含來自服務器的壓縮sqlite文件
- 29. 從Windows服務器上的批處理文件中壓縮文件夾結構
- 30. Android - 從服務器上的文件夾下載所有文件
如何將此quazip庫添加到我的項目 – pranavjayadev
確保您將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
請檢查我的答案,它已經更新了示例pro文件。 – hakim