※現在、「wonderfl build flash online」求人コンテンツ制作に関してのアンケートを実施中です!みなさまのお力添えを頂いて、続々とアンケート結果が集まっていますが、まだまだ募集しております。ご協力のほど、どうぞよろしくお願いいたします!

wonderfl運営事務局
→アンケートページ(※ログインしてからお答えいただけるようになっています。)

 notice: Flash editor updated! Join the development! Thanks to MiniBuilder


forked from : hikipuro's 2009-3-14 選んだファイルを Base64 エンコードする [diff(5)]

FORKED

選んだファイルを圧縮してから Base64 エンコードする ByteArray.compress()とByteArray.deflate()のサイズ差を検証 [diff(103)]

  1. // forked from nemu90kWw's 選んだファイルを圧縮してから Base64 エンコードする
  2. // forked from hikipuro's 2009-3-14 選んだファイルを Base64 エンコードする
  3. package 
  4. {
  5.     import flash.display.Loader;
  6.     import flash.display.SimpleButton;
  7.     import flash.display.Sprite;
  8.     import flash.events.Event;
  9.     import flash.events.MouseEvent;
  10.     import flash.net.FileFilter;
  11.     import flash.net.FileReference;
  12.     import flash.text.TextField;
  13.     import flash.text.TextFieldAutoSize;
  14.     import flash.utils.ByteArray;
  15.     import mx.utils.Base64Encoder;
  16.     import flash.system.System;
  17.     
  18.     public class Main extends Sprite 
  19.     {
  20.         private var fileReference:FileReference;
  21.         private var textField:TextField;
  22.         private var button1:SimpleButton;
  23.         private var loader:Loader;
  24.         private var bytes_source:ByteArray;
  25.         private var bytes_zlib:ByteArray;
  26.         private var bytes_deflate:ByteArray;
  27.         
  28.         /**
  29.          * コンストラクタ
  30.          */
  31.         public function Main():void 
  32.         {
  33.             if (stage) init();
  34.             else addEventListener(Event.ADDED_TO_STAGE, init);
  35.         }
  36.         
  37.         /**
  38.          * 初期化メソッド
  39.          * @param    e
  40.          */
  41.         private function init(e:Event = null):void 
  42.         {
  43.             removeEventListener(Event.ADDED_TO_STAGE, init);
  44.             // entry point
  45.             
  46.             // ファイルの選択ボタンの作成
  47.             button1 = new SimpleButton();
  48.             button1.upState = makeButton(0xDDDDDD, 1002010"ファイルの選択");
  49.             button1.overState = makeButton(0xEEEEEE, 1002010"ファイルの選択");
  50.             button1.downState = makeButton(0xCCCCCC, 1002010"ファイルの選択");
  51.             button1.hitTestState = button1.upState;
  52.             button1.addEventListener(MouseEvent.MOUSE_DOWN, onSelectButtonDown);
  53.             button1.x = 5;
  54.             button1.y = 5;
  55.             addChild(button1);
  56.             
  57.             // テキストフィールドの準備
  58.             textField = new TextField();
  59.             textField.selectable = false;
  60.             textField.autoSize = TextFieldAutoSize.LEFT;
  61.             textField.x = 0;
  62.             textField.y = 30;
  63.             textField.text = "";
  64.             addChild(textField);
  65.             
  66.             // イベントの登録
  67.             fileReference = new FileReference();
  68.             fileReference.addEventListener(Event.SELECT, onSelect);
  69.             fileReference.addEventListener(Event.COMPLETE, onComplete);
  70.             
  71.             // ローダーの準備
  72.             loader = new Loader();
  73.             loader.y = 80;
  74.             addChild(loader);
  75.         }
  76.         
  77.         /**
  78.          * ファイルの選択ボタンが押された時
  79.          * @param    event
  80.          */
  81.         private function onSelectButtonDown(event:MouseEvent):void 
  82.         {
  83.             // ファイル選択ダイアログを表示する
  84.             fileReference.browse();
  85.         }
  86.         
  87.         /**
  88.          * ファイル選択イベント
  89.          * @param    event
  90.          */
  91.         private function onSelect(event:Event):void
  92.         {
  93.             fileReference.load();
  94.         }
  95.         
  96.         /**
  97.          * 読み込み完了イベント
  98.          * @param    event
  99.          */
  100.         private function onComplete(event:Event):void
  101.         {
  102.             bytes_source = fileReference.data;
  103.             
  104.             loader.loadBytes(fileReference.data);
  105.             loader.scaleX = 0.5;
  106.             loader.scaleY = 0.5;
  107.             
  108.             bytes_source.position = 0;
  109.             bytes_zlib = new ByteArray();
  110.             bytes_source.readBytes(bytes_zlib);
  111.             bytes_zlib.compress();
  112.             bytes_source.position = 0;
  113.             bytes_deflate = new ByteArray();
  114.             bytes_source.readBytes(bytes_deflate);
  115.             bytes_deflate.deflate();
  116.             
  117.             textField.text = "source : "+bytes_source.length+"\ncompress : "+bytes_zlib.length+"\ndeflate : "+bytes_deflate.length;
  118.         }
  119.         
  120.         /**
  121.          * ボタンを作って返す
  122.          * @param    color    色
  123.          * @param    width    幅
  124.          * @param    height    高さ
  125.          * @param    round    角丸の大きさ
  126.          * @param    text    ボタンのテキスト
  127.          * @return    ボタン
  128.          */
  129.         private function makeButton(color:uint, width:int, height:int, round:int, text:String):Sprite
  130.         {
  131.             var t:TextField = new TextField();
  132.             var s:Sprite = new Sprite();
  133.             s.graphics.lineStyle(2);
  134.             s.graphics.beginFill(color);
  135.             s.graphics.drawRoundRect(00, width, height, round);
  136.             s.graphics.endFill();
  137.             
  138.             t.text = text;
  139.             t.selectable = false;
  140.             t.width = width;
  141.             t.autoSize = TextFieldAutoSize.CENTER;
  142.             s.addChild(t);
  143.             
  144.             return s;
  145.         }
  146.     }
  147. }
noswf
Get Adobe Flash Player