If the paper is original articles, reprint please indicate the original source This article blog: blog.csdn.net/qq21497936/… Long term continue to bring more projects and technology to share, consulting please add QQ:21497936, wechat: Yangsir198808 red fatty (red imitate) blog: Development technology set (including Qt practical technology, raspberry PI, 3D, OpenCV, OpenGL, FFMPEG, OSG, SCM, soft and hard combination, etc.) continue to update… (Click on the portal)

Qt Development column: Tripartite library development techniques


preface

Qt uses some compression and decompression functions to introduce the libzip library. This article describes the Zlib library.


Zlib library

Zlib is designed to be a free, general-purpose, legally unrestricted – that is, a lossless data compression library that is not protected by any patent and can be used on almost any computer hardware and operating system. Website: www.zlib.net/ CSDN download address: download.csdn.net/download/qq… QQ group: 1047134658 (click “file” to search “Zlip”, group and blog post update)


Zlib compilation

Step 1: Decompress

Step 2: Cmake configuration (mingw32)

Step 3: Generate the project

Step 4: Command line compilation

Step 5: Install


modular


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); Int unCompressLen = uncompressbytearray.size (); 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; 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)); }Copy the code


The results


Project template V1.0.0

The corresponding project template is zlibdemo_v1.0.0 _ Basic template _ Test Compression and decompression. Rar


If the paper is original articles, reprint please indicate the original source This article blog: blog.csdn.net/qq21497936/…