title: Simple Zlib C++ String Compression and Decompression
body: <p>I need a simple compression and decompression of a std::string in C++. I looked at this <a href="https://gist.github.com/arq5x/5315739" rel="noreferrer">site</a> and the code is for Character array. What I want to implement are the two functions:</p>
  
  <pre><code>std::string original = "This is to be compressed!!!!";
  std::string compressed = string_compress(original);
  std::cout &lt;&lt; compressed &lt;&lt; std::endl;
  std::string decompressed = string_decompress(compressed);
  std::cout &lt;&lt; decompressed &lt;&lt; std::endl;
  </code></pre>
  
  <p>I had tried the boost compression as:</p>
  
  <pre><code>std::string CompressData(const std::string &amp;data)
  {
      std::stringstream compressed;
      std::stringstream decompressed;
      decompressed &lt;&lt; data;
      boost::iostreams::filtering_streambuf&lt;boost::iostreams::input&gt; out;
      out.push(boost::iostreams::zlib_compressor());
      out.push(decompressed);
      boost::iostreams::copy(out, compressed);
      return compressed.str();
  }
  
  std::string DecompressData(const std::string &amp;data)
  {
      std::stringstream compressed;
      std::stringstream decompressed;
      compressed &lt;&lt; data;
      boost::iostreams::filtering_streambuf&lt;boost::iostreams::input&gt; in;
      in.push(boost::iostreams::zlib_decompressor());
      in.push(compressed);
      boost::iostreams::copy(in, decompressed);
      return decompressed.str();
  }
  </code></pre>
  
  <p>but the code sometimes gives Null characters in string ie <strong>\u0000</strong>. How do I handle if the compressed data contains these null characters. Is the return type <strong>string</strong> correct? How can I implement function <strong>string_compress</strong> and <strong>string_decompress</strong> using zlib?</p>