c ++压缩字节数组 [英] c++ compress byte array

查看:530
本文介绍了c ++压缩字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部问候,



我加载一组图片并生成卷数据。我将此卷数据保存在


无符号字符*卷


数组。


$ b b

现在我想将这个数组保存在一个文件中并检索。在保存之前,我想压缩字节数组,因为卷数据是巨大的。



解决方案

<$> c $ c> volume 在你的示例中不是一个数组。至于压缩,有关于这个话题的书。要了解如何使用C ++快速方便地使用,请查看 boost.iostream < a>库,其中包含 zlib ,< a href =http://www.boost.org/doc/libs/1_44_0/libs/iostreams/doc/classes/gzip.html#basic_gzip_compressor> gzip 和bzip2 压缩程序。



为了抵消我的nitpicking,这里是一个例子(改为 char ,因为它更多的冗长与 unsigned char s) / p>

  #include< fstream> 
#include< boost / iostreams / filtering_streambuf.hpp>
#include< boost / iostreams / stream.hpp>
#include< boost / iostreams / filter / bzip2.hpp>
#include& lt; boost / iostreams / device / array.hpp>
#include< boost / iostreams / copy.hpp>
namespace io = boost :: iostreams;
int main()
{
const size_t N = 1000000;
char * volume = new char [N];
std :: fill_n(volume,N,'a'); // 100,000 letters'a'

io :: stream< io :: array_source>源(体积,N);

{
std :: ofstream file(volume.bz2,std :: ios :: out | std :: ios :: binary);
io :: filtering_streambuf< io :: output> outStream;
outStream.push(io :: bzip2_compressor());
outStream.push(file);
io :: copy(source,outStream);
}
//此时,volume.bz2被写入并关闭。它是48字节长
}


Greetings all,

I load set of images and generate volume data.I save this volume data in a

unsigned char *volume

array.

Now I want to save this array in a file and retrieve.But before saving I want to compress the byte array since the volume data is huge.

Any tips on this?

Thanks in advance.

解决方案

volume in your example is not an array. As for compression, there are books written on the topic. For something quick and easy to use with C++, check out the boost.iostream library, which comes with zlib, gzip, and bzip2 compressors.

To offset my nitpicking, here's an example (changing to char because it's a lot more verbose with unsigned chars)

#include <fstream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/copy.hpp>
namespace io = boost::iostreams;
int main()
{
    const size_t N = 1000000;
    char* volume = new char[N];
    std::fill_n(volume, N, 'a'); // 100,000 letters 'a'

    io::stream< io::array_source > source (volume, N);

    {  
      std::ofstream file("volume.bz2", std::ios::out | std::ios::binary); 
      io::filtering_streambuf<io::output> outStream; 
      outStream.push(io::bzip2_compressor()); 
      outStream.push(file); 
      io::copy(source, outStream); 
     }
    // at this point, volume.bz2 is written and closed. It is 48 bytes long
}

这篇关于c ++压缩字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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