<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Red Leopard &#187; gzip</title>
	<atom:link href="http://www.redleopard.com/tag/gzip/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.redleopard.com</link>
	<description>A Stranger in a Strange Land</description>
	<lastBuildDate>Tue, 20 Dec 2011 14:55:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Actionscript GZIP Alternative</title>
		<link>http://www.redleopard.com/2008/12/actionscript-gzip-alternative/</link>
		<comments>http://www.redleopard.com/2008/12/actionscript-gzip-alternative/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 00:08:57 +0000</pubDate>
		<dc:creator>kelly</dc:creator>
				<category><![CDATA[KellyBlog]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[gzip]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.redleopard.com/?p=126</guid>
		<description><![CDATA[I really wish Actionscript 3 had a native decompression utility for opening GZIP compressed files. I really do. After reading (and trying to implement the advice of) numerous postings, I gave up on GZIP. In my investigation, I walked byte-by-byte through numerous binary dumps. Somewhere along the way I noticed a pattern. Forget about the [...]]]></description>
			<content:encoded><![CDATA[<p>I really wish Actionscript 3 had a native decompression utility for opening GZIP compressed files. I really do. After reading (and trying to implement the advice of) numerous postings, I gave up on GZIP.</p>
<p>In my investigation, I walked byte-by-byte through numerous binary dumps. Somewhere along the way I noticed a pattern. Forget about the head and foot bytes, the basic GZIP compressed data is simply not the same as the actionscript base deflate-algorithm compressed data.</p>
<p>People argue with me on that one. They say the compressed bytes are simply deflate-algorithm compressed and that these bytes can be decompressed by actionscript. I have but one comment. &#8220;Good luck with that, Cowboy. Knock yourself out.&#8221;</p>
<p>I have working code in production. Who you gonna believe?</p>
<p>I&#8217;ll start with the flex side first (flex3, as3). Use a URLLoader and set the dataFormat to BINARY or <em>it will not work</em>. Since the source data is not GZIP, I&#8217;ve made up a new file extension &#8216;xmlz&#8217;. </p>
<p>The code in listing 1 fetches the compressed XML, decompresses it and dumps it in a TextArea.</p>
<p>Listing 1. Simple Flex app to load compressed XML</p>
<div class="terminal">
<pre>
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="vertical"
    backgroundGradientColors="[#ffffff, #c0c0c0]"
    width="100%"
    height="100%"&gt;

  &lt;mx:Script&gt;
  &lt;![CDATA[
    import flash.utils.ByteArray;

    [Bindable]
    private var bytes:String = "";

    private var loader:URLLoader = new URLLoader();

    private function urlLoaderSend():void
    {
      loader.addEventListener(Event.COMPLETE, setResult);
      loader.dataFormat = URLLoaderDataFormat.BINARY;
      loader.load(new URLRequest("http://example.com/data.xmlz"));
    }

    private function setResult(event:Event):void
    {
      var ba:ByteArray = loader.data as ByteArray;
      ba.position = 0;
      ba.uncompress();
      bytes = ba.toString();
    }
  ]]&gt;
  &lt;/mx:Script&gt;

  &lt;mx:Button
      label="get playlist"
      click="urlLoaderSend()" /&gt;
  &lt;mx:TextArea
      id="resultTA"
      width="600"
      height="600"
      text="{bytes}" /&gt;
&lt;/mx:Application&gt;
</pre>
</div>
<p>The server-side java is quite simple. I use the StAX XMLStreamWriter to generate XML. The writer&#8217;s constructor takes an OutputStream as an argument. Simply wrap the original OutputStream in a DeflaterOutputStream. The trick is to explicitly create the Deflater. If you don&#8217;t, the default will produce bytes incompatible with actionscript&#8217;s uncompress().</p>
<p>Listing 2. Java DeflaterOutputStream fragment (with StAX writer)</p>
<div class="terminal">
<pre>
OutputStream out = new OutputStream();
    ⋮

Deflater d = new Deflater(Deflater.BEST_COMPRESSION, false);
OutputStream zipper = new DeflaterOutputStream(out, d);

XMLOutputFactory factory = XMLOutputFactory.newInstance();

XMLStreamWriter writer;
writer = factory.createXMLStreamWriter(zipper, "utf-8");
    ⋮
</pre>
</div>
<p>And that&#8217;s it! The compression is size-equivalent with GZIP (within a few bytes). I would rather actionscript supported GZIP. Until it does, I&#8217;ll continue using &#8216;xmlz&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.redleopard.com/2008/12/actionscript-gzip-alternative/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

