i would like to know if we have any class in qt which can zip a folder or file.i used qprocess to compress,it got compressed but i am unable to uncompress it using normal zip tool.can anyone let me know how can we compress a folder/file using qt api classes
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
A few years ago I had such a issue, and here is my solution:
1). get QuaZip (here is the link text)
2). include quazip sources to your project file
Headers:
HEADERS += src/quazip/crypt.h \ src/quazip/ioapi.h \ src/quazip/quazip.h \ src/quazip/quazipfile.h \ src/quazip/quazipfileinfo.h \ src/quazip/quazipnewinfo.h \ src/quazip/unzip.h \ src/quazip/zip.h \ ... Sources:
SOURCES += src/quazip/ioapi.c \ src/quazip/quazip.cpp \ src/quazip/quazipfile.cpp \ src/quazip/quazipnewinfo.cpp \ src/quazip/unzip.c \ src/quazip/zip.c ... 3). add headers
#include "quazip/quazip.h" #include "quazip/quazipfile.h" 4). use extract function:
static bool extract(const QString & filePath, const QString & extDirPath, const QString & singleFileName = QString("")) { QuaZip zip(filePath); if (!zip.open(QuaZip::mdUnzip)) { qWarning("testRead(): zip.open(): %d", zip.getZipError()); return false; } zip.setFileNameCodec("IBM866"); qWarning("%d entries\n", zip.getEntriesCount()); qWarning("Global comment: %s\n", zip.getComment().toLocal8Bit().constData()); QuaZipFileInfo info; QuaZipFile file(&zip); QFile out; QString name; char c; for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) { if (!zip.getCurrentFileInfo(&info)) { qWarning("testRead(): getCurrentFileInfo(): %d\n", zip.getZipError()); return false; } if (!singleFileName.isEmpty()) if (!info.name.contains(singleFileName)) continue; if (!file.open(QIODevice::ReadOnly)) { qWarning("testRead(): file.open(): %d", file.getZipError()); return false; } name = QString("%1/%2").arg(extDirPath).arg(file.getActualFileName()); if (file.getZipError() != UNZ_OK) { qWarning("testRead(): file.getFileName(): %d", file.getZipError()); return false; } //out.setFileName("out/" + name); out.setFileName(name); // this will fail if "name" contains subdirectories, but we don't mind that out.open(QIODevice::WriteOnly); // Slow like hell (on GNU/Linux at least), but it is not my fault. // Not ZIP/UNZIP package's fault either. // The slowest thing here is out.putChar(c). while (file.getChar(&c)) out.putChar(c); out.close(); if (file.getZipError() != UNZ_OK) { qWarning("testRead(): file.getFileName(): %d", file.getZipError()); return false; } if (!file.atEnd()) { qWarning("testRead(): read all but not EOF"); return false; } file.close(); if (file.getZipError() != UNZ_OK) { qWarning("testRead(): file.close(): %d", file.getZipError()); return false; } } zip.close(); if (zip.getZipError() != UNZ_OK) { qWarning("testRead(): zip.close(): %d", zip.getZipError()); return false; } return true; } and archive function:
static bool archive(const QString & filePath, const QDir & dir, const QString & comment = QString("")) { QuaZip zip(filePath); zip.setFileNameCodec("IBM866"); if (!zip.open(QuaZip::mdCreate)) { myMessageOutput(true, QtDebugMsg, QString("testCreate(): zip.open(): %1").arg(zip.getZipError())); return false; } if (!dir.exists()) { myMessageOutput(true, QtDebugMsg, QString("dir.exists(%1)=FALSE").arg(dir.absolutePath())); return false; } QFile inFile; // Получаем список файлов и папок рекурсивно QStringList sl; recurseAddDir(dir, sl); // Создаем массив состоящий из QFileInfo объектов QFileInfoList files; foreach (QString fn, sl) files 5). enjoy ;)
UPDATE: for CapDroid.
/* Рекурсивно ищет все файлы в директории \a и добавляет в список \b */ static void recurseAddDir(QDir d, QStringList & list) { QStringList qsl = d.entryList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files); foreach (QString file, qsl) { QFileInfo finfo(QString("%1/%2").arg(d.path()).arg(file)); if (finfo.isSymLink()) return; if (finfo.isDir()) { QString dirname = finfo.fileName(); QDir sd(finfo.filePath()); recurseAddDir(sd, list); } else list 回答2:
I don't think you can. As far as I know qcompress and quncompress only provide compressing of streams etc. This means they will not create the headers needed for a real zipfile (which also say which files are in the file).
However there is an open source Qt library called QuaZip which uses zlib (comes with qt) and provides just that.