Zipファイルのプレビュー2 – wonderfl build flash online
▼ActionScript AS3(FP10)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
/* * 使いやすいように整理してみた。 * * * */ package { import flash.display.Sprite; import flash.events.Event; import nochump.util.zip.*; import flash.display.Bitmap; public class Main2 extends Sprite { private var _zipLoader:ZipLoader; public function Main2() { var urlStr:String = (this.loaderInfo.url); if (urlStr.substr(0, String("http://swf.wonderfl.net/").length) == "http://swf.wonderfl.net/") { //wonderfl用 urlStr = "http://www.mztm.jp/wonderfl/"; }else { //wordpressだとフルパスじゃないとダメみたいなので urlStr = urlStr.substr(0, urlStr.lastIndexOf("/")) + "/"; } init(urlStr + "images.zip"); } private function init(url:String):void { _zipLoader = new ZipLoader(); _zipLoader.add(url); _zipLoader.addEventListener(Event.COMPLETE, zipLoadComp); _zipLoader.start(); } private function zipLoadComp(event:Event):void { var n:int = _zipLoader.contents.length; for (var i:int = 0; i < n; i++) { if(_zipLoader.contents[i].type == "image"){ var bitmap:Bitmap = _zipLoader.contents[i].getBitmap(); bitmap.x = 50 + 50 * i; bitmap.y = 50 + 50 * i; this.addChild(bitmap); } } } } } import flash.display.Bitmap; import flash.display.Loader; import flash.events.Event; import flash.net.URLLoader; import flash.net.URLRequest; import nochump.util.zip.*; class ZipLoader { private var _listener:Function = function(event:Event):void { }; private var _url:String; private var _imageLoaderCount:int; private var _imageLoadedCount:int; public var contents:Array; public function ZipLoader() { }; public function add(url:String):void { _url = url; } public function start():void { if (_url.length < 5) { return; } var loader_obj:URLLoader = new URLLoader(); loader_obj.addEventListener(Event.COMPLETE, loadComp); loader_obj.dataFormat = "binary"; loader_obj.load(new URLRequest(_url)); } private function loadComp(event:Event):void { contents = []; _imageLoaderCount = 0; _imageLoadedCount = 0; var zipFile:ZipFile = new ZipFile(event.target.data); var n:int = zipFile.entries.length; for(var i:int = 0; i < n; i++) { var entry:ZipEntry = zipFile.entries[i]; contents[i] = new Content(entry); contents[i].type = typeFormName(entry.name); switch (contents[i].type) { case "image": contents[i].loader = new Loader(); contents[i].loader.loadBytes(zipFile.getInput(entry)); contents[i].loader.contentLoaderInfo.addEventListener(Event.COMPLETE, atComp); _imageLoaderCount ++; break; case "text": contents[i].string = zipFile.getInput(entry).toString(); break; default: } } } private function atComp(event:Event):void { _imageLoadedCount ++; if (_imageLoadedCount == _imageLoaderCount) { _listener(event); } } private var EXTENSIONS:Object = { //"swf":["swf"], "movie":["mp4", "flv"],未対応 "image":["jpg", "jpeg", "gif", "png"], "text":["txt", "js", "xml", "php", "asp", "as", "html", "htm", "php", "py", "mxml"] }; private function typeFormName(name:String):String { var str:String = name.substr(name.lastIndexOf(".") + 1).toLowerCase(); var extension:String; for (var p:String in EXTENSIONS) { var n:int = EXTENSIONS[p].length; for (var i:int = 0; i < n; i++) { extension = EXTENSIONS[p][i]; if(extension == str){ str = p; break; } } } return str; } public function addEventListener(type:String,listener:Function):void{ _listener = listener; } } class Content { public var entry:ZipEntry; public var type:String; public var loader:Loader; public var string:String; public function Content(entry:ZipEntry):void { this.entry = entry; } public function getBitmap():Bitmap { return Bitmap(loader.contentLoaderInfo.content); } public function getText():String { return string; } } |