After downloading the library, your first step is to include the header:
You can then compress a buffer of data like this:
char compressed; size_t length = 1000; snappy_status status = snappy_compress( "Hello World!", 12, compressed, &length);
After calling this function, buffer
compressed will contain string
"Hello World!" compressed by Snappy.
length will contain length of the compressed data.
Note that the compressed version can be slightly larger than input in extreme cases.
Snappy allows you to calculate the required size of the output buffer:
char uncompressed = "Hello World!"; size_t length = snappy_max_compressed_length(sizeof(uncompressed)); char *compressed = new char[length]; // ... compress like above
You can decompress compressed buffer like this:
char uncompressed; size_t uncompressedLength = 1000; snappy_status status = snappy_uncompress( compressed, compressedLength, uncompressed, &uncompressedLength);
This works the same as compression above except the process is reversed.
uncompressed will contain our "Hello World!" string and
uncompressedLength will be 12.
Again, we can estimate size of output buffer, but this time it's not a simple constant based on input size.
Snappy stores size of uncompressed data in header of the compressed block.
It has a function that retrieves the exact length of uncompressed data in O(1) time:
size_t uncompressedLength; snappy_status status = snappy_uncompressed_length( compressed, compressedLength, &uncompressedLength);
Snappy also has a function to validate compressed buffer. And there's a whole alternate C++ API that additionally supports pluggable source/sink interfaces that allow you to compress a number of buffers into single solid block. See Snappy header files, which contain documentation for all the public APIs.