从cmake zlib开始至QT编译quazip完整流程
什么是quazip
QuaZIP是使用Qt/C++对ZLIB进行简单封装的用于压缩及解压缩ZIP的开源库。适用于多种平台,利用它可以很方便的将单个或多个文件打包为zip文件,且打包后的zip文件可以通过其它工具打开。但是使用QT编译此库,需要依赖zlib库,现在从用cmake编译zib库开始,详细讲述编译过程,最终得到我们想要的quazip库。
下载源码
使用cmake构建zlib库
需要将QT安装目录下Tools\mingw730_64\bin\mingw32-make.exe添加至系统环境变量,本文章以64位为例,32位操作与64位同理。
由于没有将mingw32-make.exe添加至系统环境变量,我直接使用另一种方法(懒得重启电脑)打开cmake,效果相同。
- 使用Qt5.14.1(MinGW7.3.0 64-bit) 进入到cmake安装目录下,输入cmake-gui.exe回车后打开cmake-gui,这中打开方式不推荐(若已经添加环境变量mingw32-make.exe,直接打开cmake-gui即可)。
- 点击Configure,选择MinGw Makefiles和指定编译器,点击Next
- C中选择QT/Tools/mingw730_64/bin/gcc.exe,C++ 中选择QT/Tools/mingw730_64/bin/g++.exe,点击Finish开始Configure,界面中输出Configureing done表示完成 (32位请使用QT/Tools/mingw730_32/目录)
- 点击Generate生成makefile文件,界面输出Generating done表示生成结束
使用Qt编译zlib库
- 打开Qt 5.14.1(MinGW 7.3.0 64-bit) 进入刚才的 buid文件夹中,执行mingw32-make.exe,等待编译完成,可以在目录下看到生成的动态库和静态库文件 (因我已经生成过,所以编译输出信息不全)
- 若需要编译32的zlib库,请使用Qt 5.14.1(MinGW 7.3.0 32-bit) 进行上一步的操作。编译完成后整理好源码中的头文件和编译生成的库文件,供编译quazip使用,接下来开始编译quazip
使用Qt编译quazip库
- 使用Qtcreator打开下载好的quazip0.7.3源码pro工程文件,在pro文件中注释qztest模块
- 在第二级pro工程文件中添加外部库,导入刚编译整理好的zlib库文件和头文件 (这里使用的是32位zlib库),注意构想项目选用 32位 编译器,64位同理
- 执行qmake后,构建整个项目,等待编译结束后,可以在文件中看到生成的动态库文件
- 整理好quazip的头文件和库文件,至此已经编译出可以使用的quazip动态库
(PS:因编译quazip时引入的是zlib的动态库,所以真正使用时需要将quazip.dll和libzlib.dll都拷到项目运行目录下,若编译quazip引入的是zlib的静态库,则使用时只需要quazip.dll一个文件即可)
使用方法
引入JlCompress.h头文件
**压缩方法**
/// Compress a single file.
/**
\param fileCompressed The name of the archive.
\param file The file to compress.
\return true if success, false otherwise.
*/
static bool compressFile(QString fileCompressed, QString file);
/// Compress a list of files.
/**
\param fileCompressed The name of the archive.
\param files The file list to compress.
\return true if success, false otherwise.
*/
static bool compressFiles(QString fileCompressed, QStringList files);
/// Compress a whole directory.
/**
Does not compress hidden files. See compressDir(QString, QString, bool, QDir::Filters).
\param fileCompressed The name of the archive.
\param dir The directory to compress.
\param recursive Whether to pack the subdirectories as well, or
just regular files.
\return true if success, false otherwise.
*/
static bool compressDir(QString fileCompressed, QString dir = QString(), bool recursive = true);
/**
* @brief Compress a whole directory.
*
* Unless filters are specified explicitly, packs
* only regular non-hidden files (and subdirs, if @c recursive is true).
* If filters are specified, they are OR-combined with
* <tt>%QDir::AllDirs|%QDir::NoDotAndDotDot</tt> when searching for dirs
* and with <tt>QDir::Files</tt> when searching for files.
*
* @param fileCompressed path to the resulting archive
* @param dir path to the directory being compressed
* @param recursive if true, then the subdirectories are packed as well
* @param filters what to pack, filters are applied both when searching
* for subdirs (if packing recursively) and when looking for files to pack
* @return true on success, false otherwise
*/
static bool compressDir(QString fileCompressed, QString dir,
bool recursive, QDir::Filters filters);
**解压方法**
/// Extract a single file.
/**
\param fileCompressed The name of the archive.
\param fileName The file to extract.
\param fileDest The destination file, assumed to be identical to
\a file if left empty.
\return The list of the full paths of the files extracted, empty on failure.
*/
static QString extractFile(QString fileCompressed, QString fileName, QString fileDest = QString());
/// Extract a list of files.
/**
\param fileCompressed The name of the archive.
\param files The file list to extract.
\param dir The directory to put the files to, the current
directory if left empty.
\return The list of the full paths of the files extracted, empty on failure.
*/
static QStringList extractFiles(QString fileCompressed, QStringList files, QString dir = QString());
/// Extract a whole archive.
/**
\param fileCompressed The name of the archive.
\param dir The directory to extract to, the current directory if
left empty.
\return The list of the full paths of the files extracted, empty on failure.
*/
static QStringList extractDir(QString fileCompressed, QString dir = QString());
具体需要哪种方法调用那种方法即可,一般会自己封装一下再使用!至此,quazip库从编译到使用完整流程STOP!!!
可以直接下载我已经编译好的32/64位quazip库和zlib库使用!戳这里
没有积分的评论区留下邮箱,看见后发你!!!