Search code examples
actionscript-3flex4flash-cs4

Embed unknown file formats


Using the Embed tag, we can embed in the swf file not only swf's or jpg's etc, but also unsupported file formats like:

Embed[(source="mzip.zip", mimeType="application/octet-stream")]

public static MyZip:Class;

When i instantiate such a class, what type of object do i get? Is it possible to get a ByteArray from this class?

When i do this: package { import flash.utils.ByteArray;

public final class Resource {       
    [Embed(source="p2.zip", mimeType="application/octet-stream")]
    public static const MyZip:Class;

    public static function getByteArrayFromZip():ByteArray {
        var zip:ByteArray = new MyZip as ByteArray;
        return zip;
    }
}

}

and

public class Main extends Sprite {
private var ship:Bitmap; private var lib:FZipLibrary;

    public function Main() {            
        lib = new FZipLibrary();    
        lib.formatAsBitmapData(".jpg");       
        lib.addEventListener(Event.COMPLETE,onCompleteHandler);

        var fzip:FZip = new FZip();
        fzip.loadBytes(Resource.getByteArrayFromZip());
        lib.addZip(fzip);
    }

   private function onCompleteHandler(evt:Event) {
        var image:BitmapData = lib.getBitmapData("assets/graphics/scratchers/myimg.jpg");
        trace("Size: " + image.width + "x" + image.height); 
        var sa0img0:Bitmap = new Bitmap(image);
        addChild(sa0img0);
   }
}

}

The i get the following error: TypeError: Error #1009: Cannot access a property or method of a null object reference. at Main/onCompleteHandler() at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at deng.fzip::FZipLibrary/processNext() at deng.fzip::FZipLibrary/loaderCompleteHandler()


Solution

  • Yes, just instantiate MyZip and cast as a ByteArray.

    Embed[(source="mzip.zip", mimeType="application/octet-stream")]
    public static var MyZip:Class;
    
    var zip:ByteArray = new MyZip() as ByteArray;