Qt 自带的压缩QZipWriter和解压QZipReader详解~含Demo-CSDN博客
示例代码1:
压缩一个文件:
#include "qzipwriter_p.h"
#include "qfileinfo.h"
#include <QDebug>
int main(int argc, char *argv[])
{QApplication a(argc, argv);QString m_savePath=QString::fromLocal8Bit("E:\\桌面\\");QString m_zipFile=QString::fromLocal8Bit("E:\\桌面\\文字.txt");QZipWriter* writer = new QZipWriter(m_savePath + "/QtZip1.zip");QZipWriter::CompressionPolicy policy = writer->compressionPolicy();qDebug() << "policy:" << policy;writer->setCompressionPolicy(QZipWriter::AutoCompress);QFile file(m_zipFile);if (!file.exists()) {return 0;}QFileInfo fileInfo(m_zipFile);file.open(QIODevice::ReadOnly);writer->addFile(fileInfo.fileName(), file.readAll());file.close();if (writer) {delete writer;writer = NULL;}return a.exec();
}
关键代码:
QZipWriter* writer = new QZipWriter(m_savePath + "/QtZip1.zip");
writer->setCompressionPolicy(QZipWriter::AutoCompress);
这句我不懂。。。
writer->addFile(fileInfo.fileName(), file.readAll());
示例代码2:
压缩两个文件:
#include "qzipwriter_p.h"
#include "qfileinfo.h"
#include <QDebug>
void add_file(QString m_zipFile, QZipWriter* writer)
{QFile file(m_zipFile);if (!file.exists()) {return ;}QFileInfo fileInfo(m_zipFile);file.open(QIODevice::ReadOnly);writer->addFile(fileInfo.fileName(), file.readAll());file.close();
}
int main(int argc, char *argv[])
{QApplication a(argc, argv);QString m_savePath=QString::fromLocal8Bit("E:\\桌面\\");QString m_zipFile=QString::fromLocal8Bit("E:\\桌面\\文字.txt");QString m_zipFile1 = QString::fromLocal8Bit("E:\\桌面\\文字2.txt");QZipWriter* writer = new QZipWriter(m_savePath + "/QtZip1.zip");QZipWriter::CompressionPolicy policy = writer->compressionPolicy();qDebug() << "policy:" << policy;writer->setCompressionPolicy(QZipWriter::AutoCompress);add_file(m_zipFile, writer);add_file(m_zipFile1, writer);if (writer) {delete writer;writer = NULL;}return a.exec();
}
示例代码3:
zip中添加目录:
writer->addDirectory("lib");writer->addDirectory("lib2");
结果:
示例代码4:
构建多级目录:
writer->addDirectory("lib/a/b");
结果:
示例代码5:
多级目录+中文路径:
writer->addDirectory(QString::fromLocal8Bit("lib连/a连/b连"));
结果:
示例代码6:
void print_inform(QString path)
{QDir dir(path);dir.setFilter(QDir::Files |QDir::Dirs| QDir::NoDotAndDotDot);QStringList list=dir.entryList();qDebug() << list.count();for (int i = 0; i < list.count(); ++i){qDebug() << list[i];}
}
输出文件夹下的文件和文件夹(不进行递归)
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
只输出文件
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
只输出文件夹
递归打印文件夹:
void print_inform1(QString path)
{QDir dir(path);dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);QStringList list = dir.entryList();for (int i = 0; i < list.count(); ++i){QString path1 = path + "\\" + list[i];qDebug() << path1;}
}
void print_inform3(QString path)
{QDir dir(path);dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);QStringList list = dir.entryList();//qDebug() << list.count();for (int i = 0; i < list.count(); ++i){QString path1 = path + "\\" + list[i];qDebug() << path1;print_inform3(path1);//前面要加上前缀}print_inform1(path);
}
int main(int argc, char *argv[])
{QApplication a(argc, argv);print_inform3(QString::fromLocal8Bit("E:\\桌面\\A"));return a.exec();
}
递归压缩文件夹:
QZipWriter* writer;
QString first_str;
void add_file(QString m_zipFile)
{QFile file(m_zipFile);if (!file.exists()) {return ;}QFileInfo fileInfo(m_zipFile);file.open(QIODevice::ReadOnly);QString path2 =m_zipFile.right(m_zipFile.length() - first_str.length());//qDebug() << path2;writer->addFile(path2, file.readAll());file.close();
}
//压缩文件夹
//首先,得到所有的文件夹和文件void print_inform1(QString path)
{QDir dir(path);dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);QStringList list = dir.entryList();for (int i = 0; i < list.count(); ++i){QString path1 = path + "\\" + list[i];//qDebug() << path1;add_file(path1);}
}
void print_inform3(QString path)
{QDir dir(path);dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);QStringList list = dir.entryList();//qDebug() << list.count();for (int i = 0; i < list.count(); ++i){QString path1 = path + "\\" + list[i];//qDebug() << path1;print_inform3(path1);//前面要加上前缀QString path2=path1.right(path1.length() - first_str.length());//qDebug() << path2;writer->addDirectory(path2);}print_inform1(path);
}
int main(int argc, char *argv[])
{QApplication a(argc, argv);//1.savePath 2.folderPath //注意:savePath结尾为\\//folderPath结尾不是\\QString m_savePath = QString::fromLocal8Bit("E:\\桌面\\B\\");QString m_folderPath = QString::fromLocal8Bit("D:\\Release\\app\\FeiQ");int m=m_folderPath.lastIndexOf('\\')+1;first_str = m_folderPath+"\\";qDebug() << first_str;QString zip_name = m_folderPath.right(m_folderPath.length() - m);qDebug() <<zip_name;writer = new QZipWriter(m_savePath + "/"+zip_name+".zip");QZipWriter::CompressionPolicy policy = writer->compressionPolicy();//qDebug() << "policy:" << policy;writer->setCompressionPolicy(QZipWriter::AutoCompress);print_inform3(m_folderPath);if (writer) {delete writer;writer = NULL;}return a.exec();
}