title: Protobuf with GZIP compression
body: <p>I am having a problem in trying to serialize an array of <code>unsigned char</code> into file with <code>GZIP</code> compression using <code>protobuf</code> while playing with the library.</p>
  
  <p>I think the problem might have to do with some of my syntax or misuse of API.
  I have also tried <code>std::fstream</code>.
  FYI, Windows 8.1 &amp; VS2013 is the building environment.</p>
  
  <p>scene.proto</p>
  
  <pre><code>syntax = "proto3";
  
  package Recipe;
  
  message Scene
  {
      repeated int32 imageData = 1 [packed=true];
  }
  </code></pre>
  
  <p>source.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;ostream&gt;
  #include &lt;istream&gt;
  #include &lt;string&gt;
  #include &lt;cstdint&gt;
  #include "Scene.pb.h"
  #include &lt;google\protobuf\io\zero_copy_stream_impl.h&gt;
  #include &lt;google\protobuf\io\gzip_stream.h&gt;
  
  int const _MIN = 0;
  int const _MAX = 255;
  unsigned int const _SIZE = 65200000;
  unsigned int const _COMPRESSION_LEVEL = 10;
  
  void randWithinUnsignedCharSize(uint8_t * buffer, unsigned int size)
  {
      for (size_t i = 0; i &lt; size; ++i)
      {
          buffer[i] = _MIN + (rand() % static_cast&lt;int&gt;(_MAX - _MIN + 1));
      }
  }
  
  using namespace google::protobuf::io;
  
  int main()
  {
      GOOGLE_PROTOBUF_VERIFY_VERSION;
  
      Recipe::Scene * scene = new Recipe::Scene();
      uint8_t * imageData = new uint8_t[_SIZE];
      randWithinUnsignedCharSize(imageData, _SIZE);
  
      for (size_t i = 0; i &lt; _SIZE; i++)
      {
          scene-&gt;add_imagedata(imageData[i]);
      }
      std::cout &lt;&lt; "scene-&gt;imagedata_size() " &lt;&lt; scene-&gt;imagedata_size() &lt;&lt; std::endl;
      {
          std::ofstream output("scene.art", std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
          OstreamOutputStream outputFileStream(&amp;output);
          GzipOutputStream::Options options;
          options.format = GzipOutputStream::GZIP;
          options.compression_level = _COMPRESSION_LEVEL;
  
          GzipOutputStream gzipOutputStream(&amp;outputFileStream, options);
  
          if (!scene-&gt;SerializeToZeroCopyStream(&amp;gzipOutputStream)) {
              std::cerr &lt;&lt; "Failed to write scene." &lt;&lt; std::endl;
              return -1;
          }
      }
      Recipe::Scene * scene1 = new Recipe::Scene();
      {
          std::ifstream input("scene.art", std::ifstream::in | std::ifstream::binary);
          IstreamInputStream inputFileStream(&amp;input);
          GzipInputStream gzipInputStream(&amp;inputFileStream);
  
          if (!scene1-&gt;ParseFromZeroCopyStream(&amp;gzipInputStream)) {
              std::cerr &lt;&lt; "Failed to parse scene." &lt;&lt; std::endl;
              return -1;
          }
      }
      std::cout &lt;&lt; "scene1-&gt;imagedata_size() " &lt;&lt; scene1-&gt;imagedata_size() &lt;&lt;std::endl;
      google::protobuf::ShutdownProtobufLibrary();
      return 0;
  }
  </code></pre>