format.zip: Flash example

Reading a zip in flash


The format zip library is cross platform, which means that it can be used in flash targets. This overcomes the problems of using zip files in flash applications when programming in Actionscript.

The usage is basically the same, only the data loading differs.

Here's an example assuming that we have the same "test.zip" as created here http://haxe.org/com/libs/format/zip:

static function main() 
{
    //...
    // Create a URLLoader, set its data format to binary, add a complete listener and start loading:
    var ul = new URLLoader();
    ul.dataFormat = URLLoaderDataFormat.BINARY;
    ul.addEventListener(Event.COMPLETE, onZipLoaded);
    ul.load(new URLRequest('test.zip'));        
}

static private function onZipLoaded(e:Event):Void 
{        
    trace('Zip loaded!');
    // Grab the data as a ByteArray out of the URLLoader.data
    var byteArray:ByteArray = cast(e.target, URLLoader).data;
    // Convert the ByteArray into Bytes format
    var bytes:Bytes = Bytes.ofData(byteArray);    
    // Populate the BytesInput stream with our bytes
    var bytesInput = new BytesInput(bytes);
    
    // From here on exactly like usage on neko/cpp targets...
    var reader = new format.zip.Reader(bytesInput);
    var entries:List<format.zip.Data.Entry> = reader.read();            
    var searchEntryFileName = "path/to/textfile.txt";
    for (entry in entries ) {
        if (entry.fileName == searchEntryFileName) {
            var bytes:Bytes = entry.data;
            var string = bytes.toString();
            trace(string); // <--- Traces "Hello zip!"
        }
    }        
}

version #15558, modified 2012-10-20 03:18:14 by bradparks