54

Extremely Fast Compression Algorithm

 5 years ago
source link: https://www.tuicool.com/articles/hit/FJVzmaU
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 :stuck_out_tongue: ); 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:

compression.cpp :

#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;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK