概述
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, comLen);
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);
最后
以上就是魁梧灯泡为你收集整理的c++使用libz库压缩gzip格式内存字节,节省网络传输的全部内容,希望文章能够帮你解决c++使用libz库压缩gzip格式内存字节,节省网络传输所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复