Skip to content

Instantly share code, notes, and snippets.

@PrimaryFeather
Last active June 4, 2018 07:24
Show Gist options
  • Save PrimaryFeather/6c9874fb2c38c8376554c51f47b91777 to your computer and use it in GitHub Desktop.
Save PrimaryFeather/6c9874fb2c38c8376554c51f47b91777 to your computer and use it in GitHub Desktop.
Makes Starling's AssetManager recognize and unzip zipped assets. Depends on FZip: http://codeazur.com.br/lab/fzip/
package starling.extensions
{
import deng.fzip.FZip;
import deng.fzip.FZipEvent;
import deng.fzip.FZipFile;
import flash.events.Event;
import flash.utils.ByteArray;
import starling.assets.DataLoader;
import starling.utils.execute;
public class ZipLoader extends DataLoader
{
override public function load(url:String, onComplete:Function,
onError:Function, onProgress:Function = null):void
{
var baseUrl:String = url.split("?")[0];
if (baseUrl.toLowerCase().lastIndexOf(".zip") == baseUrl.length - 4)
super.load(url, onLoadComplete, onError, onProgress);
else
super.load(url, onComplete, onError, onProgress);
function onLoadComplete(data:ByteArray):void
{
baseUrl = baseUrl.substring(0, baseUrl.length - 4); // remove ".zip"
var fzip:FZip = new FZip();
fzip.addEventListener(Event.COMPLETE, onZipComplete);
fzip.addEventListener(FZipEvent.FILE_LOADED, onZipFileLoaded);
fzip.loadBytes(data);
}
function onZipComplete(event:Event):void
{
onError("No content found in unzipped file " + url);
}
function onZipFileLoaded(event:FZipEvent):void
{
var fzip:FZip = event.target as FZip;
var file:FZipFile = event.file;
var filename:String = file.filename;
var components:Array = filename.split(".");
var extension:String = components.pop();
var basename:String = components.join(".");
if (file.sizeUncompressed > 0 && filename.indexOf("__MACOSX") == -1)
{
execute(onComplete, file.content, null, basename, extension);
fzip.removeEventListener(Event.COMPLETE, onZipComplete);
fzip.removeEventListener(FZipEvent.FILE_LOADED, onZipFileLoaded);
fzip.close();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment