zlib使用

项目中添加lib库
demo

// zlib_demo.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include <iostream>
#include <string>
#include "zlib.h" //导入zlib.lib之后,把zlib.h头文件包含
#include <fstream>

#define CHUNK 16384

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	char mid[CHUNK], out[CHUNK], in[CHUNK], hc[] = {'\n'};
	
	string src_file = "E:\\Test_2_project\\src.txt";
	fstream p_src_stream(src_file, ios::in);
	if (!p_src_stream)
	{
		std::cout << "open file failed" << endl;
		return -1;
	}

	char *zip_file = "E:\\Test_2_project\\test.gz";
	string des_file = "E:\\Test_2_project\\dest.txt";
	fstream p_dest_stream(des_file, ios::out);
	//gz方式打开文件
	gzFile gzfp = gzopen(zip_file,"wb");
	if (!gzfp)
	{
		cout << "open zip file failed" << endl;
		return -1;
	}

	cout<<"gztell:"<<gztell(gzfp)<<endl;  

	while (1) {
		//文件内容读入内存in中
		p_src_stream.getline(in, CHUNK);
		//cout << "in: " << in << endl;
		//压缩成gz文件
		gzwrite(gzfp, in, strlen(in) + 1);
		gzwrite(gzfp, hc, 1);
		//压缩
		z_stream stream;
		stream.zalloc = Z_NULL;
		stream.zfree = Z_NULL;
		stream.opaque = Z_NULL;
		//数据压缩初始化
		deflateInit(&stream, Z_DEFAULT_COMPRESSION);
		stream.avail_in = strlen(in) + 1;
		stream.next_in = (Bytef *)in;
		stream.avail_out = sizeof(mid);
		stream.next_out = (Bytef *)mid;

		deflate(&stream, Z_FINISH);
		//deflate(&stream, Z_NO_FLUSH);
		deflateEnd(&stream);
		//解压
		z_stream strm;
		strm.zalloc = Z_NULL;
		strm.zfree = Z_NULL;
		strm.opaque = Z_NULL;

		inflateInit(&strm);
		strm.avail_in = strlen(mid) + 1;
		strm.next_in = (Bytef *)mid;
		strm.avail_out = sizeof(out);
		strm.next_out = (Bytef *)out;

		//inflate(&strm, Z_NO_FLUSH);
		inflate(&strm, Z_FINISH);
		inflateEnd(&strm);
		//cout << "out: " << out << endl;
		//解压数据写会文件中
		if (p_dest_stream.is_open())
		{
			p_dest_stream << out << endl;
		}

		if (p_src_stream.eof())
			break;
	};
	p_src_stream.close();
	p_dest_stream.close();
	gzclose(gzfp);
	system("pause");
	return 0;
}
C++使用zlib库可以实现数据的压缩和解压缩。下面是一个简单的示例代码: ```cpp #include <iostream> #include <fstream> #include <string> #include <zlib.h> // 压缩函数 bool compressFile(const std::string& sourceFile, const std::string& destFile) { std::ifstream inFile(sourceFile, std::ios::binary); std::ofstream outFile(destFile, std::ios::binary); if (!inFile || !outFile) { return false; } z_stream stream; stream.zalloc = Z_NULL; stream.zfree = Z_NULL; stream.opaque = Z_NULL; if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) { return false; } char inBuffer[1024]; char outBuffer[1024]; stream.avail_in = inFile.readsome(inBuffer, sizeof(inBuffer)); stream.next_in = reinterpret_cast<Bytef*>(inBuffer); do { stream.avail_out = sizeof(outBuffer); stream.next_out = reinterpret_cast<Bytef*>(outBuffer); deflate(&stream, Z_FINISH); outFile.write(outBuffer, sizeof(outBuffer) - stream.avail_out); } while (stream.avail_out == 0); deflateEnd(&stream); inFile.close(); outFile.close(); return true; } // 解压缩函数 bool decompressFile(const std::string& sourceFile, const std::string& destFile) { std::ifstream inFile(sourceFile, std::ios::binary); std::ofstream outFile(destFile, std::ios::binary); if (!inFile || !outFile) { return false; } z_stream stream; stream.zalloc = Z_NULL; stream.zfree = Z_NULL; stream.opaque = Z_NULL; if (inflateInit(&stream) != Z_OK) { return false; } char inBuffer[1024]; char outBuffer[1024]; stream.avail_in = inFile.readsome(inBuffer, sizeof(inBuffer)); stream.next_in = reinterpret_cast<Bytef*>(inBuffer); do { stream.avail_out = sizeof(outBuffer); stream.next_out = reinterpret_cast<Bytef*>(outBuffer); inflate(&stream, Z_NO_FLUSH); outFile.write(outBuffer, sizeof(outBuffer) - stream.avail_out); } while (stream.avail_out == 0); inflateEnd(&stream); inFile.close(); outFile.close(); return true; } int main() { std::string sourceFile = "input.txt"; std::string compressedFile = "compressed.zlib"; std::string decompressedFile = "decompressed.txt"; // 压缩文件 if (compressFile(sourceFile, compressedFile)) { std::cout << "File compressed successfully." << std::endl; } else { std::cout << "Failed to compress file." << std::endl; } // 解压缩文件 if (decompressFile(compressedFile, decompressedFile)) { std::cout << "File decompressed successfully." << std::endl; } else { std::cout << "Failed to decompress file." << std::endl; } return 0; } ``` 这个示例代码演示了如何使用zlib库进行文件的压缩和解压缩。你可以根据自己的需求修改输入输出文件的路径和名称。在压缩文件时,使用`deflate`函数将输入数据压缩,并将压缩后的数据写入输出文件。在解压缩文件时,使用`inflate`函数将压缩数据解压缩,并将解压缩后的数据写入输出文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值