28 ZStream(
int mode,
int level) {
33 strm.next_in = Z_NULL;
39 }
else if (mode == 1) {
41 }
else if (mode == 2) {
44 throw std::runtime_error(
"unknown Zlib compression mode supplied");
47 int ret = deflateInit2(&strm, level, Z_DEFLATED, windowBits, 8, Z_DEFAULT_STRATEGY);
49 throw std::runtime_error(
"failed to initialize Zlib buffer compression");
60 ZStream(
const ZStream&) =
delete;
61 ZStream(ZStream&&) =
delete;
62 ZStream& operator=(
const ZStream&) =
delete;
63 ZStream& operator=(ZStream&&) =
delete;
80 ZlibBufferWriter(
int mode = 2,
int compression_level = 6,
size_t buffer_size = 65536) : my_zstr(mode, compression_level), my_holding(buffer_size) {}
85 void write(
const unsigned char* buffer,
size_t n) {
86 my_zstr.strm.next_in =
const_cast<unsigned char*
>(buffer);
87 my_zstr.strm.avail_in = n;
92 my_zstr.strm.next_in =
nullptr;
93 my_zstr.strm.avail_in = 0;
99 std::vector<unsigned char> my_holding;
101 void dump(
int flag) {
103 my_zstr.strm.avail_out = my_holding.size();
104 my_zstr.strm.next_out = my_holding.data();
105 deflate(&(my_zstr.strm), flag);
106 size_t compressed = my_holding.size() - my_zstr.strm.avail_out;
107 output.insert(output.end(), my_holding.begin(), my_holding.begin() + compressed);
108 }
while (my_zstr.strm.avail_out == 0);
116 std::vector<unsigned char> output;