LZ4. GitHub repository here. It is open-source, available on pretty much every platform, and widely used in the industry.
It was extremely easy to get started with it. The C API could not possibly be any simpler (I’m looking at you zlib 😛 ); you pass in 4 parameters to the compression and decompression functions: input buffer, input length, output buffer, and max output length. They return either the number of bytes produced on the output, or an error code. Just be careful when compressing random data (which you should not be doing anyways!): the output is larger than the input!
Here’s a short example that compresses a vector of thousand characters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <iostream> #include <vector> #include <lz4.h> using namespace std; using buffer = vector<char>; void lz4_compress(const buffer& in, buffer& out) { auto rv = LZ4_compress_default(in.data(), out.data(), in.size(), out.size()); if(rv < 1) cerr << "Something went wrong!" << endl; else out.resize(rv); } void lz4_decompress(const buffer& in, buffer& out) { auto rv = LZ4_decompress_safe(in.data(), out.data(), in.size(), out.size()); if(rv < 1) cerr << "Something went wrong!" << endl; else out.resize(rv); } int main(int argc, char** argv) { buffer data(1000, 'X'); buffer compressed(data.size()), decompressed(data.size()); lz4_compress(data, compressed); cout << "LZ4 compress, bytes in: " << data.size() << ", bytes out: " << compressed.size() << endl; lz4_decompress(compressed, decompressed); cout << "LZ4 decompress, bytes in: " << compressed.size() << ", bytes out: " << decompressed.size() << endl; if(data != decompressed) cerr << "Oh snap! Data mismatch!" << endl; else cout << "Decompressed data matches original :o)" << endl; return 1; } |
LZ4 compress, bytes in: 1000, bytes out: 14
Program output.
LZ4 decompress, bytes in: 14, bytes out: 1000
Decompressed data matches original :o)
any chance you can post output?
done!
awesome. thanks!
awesome blog BTW, keep it up!
thanks! I’m trying to produce a lot of content. I hope people are reading 😉 though I’ll probably slow down soon and stop posting every day 🙂 send me topics you would like me to blog about at [email protected], i need post ideas!
we all read the blog 🙂
Sounds good, I’ll have some ideas.
I can’ remember if you already had stuff related to future/promise.