通过的QZipWriter的addFile来实现的。 第一步:pro文件中添加 QT += gui-private 第二步:头文件中添加:
#include <QtGui/private/qzipreader_p.h>
#include <QtGui/private/qzipwriter_p.h>
第三步:cpp文件中实现:
bool zipDir(const QString& dirPath,QString SavePath)
{
bool ret;
QZipWriter *writer = new QZipWriter(SavePath);
if(QZipWriterEx(writer,dirPath,dirPath))
ret=true;
else
ret=false;
writer->close();
delete writer;
return ret;
}
bool QZipWriterEx(QZipWriter *writer, QString tmpPath, QString basePath)
{
QDir dir(tmpPath);
foreach (QFileInfo info, dir.entryInfoList())
{
if (info.fileName() == "." || info.fileName() == "..")
continue;
if (info.isFile())
{
QFile upfile(info.filePath());
upfile.open(QIODevice::ReadOnly);
QString fileName = info.filePath().mid(basePath.size()+1,info.filePath().size());
writer->addFile(fileName,upfile.readAll());
qDebug()<<fileName<<tmpPath<<basePath;
upfile.close();
}
else if (info.isDir())
{
QZipWriterEx(writer,info.filePath(),basePath);
}
}
return true;
源码路径
|