title: GZip a string for output from Coldfusion results in "Content Encoding Error" in browsers
body: <p>I am trying to GZip content in a variable to output to the browser.  To start I am making this very simple and not worrying about browsers that do not support gzip.  Also I have put this together from researching several methods that I could find on the web.  Some of them from people that may be reading this question.</p>
  <pre><code>&lt;cfsavecontent variable=&quot;toGZIP&quot;&gt;&lt;html&gt;&lt;head&gt;&lt;title&gt;Test&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Fear my test&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;&lt;/cfsavecontent&gt;
  
  &lt;cfscript&gt;
  ioOutput = CreateObject(&quot;java&quot;,&quot;java.io.ByteArrayOutputStream&quot;);
  gzOutput = CreateObject(&quot;java&quot;,&quot;java.util.zip.GZIPOutputStream&quot;);
  
  ioOutput.init();
  gzOutput.init(ioOutput);
  
  gzOutput.write(toGZIP.getBytes(&quot;UTF-8&quot;), 0, Len(toGZIP.getBytes()));
  
  gzOutput.finish();
  gzOutput.close();
  ioOutput.flush();
  ioOutput.close();
  
  toOutput=ioOutput.toString(&quot;UTF-8&quot;);
  &lt;/cfscript&gt;
  
  &lt;cfcontent reset=&quot;yes&quot; /&gt;&lt;cfheader name=&quot;Content-Encoding&quot; value=&quot;gzip&quot;&gt;&lt;cfheader name=&quot;Content-Length&quot; value=&quot;#ArrayLen( toOuptut.getBytes() )#&quot; &gt;&lt;cfoutput&gt;#toOuptut#&lt;/cfoutput&gt;&lt;cfabort /&gt;
  </code></pre>
  <p>But I get an error in Firefox (and chrome and Safari)</p>
  <blockquote>
  <p>Content Encoding Error</p>
  <p>The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.</p>
  </blockquote>
  <p>Anybody have any ideas?</p>
  <p>OS: Mac OX-X Snow Leopard<br />
  CF: 9-Dev<br />
  Webserver: Apache</p>
  <hr />
  <p><strong>SOLUTION</strong></p>
  <pre><code>&lt;cfsavecontent variable=&quot;toGZIP&quot;&gt;&lt;html&gt;&lt;head&gt;&lt;title&gt;Test&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Fear my test&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;&lt;/cfsavecontent&gt;
  
  &lt;cfscript&gt;
  ioOutput = CreateObject(&quot;java&quot;,&quot;java.io.ByteArrayOutputStream&quot;);
  gzOutput = CreateObject(&quot;java&quot;,&quot;java.util.zip.GZIPOutputStream&quot;);
  
  ioOutput.init();
  gzOutput.init(ioOutput);
  
  gzOutput.write(toGZIP.getBytes(), 0, Len(toGZIP.getBytes()));
  
  gzOutput.finish();
  gzOutput.close();
  ioOutput.flush();
  ioOutput.close();
  
  toOutput=ioOutput.toByteArray();
  &lt;/cfscript&gt;
  
  &lt;cfheader name=&quot;Content-Encoding&quot; value=&quot;gzip&quot;&gt;&lt;cfheader name=&quot;Content-Length&quot; value=&quot;#ArrayLen(toOutput)#&quot; &gt;&lt;cfcontent reset=&quot;yes&quot; variable=&quot;#toOutput#&quot; /&gt;&lt;cfabort /&gt;
  </code></pre>