Skip to content

Instantly share code, notes, and snippets.

@nna774
Created August 7, 2018 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nna774/3e9bf18b211123dcd91711d0c8484842 to your computer and use it in GitHub Desktop.
Save nna774/3e9bf18b211123dcd91711d0c8484842 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <memory>
#include "deflate.h"
#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
#include "miniz.c"
// using std::begin;
// using std::end;
namespace Deflate {
std::vector<Byte> compress(std::vector<Byte> const& src) {
unsigned long size = mz_compressBound(src.size());
std::unique_ptr<Byte> buf{new Byte[size]};
int status = mz_compress(buf.get(), &size, src.data(), src.size());
if (status != MZ_OK) {
return {};
}
std::vector<Byte> v(size);
std::copy_n(buf.get(), size, begin(v));
return v;
}
std::vector<Byte> decompress(std::vector<Byte> const& src) {
std::vector<Byte> v;
unsigned long size = src.size() * 100; //
Byte *buf = new Byte[size];
int status = mz_uncompress(buf, &size, src.data(), src.size());
if (status != MZ_OK) {
return v;
}
for(unsigned long i{0}; i < size; ++i) {
v.push_back(buf[i]);
}
return v;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment