Qt压缩和解压 zip

zlib编译详见
https://blog.youkuaiyun.com/zhangxuechao_/article/details/85049711

下载quazip
https://github.com/stachenov/quazip

也可直接下载我配置好的quazip
https://github.com/zwx230741/quazip.git

加入头文件
在quazip中创建include文件夹
1
将zlib头文件拷贝到include文件夹下
2
加入动态库文件
在quazip中创建lib文件夹
1
将zlib编译好的动态库拷贝到lib文件夹下
2
quazip工程配置
修改quazip.pro工程文件。添加头文件路径和动态库路径

INCLUDEPATH += $$PWD/include
LIBS += -L$$PWD/lib -lzlib1

3
编译动态库
3
不加入头文件,报以下错误
4


创建测试工程quazipTest
同理,创建include和lib目录。分别放头文件和动态库文件
5
拷贝头文件
从quazip拷贝所有头文件
2
拷贝库文件
拷贝刚刚生成的动态库文件。quazipd.dll是Debug生成的,quazipd.dll是Release生成的,zlib1.dll是zlib动态库
3
quazipTest工程配置
修改quazipTest.pro工程文件。添加头文件路径和动态库路径

INCLUDEPATH += $$PWD/include

CONFIG(debug, debug|release) {
    LIBS += -L$$PWD/lib -lquazipd
} else {
    LIBS += -L$$PWD/lib -lquazip
}

添加压缩解压代码

#include "mainwindow.h"
#include <QApplication>
#include <JlCompress.h>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    JlCompress::compressDir("D:/testzip/a.zip", "D:/testzipdir1");
    JlCompress::extractDir("D:/testzip/a.zip", "D:/testzipdir2");
    
    return a.exec();
}

我测试好的工程:https://github.com/zwx230741/quazipTest.git

05-31
您需要使用Qt中提供的Zip库来进行zip文件的创建和解压缩。以下是一个简单的示例代码,用于创建一个zip文件: ```cpp #include <QDir> #include <QFile> #include <QFileInfo> #include <QZipWriter> void createZip(const QString& zipFilePath, const QString& sourceDirPath) { // Create a QFile object for the zip file QFile zipFile(zipFilePath); if (!zipFile.open(QIODevice::WriteOnly)) { qDebug() << "Failed to open zip file: " << zipFilePath; return; } // Create a QZipWriter object for writing to the zip file QZipWriter zip(&zipFile); // Iterate through all the files in the source directory QDir sourceDir(sourceDirPath); for (const QFileInfo& fileInfo : sourceDir.entryInfoList(QDir::Files)) { // Get the path of the file relative to the source directory QString filePath = fileInfo.absoluteFilePath(); QString relativeFilePath = sourceDir.relativeFilePath(filePath); // Add the file to the zip archive if (!zip.addFile(filePath, relativeFilePath)) { qDebug() << "Failed to add file to zip archive: " << filePath; } } // Close the zip archive zip.close(); } ``` 调用`createZip`函数并传入要创建的zip文件路径和源目录路径即可创建一个zip文件。 解压缩zip文件也很简单,使用QZipReader类即可。以下是一个简单的示例代码,用于解压缩一个zip文件: ```cpp #include <QDir> #include <QFile> #include <QFileInfo> #include <QZipReader> void extractZip(const QString& zipFilePath, const QString& destinationDirPath) { // Create a QZipReader object for reading the zip file QZipReader zip(zipFilePath); if (!zip.isValid()) { qDebug() << "Invalid zip file: " << zipFilePath; return; } // Iterate through all the files in the zip archive for (const QString& filePath : zip.fileNames()) { // Get the path of the file relative to the zip archive QString relativeFilePath = filePath; // Extract the file to the destination directory QString destinationFilePath = QDir(destinationDirPath).filePath(relativeFilePath); if (!zip.extractFile(filePath, destinationFilePath)) { qDebug() << "Failed to extract file from zip archive: " << filePath; } } // Close the zip archive zip.close(); } ``` 调用`extractZip`函数并传入要解压缩zip文件路径和目标目录路径即可解压缩zip文件。
评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值