zlib解压http deflate demo

 
#include <stdio.h>
#include <string.h>  // for strlen
#include <assert.h>
#include "zlib.h"
 
#include <iostream>
#include <fstream>
#include <sstream>
 
using std::cout; using std::cerr;
using std::endl; using std::string;
using std::ifstream; using std::ostringstream;

// adapted from: http://stackoverflow.com/questions/7540259/deflate-and-inflate-zlib-h-in-c
int main(int argc, char* argv[])
{
    char a[50] = "Hello, world!";
    char b[50];
    char c[50];

    uLong ucompSize = strlen(a)+1; // "Hello, world!" + NULL delimiter.
    uLong compSize = compressBound(ucompSize);

    // Deflate
    compress((Bytef *)b, &compSize, (Bytef *)a, ucompSize);
    printf("compress data is %s",c);
    
    // Inflate
    uncompress((Bytef *)c, &ucompSize, (Bytef *)b, compSize);
    printf("uncompress data is %s",c);
    return 0;
}

### C++ 实现 zlib 压缩与解压 为了展示如何利用 `zlib` 库执行数据的压缩和解压操作,在下面提供了两个函数:一个用于压缩字符串,另一个则负责解压已压缩的数据。 #### 使用 z_stream 进行初始化设置 在调用实际处理之前,需要创建并配置 `z_stream` 结构体实例来保存状态信息。对于压缩过程而言,这涉及到设定输入缓冲区以及预期输出的最大尺寸;而对于解压,则需告知库所期望读取的内容长度或允许其自动检测文件头类型[^2]。 #### 完整的例子如下: ```cpp #include <iostream> #include <vector> #include <string> #include <stdexcept> // Include the ZLIB headers. extern "C" { #include <zlib.h> } std::string compressString(const std::string& input) { // Allocate compressed data buffer with an initial size. unsigned long compressedLength = compressBound(input.size()); std::vector<Bytef> dest(compressedLength); int result = compress(dest.data(), &compressedLength, reinterpret_cast<const Bytef*>(input.c_str()), input.size()); if (result != Z_OK) throw std::runtime_error("Compression failed"); return {dest.begin(), dest.begin() + compressedLength}; } std::string decompressString(const std::string& compressedData) { z_stream strm; memset(&strm, 0, sizeof(strm)); if (inflateInit2(&strm, MAX_WBITS | 16) != Z_OK) // Support both raw DEFLATE and GZIP formats. throw std::runtime_error("Failed to initialize inflater."); try { strm.avail_in = static_cast<uInt>(compressedData.size()); strm.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(compressedData.data())); std::vector<char> outBuffer(8 * 1024); // Output buffer. std::string output; do { strm.avail_out = outBuffer.size(); strm.next_out = reinterpret_cast<Bytef*>(&outBuffer[0]); auto ret = inflate(&strm, Z_NO_FLUSH); switch(ret){ case Z_STREAM_END: break; case Z_NEED_DICT: case Z_DATA_ERROR: case Z_MEM_ERROR: inflateEnd(&strm); throw std::runtime_error("Decompression error"); } if (strm.avail_out == 0 || ret == Z_STREAM_END) { output.append(outBuffer.begin(), outBuffer.end() - strm.avail_out); } } while (strm.avail_out == 0); inflateEnd(&strm); return output; } catch (...) { inflateEnd(&strm); throw; } } ``` 上述代码实现了基本的功能,即接受原始字节流作为参数,并返回经过压缩后的版本;反之亦然。注意这里采用了异常机制来进行错误处理,当遇到不可恢复的情况时会抛出运行期异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信安成长日记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值