How to use protobuf with zlib compression in C++
It's been a while since my last post, where I explained how to build protobuf with zlib support. In this post I'll explain how you can use zlib-based streams in C++ to serialize and deserialize protobuf messages. We'll use a small Mona Lisa bitmap for this example, since its uncompressed format will compress well with zlib's default Deflate lossless compression algorithm.
Take a look at Simverge/howto-protobuf-zlib in GitHub for a more complete implementation of this example.
Define a Protobuf Message Schema
First, let's create a file called blob.proto that defines a simple Protobuf message to store an arbitrary blob of data along with a string identifying its source:
[crayon-6825b584d31ee291433551/]
Compile blob.proto with the Protobuf compiler (protoc) using the instructions from the official Protobuf C++ tutorial. For example, the following command will generate the blob.pb.cc C++ source file and the blob.pb.h C++ header file in the same directory as blob.proto:
[crayon-6825b584d31fc745055719/]
You can instead use the FindProtobuf module if you are building with CMake, for example:
[crayon-6825b584d31fe134003878/]
Once blob.proto is...