title: Why does base64-encoded data compress so poorly?
body: <p>I was recently compressing some files, and I noticed that base64-encoded data seems to compress really bad. Here is one example:</p>
  
  <ul>
  <li>Original file: <strong>429,7 MiB</strong></li>
  <li>compress via <code>xz -9</code>:<br/>
  <code>13,2 MiB / 429,7 MiB = 0,031</code> <code>4,9 MiB/s</code> <code>1:28</code></li>
  <li><code>base64</code> it and compress via <code>xz -9</code>:<br/>
  <code>26,7 MiB / 580,4 MiB = 0,046</code> <code>2,6 MiB/s</code> <code>3:47</code></li>
  <li><code>base64</code> the original compressed xz file:<br/>
  <code>17,8 MiB</code> in almost no time = the expected <code>1.33x</code> increase in size</li>
  </ul>
  
  <p>So what can be observed is that:</p>
  
  <ul>
  <li>xz compresses really good ☺</li>
  <li>base64-encoded data doesn't compress well, it is 2 times larger than the unencoded compressed file</li>
  <li><em>base64-then-compress</em> is significantly worse and slower than <em>compress-then-base64</em></li>
  </ul>
  
  <p>How can this be? Base64 is a lossless, reversible algorithm, why does it affect compression so much? (I tried with gzip as well, with similar results).</p>
  
  <p>I know it doesn't make sense to <em>base64-then-compress</em> a file, but most of the time one doesn't have control over the input files, and I would have thought that since the actual information density (or whatever it is called) of a base64-encoded file would be nearly identical to the non-encoded version, and thus be similarily compressible.</p>