title: QBtyeArray QDataStream qCompress to file adding extra leading bytes
body: <p>Qt/C++ program has a function which writes an objects data (<code>_token</code>) to a file as follows:</p>
  <pre><code>    QFile f( _tokenFile);
  
      if (!f.open( QIODevice::WriteOnly)) {
          qDebug() &lt;&lt; &quot;Unable to open token file for writing&quot; &lt;&lt; f.errorString();
          return false;
      }
  
      QByteArray tokenBa;
      QDataStream ds( &amp;tokenBa, QIODevice::WriteOnly);
      ds &lt;&lt; _token;
  
      tokenBa = qCompress( tokenBa);
  
      f.write( tokenBa);
      f.close();
  </code></pre>
  <p>The _token is an instance of the following <code>struct</code>:</p>
  <pre><code>struct Token {
      QString accessToken;
      QString refreshToken;
      QString clientSecret;
      QString authCode;
      QTime expiryTime;
  
  
      enum AuthState {
          A_Invalid,
          A_RequestAuth,
          A_Authenticated,
      };
  
      AuthState state;
  
      Token() : state( A_Invalid) {}
  
      bool isValid() const {
          if (accessToken.isEmpty() ||
              refreshToken.isEmpty()) {
              return false;
          }
          return true;
      }
  
      void inValidate() {
          accessToken.clear();
          refreshToken.clear();
          clientSecret.clear();
          authCode.clear();
          expiryTime = QTime();
      }
  
      void cleanUp() {
          accessToken.clear();
          refreshToken.clear();
          clientSecret.clear();
          authCode.clear();
          expiryTime = QTime();
      }
  };
  
  </code></pre>
  <p>When the file is saved it has 4 extra bytes at the start which render the file as a invalid zlib file.</p>
  <pre><code>
  0000000 0000 5e01 9c78 904d 4f5d 5082 871c fa9f
  0000020 353e 25cd 6975 2c2f d563 4c2c 62b8 cad1
  </code></pre>
  <p>We can see above bytes 5-6 are <code>9C 78</code> which is the zlib signature, but the 4 bytes before these are the issue.</p>
  <p>To check the compressed data is correct I do the following:</p>
  <p><code>dd if=file.token bs=1 skip=4 | openssl zlib -d</code></p>
  <p>And this produces the expected result (for testing).</p>
  <p>The problem is in the application reading this data back into the data object:</p>
  <pre><code>    QFile f( _tokenFile);
  
      if (!f.exists()) {
          qDebug() &lt;&lt; &quot;Token file doesn't exist&quot;  &lt;&lt; f.fileName();
          return false;
      }
  
      if (!f.open( QIODevice::ReadOnly)) {
          qDebug() &lt;&lt; &quot;Unable to open token file for reading&quot; &lt;&lt; f.errorString();
          return false;
      }
  
      QByteArray tokenBa = f.readAll();
      f.close();
  
      if (tokenBa.isEmpty()) {
          qDebug() &lt;&lt; &quot;Token file is empty.&quot;;
          return false;
      }
  
      tokenBa = qUncompress( tokenBa);
  
      QDataStream ds( &amp;tokenBa, QIODevice::ReadOnly);
      ds &gt;&gt; _token;
  </code></pre>
  <p>This returns null - because of the leading 4 extraneous bytes. I could put some code in to skip these 4 leading bytes, but how do I know it will always be 4 bytes? I'd like to instead have certainly that the files data is all zlib compressed.</p>
  <p>My question is how to avoid those bytes being saved in the first place so that on re-read the format is known to be zlib type?</p>