如何使用zlib压缩缓冲区? [英] How to compress a buffer with zlib?

查看:261
本文介绍了如何使用zlib压缩缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

zlib网站上有一个使用示例: http://www.zlib.net/zlib_how.html



但是在示例中,他们正在压缩文件。我想压缩存储在内存中缓冲区中的二进制数据。我不想将压缩缓冲区保存到磁盘。



基本上这里是我的缓冲区:

  fIplImageHeader-> imageData =(char *)imageIn-> getFrame(); 

如何使用zlib压缩?

解决方案

这是一个用zlib包装缓冲区的示例,将压缩内容保存在向量中。

  void compress_memory(void * in_data,size_t in_data_size,std :: vector< uint8_t> ; out_data)
{
std :: vector< uint8_t>缓冲;

const size_t BUFSIZE = 128 * 1024;
uint8_t temp_buffer [BUFSIZE];

z_stream strm;
strm.zalloc = 0;
strm.zfree = 0;
strm.next_in = reinterpret_cast< uint8_t *>(in_data);
strm.avail_in = in_data_size;
strm.next_out = temp_buffer;
strm.avail_out = BUFSIZE;

deflateInit(& strm,Z_BEST_COMPRESSION);

while(strm.avail_in!= 0)
{
int res = deflate(& strm,Z_NO_FLUSH);
assert(res == Z_OK);
if(strm.avail_out == 0)
{
buffer.insert(buffer.end(),temp_buffer,temp_buffer + BUFSIZE);
strm.next_out = temp_buffer;
strm.avail_out = BUFSIZE;
}
}

int deflate_res = Z_OK;
while(deflate_res == Z_OK)
{
if(strm.avail_out == 0)
{
buffer.insert(buffer.end(),temp_buffer, temp_buffer + BUFSIZE);
strm.next_out = temp_buffer;
strm.avail_out = BUFSIZE;
}
deflate_res = deflate(& strm,Z_FINISH);
}

assert(deflate_res == Z_STREAM_END);
buffer.insert(buffer.end(),temp_buffer,temp_buffer + BUFSIZE - strm.avail_out);
deflateEnd(& strm);

out_data.swap(buffer);
}


There is a usage example at the zlib website: http://www.zlib.net/zlib_how.html

However in the example they are compressing a file. I would like to compress a binary data stored in a buffer in memory. I don't want to save the compressed buffer to disk either.

Basically here is my buffer:

fIplImageHeader->imageData = (char*)imageIn->getFrame();

How can I compress it with zlib?

I would appreciate some code example of how to do that.

解决方案

This is an example to pack a buffer with zlib and save the compressed contents in a vector.

void compress_memory(void *in_data, size_t in_data_size, std::vector<uint8_t> &out_data)
{
 std::vector<uint8_t> buffer;

 const size_t BUFSIZE = 128 * 1024;
 uint8_t temp_buffer[BUFSIZE];

 z_stream strm;
 strm.zalloc = 0;
 strm.zfree = 0;
 strm.next_in = reinterpret_cast<uint8_t *>(in_data);
 strm.avail_in = in_data_size;
 strm.next_out = temp_buffer;
 strm.avail_out = BUFSIZE;

 deflateInit(&strm, Z_BEST_COMPRESSION);

 while (strm.avail_in != 0)
 {
  int res = deflate(&strm, Z_NO_FLUSH);
  assert(res == Z_OK);
  if (strm.avail_out == 0)
  {
   buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE);
   strm.next_out = temp_buffer;
   strm.avail_out = BUFSIZE;
  }
 }

 int deflate_res = Z_OK;
 while (deflate_res == Z_OK)
 {
  if (strm.avail_out == 0)
  {
   buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE);
   strm.next_out = temp_buffer;
   strm.avail_out = BUFSIZE;
  }
  deflate_res = deflate(&strm, Z_FINISH);
 }

 assert(deflate_res == Z_STREAM_END);
 buffer.insert(buffer.end(), temp_buffer, temp_buffer + BUFSIZE - strm.avail_out);
 deflateEnd(&strm);

 out_data.swap(buffer);
}

这篇关于如何使用zlib压缩缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆