Qt使用一些压缩解压功能,介绍过libzip库编译,本篇说明zlib库。
zlib被设计为一个免费的,通用的,法律上不受限制的-即不受任何专利保护的无损数据压缩库,几乎可以在任何计算机硬件和操作系统上使用。
官网:http://www.zlib.net/
CSDN下载地址:https://download.csdn.net/download/qq21497936/13944558
QQ群:1047134658(点击“文件”搜索“zlip”,群内与博文同步更新)
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));
}
对应工程模板:zlibDemo_v1.0.0_基础模板_测试压缩解压.rar
若该文为原创文章,转载请注明原文出处
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/111877005