Zlib experiments

August 11th, 2006

I’m getting tired, but I have the following for you all, a working sample of this whole compression thing that I’ve become interested in lately. You’ll need Python for this. You should also know how to compile and debug an AS3 class. Save the following code sample as “zlib.py” and put it along with the file you wish to compress somewhere. The file you want to compress should be a text file for this example, although any file will work.

[Python]
#!/usr/bin/env python

import zlib
import os, sys

# Make windows output as binary
if sys.platform == “win32”:
import msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

if len(sys.argv) < 2:
sys.exit(“No filename given”)
else:
f = open(sys.argv[1], “rb”)

deflate = zlib.compressobj(9)

while 1:
buf = f.read(1024);
if buf:
sys.stdout.write(deflate.compress(buf))
else:
break;
sys.stdout.write(deflate.flush(zlib.Z_FINISH));
[/Python]

Open the directory where you put the files in the command line.

  • If you’re using Windows then type “zlib.py filename > compressed.zlib” at the command line. Replace “filename” with the name of the file you want to compress.
  • If you’re using Mac OS X (or another Unix OS) first make sure you can execute the file, then type “./zlib.py filename > compressed.zlib” at the command line. Replace “filename” with the name of the file you want to compress.

Save the following Actionscript 3.0 file as “Testify.as” and compile it.

[Actionscript]
package {
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.events.Event;
import flash.events.IOErrorEvent;

public class Testify {
static private var url:String = “compressed.zlib”;

private var urlLoader:URLLoader;

public function Testify() {
trace(“Loading “+url);
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Eve​nt.COMPLETE,onComplete);
urlLoader.addEventListener(IOE​rrorEvent.IO_ERROR,onError);
urlLoader.load(new URLRequest(wadUrl));
}
private function onComplete(e:Event):void {
trace(“Loaded”);
var data:ByteArray = urlLoader.data;
urlLoader.data = null;
urlLoader.close();
urlLoader = null;

data.uncompress();

data.position = 0;
trace(data.readUTFBytes(data.b​ytesAvailable));
}
private function onError(e:Event):void {
trace(“Error”);
}
}
}
[/Actionscript]

Alright, now make sure the “compressed.zlib” is in the same folder as your SWF file, and use either the Flex debugger (flc.exe) or (if you’re using Eclipse) debug the class as an Actionscript project. Your output should be the original file.

3 Responses to “Zlib experiments”

  1. lwu Says:

    In Ruby:

    #!/usr/bin/env ruby
    require ‘zlib’
    raise “what file?” if ARGV.empty?
    puts Zlib::Deflate.deflate(IO.read(ARGV[0]))

    or in Adobe AIR 1.5:

    http://gist.github.com/77921

  2. S. PANTS Says:

    This was extremely useful — yours is the only good documentation with clear examples on the net. WOOT!

  3. Max Says:

    Iwu: This is an old, old post. I agree that AS3 is not the best language for conciseness though. The Python bit could be a lot better too, actually. Wow, I can’t believe I wrote that. Ugh.

    #!/usr/bin/env python
    import sys, zlib
    print zlib.compress(sys.stdin.read())

Leave a Reply