若该文为原创文章,转载请注明原文出处
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/111877005
长期持续带来更多项目与技术分享,咨询请加QQ:21497936、微信:yangsir198808
红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…(点击传送门)
Qt开发专栏:三方库开发技术
前言
Qt使用一些压缩解压功能,介绍过libzip库编译,本篇说明zlib库。
zlib库
zlib被设计为一个免费的,通用的,法律上不受限制的-即不受任何专利保护的无损数据压缩库,几乎可以在任何计算机硬件和操作系统上使用。
官网:http://www.zlib.net/
CSDN下载地址:https://download.csdn.net/download/qq21497936/13944558
QQ群:1047134658(点击“文件”搜索“zlip”,群内与博文同步更新)
zlib编译
步骤一:解压
步骤二:Cmake配置(mingw32版本)
步骤三:生成工程
步骤四:命令行编译
步骤五:安装install
模块化
Demo
void ZlibManager::testEnv()
{
QString fileName = "1.txt";
QString outFileName = "1.txt_zip";
// 打开文件,读取进入内存
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly))
{
LOG;
return;
}
QByteArray byteArray = file.readAll();
LOG << byteArray.size();
byteArray.append((char)0x00);
LOG << "origin size:" << byteArray.size() << ":" << QString(byteArray);
file.close();
// 计算压缩后的文件大小
int len = compressBound(byteArray.size());
LOG << len << (uLong)byteArray.size();
QByteArray compressByteArray = QByteArray(len, 0x00);
int ret = compress((uchar *)compressByteArray.data(), (uLong *)&len, (uchar *)byteArray.data(), (uLong)byteArray.size());
LOG << ret << len;
if(Z_OK == ret)
{
LOG << "Succeed to compress" << byteArray.size() << "to" << len;
}
// 压缩后存入文件
QFile outFile(outFileName);
if(!outFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
LOG;
return;
}
outFile.write(compressByteArray, len);
outFile.close();
LOG << QString(compressByteArray.mid(0, len).toHex(' '));
// 解压
QByteArray unCompressByteArray = QByteArray(1024, 0x00);
// 入坑一 unCompressLen传入的时候,作为指示缓存区的长度,运行完后,作为传出实际的长度
int unCompressLen = unCompressByteArray.size();
LOG << unCompressByteArray.size();
ret = uncompress((uchar *)unCompressByteArray.data(), (uLong *)&unCompressLen, (uchar *)compressByteArray.data(), (uLong)len);
LOG << ret << unCompressLen << len;
if(Z_OK == ret)
{
LOG << "Succeed to uncompress" << compressByteArray.size() << "to" << unCompressLen;
}
// 解压后的存文文件 2.txt
QString fileName2 = "2.txt";
QFile file2(fileName2);
if(!file2.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
LOG;
return;
}
file2.write(unCompressByteArray, unCompressLen);
file2.close();
LOG << "restore:" << QString(unCompressByteArray.mid(0, unCompressLen));
}
运行结果
工程模板v1.0.0
对应工程模板:zlibDemo_v1.0.0_基础模板_测试压缩解压.rar
若该文为原创文章,转载请注明原文出处
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/111877005