Skip to content

Instantly share code, notes, and snippets.

@lwu
Created March 12, 2009 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lwu/77921 to your computer and use it in GitHub Desktop.
Save lwu/77921 to your computer and use it in GitHub Desktop.
Want to load compressed JSON files from ActionScript 3? Use this Ruby script (or AIR application) to create the compressed files
#!/usr/bin/env ruby
require 'zlib'
raise "what file?" if ARGV.empty?
puts Zlib::Deflate.deflate(IO.read(ARGV[0]))
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
title="ZlibCompressor"
nativeDragDrop="onDrop(event)"
nativeDragEnter="onDragIn(event)"
>
<mx:Script>
<![CDATA[
// From http://onrails.org/articles/2007/11/27/flash-utils-bytearray-compressing-4-1mb-to-20k
import flash.desktop.ClipboardFormats;
import flash.utils.CompressionAlgorithm;
import flash.desktop.NativeDragManager;
public function onDragIn(event:NativeDragEvent):void {
var transferable:Clipboard = event.clipboard;
if (transferable.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)) {
NativeDragManager.acceptDragDrop(this);
}
}
public function onDrop(event:NativeDragEvent):void {
var fileList:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
if (fileList.length==0) return;
var inFile:File = fileList[0];
var fileStream:FileStream = new FileStream();
fileStream.open(inFile, FileMode.READ);
var ba:ByteArray = new ByteArray();
fileStream.readBytes(ba, 0, fileStream.bytesAvailable);
fileStream.close();
var newFileName:String = inFile.nativePath+".zlib";
ba.compress();
var outFile:File = new File(newFileName);
fileStream = new FileStream();
fileStream.open(outFile, FileMode.WRITE);
fileStream.writeBytes(ba, 0, ba.length);
fileStream.close();
}
]]>
</mx:Script>
</mx:WindowedApplication>
<application xmlns="http://ns.adobe.com/air/application/1.5">
<id>edu.stanford.Zlib</id>
<version>0.1</version>
<filename>ZlibCompressor</filename>
<initialWindow>
<content>ZlibCompressor.swf</content>
<visible>true</visible>
<width>640</width>
<height>480</height>
</initialWindow>
</application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment