onrails.org home

flash.utils.ByteArray compressing 4.1MB to 20K

I am currently preparing a demo using an mx:OLAPCube and OLAPDataGrid which analyze the Rails svn commit log. however I don’t want to deploy a specific server side application as the Cube can load data from XML. So I have an report.xml that is 4.1MB. I created the following AIR application (ZlibCompressor.mxml) that use the standard compression provided by the ByteArray class to compress this file down to 20Kb. The application that consumes this file (UnzipTest.mxml) uses the URLLoader to read this file straight into a ByteArray and uncompress the data. It’s fast!

The key code for compression is the ‘compress’ and ‘uncompress’ method provided by the ByteArray. Note the URLLoader dataFormat is set to “binary”.


<?xml version=“1.0” encoding=“utf-8”?>

import flash.desktop.ClipboardFormats;
import flash.utils.CompressionAlgorithm;
public function onDragIn(event:NativeDragEvent):void {
var transferable:Clipboard = event.clipboard;
if (transferable.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)) {
public function onDrop(event:NativeDragEvent):void {
var fileList:Array = event.clipboard.dataForFormat(ClipboardFormats.FILE_LIST_FORMAT) as Array;
if (fileList.length==0) return;

var inFile:File = fileList0;
var fileStream:FileStream = new FileStream();
fileStream.open(inFile, FileMode.READ);
var ba:ByteArray = new ByteArray();
fileStream.readBytes(ba, 0, fileStream.bytesAvailable);

var newFileName:String = inFile.nativePath+".zlib";

var outFile:File = new File(newFileName);
fileStream = new FileStream();
fileStream.open(outFile, FileMode.WRITE);
fileStream.writeBytes(ba, 0, ba.length);


<?xml version=“1.0” encoding=“utf-8”?>

import mx.rpc.events.ResultEvent;
import flash.utils.ByteArray;

import flash.events.*; import flash.net.*;

private function loadData():void {
var loader:URLLoader = new URLLoader();
loader.dataFormat = “binary”;
loader.addEventListener(Event.COMPLETE, completeHandler);
var request:URLRequest = new URLRequest(“../data/report.xml.zlib”);
private function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
var ba:ByteArray = loader.data;
var s:String = ba.toString();
var xml:XML = new XML;

Fork me on GitHub