How to unzip a PNG

I’m trying to extract a PNG from a zip (html5 target).
I’ve tried this zip lib (but maybe there is a 100% openfl solution?), and I’m able to get an xml fromt the zip but I can’t load any PNG (well the bytes are loaded but my BitmapData has width/height of 0 so it is incorrect).

    var entries = new StringMap<ZipEntry>();
	var zip = new ZipReader(bytes);
	var entry:ZipEntry;

	while ( (entry = zip.getNextEntry()) != null )
	  entries.set(entry.fileName, entry);

	var myBytes = Zip.getBytes(entries.get("flipped_left.png"));//it's using  lime.utils.compress.Deflate.decompress()
	var bm = BitmapData.fromBytes(myBytes);
	trace(bm.width);//trace 0
	var b = new Bitmap(bm, PixelSnapping.AUTO, true);

Any clue? Thanks

On HTML5, you cannot BitmapData.fromBytes and have it work immediately. You need to wait for the image to load. If you do not need to read the bitmapData.width or height immediately, you can add it to your Bitmap, and it should display when it is finished loading. Otherwise, it would be better to use BitmapData.loadFromBytes

BitmapData.loadFromBytes (myBytes).onComplete (function (bm) {
    trace (bm.width);
    var b = new Bitmap(bm, PixelSnapping.AUTO, true);

Thanks it works when I use BitmapData.loadFromBytes()!

1 Like