1. zlib
zlib是一个开源的数据压缩库,用于在应用程序中进行数据的压缩和解压缩操作。它提供了一组函数和数据结构,可以实现广泛的压缩算法,其中最常用的是Deflate算法。zlib库的设计简单、高效,并且具有广泛的应用领域。
zlib库的主要特点包括:
-
数据压缩和解压缩:zlib库可以将数据压缩为更小的体积,以节省存储空间或网络带宽,并且可以将压缩后的数据解压缩回原始数据。
-
高效性能:zlib库的压缩和解压缩算法经过优化,具有较高的压缩速度和解压缩速度。
-
可移植性:zlib库是使用纯C编写的,可以在各种操作系统和平台上进行编译和运行。
-
简单易用:zlib库提供了简单的API,易于集成到应用程序中,并且具有丰富的文档和示例代码。
在实际应用中,zlib库常用于压缩文件、网络传输、数据库存储、图像处理等场景。它被广泛应用于各种领域,包括Web服务器、数据压缩工具、游戏开发等。
除了基本的压缩和解压缩功能,zlib库还提供了一些高级特性,如流式压缩、字典压缩、动态压缩级别调整等,以满足不同场景下的需求。zlib是一个功能强大、高效可靠的数据压缩库,可以帮助开发者在应用程序中实现数据的压缩和解压缩功能,从而提升存储和传输效率。
1.1 zlib下载
source | 地址 |
---|---|
官网 | https://www.zlib.net/ |
GitHub | https://github.com/madler/zlib/ |
所有版本下载地址 | https://www.zlib.net/fossils/ |
编译好的库 | 【免费】zlib动态链接库,包括dll、lib、include |
2. zlib编译与部署
2.1 zlib编译lib、dll
-
zlib下载
-
解压压缩包:zlib-1.3.1
-
使用CMake-Gui配置路径
文件目录如下
-
点击Configure,选择x64
-
点击Generate生成vs2019工程
-
点击Open Project,打开VS工程
工程目录如下
-
生成Debug x64动态库
Debug模式下生成zlibd.lib、zlibd.dll
-
生成Release x64动态库
Release模式下生成zlib.lib、zlib.dll
2.2 zlib部署与使用
将编译好的库进行整理,以方便应用到自己的工程中
- 创建目录如下
目录 | 说明 | 文件 |
---|---|---|
bin | 存放dll | zlibd.dll、zlib.dll |
lib | 存放lib | zlibd.lib、zlib.lib |
include | 存放.h文件 | zlib.h、zconf.h |
- bin目录:zlibd.dll、zlib.dll
- lib目录:zlibd.lib、zlib.lib
4. include目录:zlib.h、zconf.h
3. C++示例
#include <iostream>
#include <fstream>
#include <vector>
#include <zlib.h>
压缩数据
// 压缩数据
bool CompressData(const std::string& inputFileName, const std::string& outputFileName) {
std::ifstream inputFile(inputFileName, std::ios::binary);
if (!inputFile) {
std::cerr << "Failed to open input file: " << inputFileName << std::endl;
return false;
}
std::ofstream outputFile(outputFileName, std::ios::binary);
if (!outputFile) {
std::cerr << "Failed to open output file: " << outputFileName << std::endl;
return false;
}
// 设置压缩参数
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
// 初始化压缩流
if (deflateInit(&strm, Z_DEFAULT_COMPRESSION) != Z_OK) {
std::cerr << "Failed to initialize deflate stream" << std::endl;
return false;
}
// 读取输入文件并压缩数据
const int bufferSize = 16384;
std::vector<char> buffer(bufferSize);
do {
inputFile.read(buffer.data(), bufferSize);
strm.avail_in = static_cast<uInt>(inputFile.gcount());
strm.next_in = reinterpret_cast<Bytef*>(buffer.data());
do {
std::vector<char> outputBuffer(bufferSize);
strm.avail_out = bufferSize;
strm.next_out = reinterpret_cast<Bytef*>(outputBuffer.data());
// 压缩数据
int result = deflate(&strm, inputFile.eof() ? Z_FINISH : Z_NO_FLUSH);
if (result == Z_STREAM_ERROR) {
std::cerr << "Error during compression: " << strm.msg << std::endl;
deflateEnd(&strm);
return false;
}
// 写入压缩后的数据到输出文件
std::streamsize compressedSize = bufferSize - strm.avail_out;
outputFile.write(outputBuffer.data(), compressedSize);
} while (strm.avail_out == 0);
} while (!inputFile.eof());
// 压缩结束
deflateEnd(&strm);
inputFile.close();
outputFile.close();
return true;
}
解压缩数据
// 解压缩数据
bool DecompressData(const std::string& inputFileName, const std::string& outputFileName) {
std::ifstream inputFile(inputFileName, std::ios::binary);
if (!inputFile) {
std::cerr << "Failed to open input file: " << inputFileName << std::endl;
return false;
}
std::ofstream outputFile(outputFileName, std::ios::binary);
if (!outputFile) {
std::cerr << "Failed to open output file: " << outputFileName << std::endl;
return false;
}
// 设置解压缩参数
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
// 初始化解压缩流
if (inflateInit(&strm) != Z_OK) {
std::cerr << "Failed to initialize inflate stream" << std::endl;
return false;
}
// 读取输入文件并解压缩数据
const int bufferSize = 16384;
std::vector<char> buffer(bufferSize);
do {
inputFile.read(buffer.data(), bufferSize);
strm.avail_in = static_cast<uInt>(inputFile.gcount());
strm.next_in = reinterpret_cast<Bytef*>(buffer.data());
do {
std::vector<char> outputBuffer(bufferSize);
strm.avail_out = bufferSize;
strm.next_out = reinterpret_cast<Bytef*>(outputBuffer.data());
// 解压缩数据
int result = inflate(&strm, inputFile.eof() ? Z_FINISH : Z_NO_FLUSH);
if (result == Z_STREAM_ERROR) {
std::cerr << "Error during decompression: " << strm.msg << std::endl;
inflateEnd(&strm);
return false;
}
// 写入解压缩后的数据到输出文件
std::streamsize uncompressedSize = bufferSize - strm.avail_out;
outputFile.write(outputBuffer.data(), uncompressedSize);
} while (strm.avail_out == 0);
} while (!inputFile.eof());
// 解压缩结束
inflateEnd(&strm);
inputFile.close();
outputFile.close();
return true;
}
测试代码
int main() {
std::string inputFileName = "input.txt";
std::string compressedFileName = "compressed.bin";
std::string decompressedFileName = "decompressed.txt";
// 压缩数据
if (CompressData(inputFileName, compressedFileName)) {
std::cout << "Data compressed successfully." << std::endl;
} else {
std::cerr << "Failed to compress data." << std::endl;
return 1;
}
// 解压缩数据
if (DecompressData(compressedFileName, decompressedFileName)) {
std::cout << "Data decompressed successfully." << std::endl;
} else {
std::cerr << "Failed to decompress data." << std::endl;
return 1;
}
return 0;
}