c++使用libz库压缩gzip格式内存字节,节省网络传输

1、头文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>

// gzCompress: do the compressing
int gzCompress(uint8_t *src, int srcLen, uint8_t *dest, int destLen)
{
    z_stream c_stream;
    int err = 0;
    int windowBits = 15;
    int GZIP_ENCODING = 16;

    if(src && srcLen > 0)
    {
        c_stream.zalloc = (alloc_func)0;
        c_stream.zfree = (free_func)0;
        c_stream.opaque = (voidpf)0;
        if(deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
                        windowBits | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY) != Z_OK) return -1;
        c_stream.next_in  = (Bytef *)src;
        c_stream.avail_in  = srcLen;
        c_stream.next_out = (Bytef *)dest;
        c_stream.avail_out  = destLen;
        while (c_stream.avail_in != 0 && c_stream.total_out < destLen)
        {
            if(deflate(&c_stream, Z_NO_FLUSH) != Z_OK) return -1;
        }
        if(c_stream.avail_in != 0) return c_stream.avail_in;
        for (;;) {
            if((err = deflate(&c_stream, Z_FINISH)) == Z_STREAM_END) break;
            if(err != Z_OK) return -1;
        }
        if(deflateEnd(&c_stream) != Z_OK) return -1;
        return c_stream.total_out;
    }
    return -1;
}

// gzDecompress: do the decompressing
int gzDecompress(const char *src, int srcLen, const char *dst, int dstLen){
    z_stream strm;
    strm.zalloc=NULL;
    strm.zfree=NULL;
    strm.opaque=NULL;

    strm.avail_in = srcLen;
    strm.avail_out = dstLen;
    strm.next_in = (Bytef *)src;
    strm.next_out = (Bytef *)dst;

    int err=-1, ret=-1;
    err = inflateInit2(&strm, MAX_WBITS+16);
    if (err == Z_OK){
        err = inflate(&strm, Z_FINISH);
        if (err == Z_STREAM_END){
            ret = strm.total_out;
        }
        else{
            inflateEnd(&strm);
            return err;
        }
    }
    else{
        inflateEnd(&strm);
        return err;
    }
    inflateEnd(&strm);
    return err;
}

2、在函数中调用压缩:

uint8_t msg [1400];
        msg[0] = 0x47;
        msg[1] = 0x01;
        msg[2] = 0x01;
        msg[3] = 0x02;
        msg[4] = 0x00;
        msg[5] = 0x02;
        msg[6] = 0x00;
        uint8_t* data = (uint8_t *)msg;//"2222per3434534a.msg333"
        int datalen = 7;//period_data.msg_len
        int blen = compressBound(datalen);//compressBound(datalen)
        uint8_t* compressed = (uint8_t*)malloc(blen);
        memset(compressed, 0, blen);
        int comLen;
        int gzSize = gzCompress(data, datalen, compressed, blen);
        int i = 0;
        uint8_t* print_buffer = new uint8_t[datalen+1];
        memset(print_buffer, 0x00, datalen+1);

        while(i < datalen && i < 1400)
        {
            sprintf((char*)(print_buffer+2*i), "%02x", data[i]);
            ++i;
        }
        char* uncompressed = (char*)malloc(datalen);
        memset(uncompressed, 0, datalen);
        int ret = gzDecompress(compressed, gzSize, uncompressed, datalen);//gzip解压,测试用
        
        LOGE("1111zdzdzdzd=%s",print_buffer);
        LOGE("1111zzz,data=%02x,datalen= %d",data,datalen);
        LOGE("1111qqq,data=%02x,blen= %d",data,blen);
        LOGE("1111www,data=%02x,gzSize= %d",compressed,gzSize);

        LOGE("1111ggg=%s,%d",(uint8_t*)compressed,comLen);

测试可以正常使用

c++IO流读写:

FILE* resStream;
char *fileName ="collect/resFile";
resStream = fopen(fileName, "wb");
fwrite((char *)period_data.msg, period_data.msg_len, 1, resStream);
fflush(resStream);
fclose(resStream);
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值