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 char
s)
#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
}