title: GZIPOutputStream: Increase compression level
body: <p><code>java.util.zip.GZIPOutputStream</code> does not provide a constructor argument or a setter for the compression level of its underlying <code>Deflater</code>.</p>
  
  <p>There are ways to work around this issue, as described <a href="http://www.redirecttonull.com/?p=134" rel="noreferrer">here</a>, for example:</p>
  
  <pre><code>GZIPOutputStream gzip = new GZIPOutputStream(output) {
      {
          this.def.setLevel(Deflater.BEST_COMPRESSION);
      }
  };
  </code></pre>
  
  <p>I GZIPped a 10G file with this and its size didn't decrease by a single bit compared to using the preset <code>DEFAULT_COMPRESSION.</code></p>
  
  <p>The <a href="https://stackoverflow.com/a/17634304/1845976">answer</a> to <a href="https://stackoverflow.com/questions/17632314/does-deflater-setlevel-work">this question</a> says that under certain circumstances setting the level might not work as planned. Just to make sure, I also tried to create a new <code>Deflater</code>:</p>
  
  <pre><code>this.def = new Deflater(Deflater.BEST_COMPRESSION, true);
  </code></pre>
  
  <p>But sill no reduction in file size...</p>
  
  <p>Is there a reason why they did not provide access to the <code>Deflater</code> level?</p>
  
  <p>Or is something wrong with the code sample above? </p>
  
  <p>Does the deflater level work at all?</p>
  
  <p><strong>Edit</strong>: Thanks for the <strong>comments</strong>.</p>
  
  <ol>
  <li><p>Can the file be compressed any further?</p>
  
  <p>It's a UTF-8 text file that is compressed from 10G to 10M using
  Default compression. So without knowing details about the
  compression levels, I reckoned it could be compressed further.</p></li>
  <li><p>Time difference between <code>DEFAULT_COMPRESSION</code> and <code>BEST_COMPRESSION</code>? </p>
  
  <p>I don't have time to create really reliable figures. But I executed the code with each compression level about five times and both take about the same time (2 minutes +/- 5 seconds).</p></li>
  <li><p>File size with <code>gzip -v9</code>?
  The file created by gzip is about 15KB smaller than the one created by java. So, for my specific use case it's not worth investigating this topic any further.</p></li>
  </ol>
  
  <p>However, the three fundamental questions stated above still persist. Anyone ever successfully decreased a file using higher compression levels with <code>GZIPOutputStream</code>?</p>