In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Unzip

Get Adobe Flash player
by kimo0517 28 Jul 2011
    Embed
/**
 * Copyright kimo0517 ( http://wonderfl.net/user/kimo0517 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/oqq7
 */

package 
{
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;
    import flash.net.FileReference;
    import flash.net.FileFilter;
    import flash.text.TextField;
    import flash.utils.ByteArray;
    import flash.utils.Dictionary;
    
    import nochump.util.zip.ZipEntry;
    import nochump.util.zip.ZipFile;
    import nochump.util.zip.ZipError;
    
    import com.bit101.components.PushButton;
    import com.bit101.components.VScrollBar;
    
    [SWF(width=468, height=468, backgroundColor=0xFFFFFF)]
    public class FlashUnZip extends Sprite 
    {
        private var loadingSprite:Sprite;
        
        private var fr:FileReference;
        private var loader:Loader;
        
        private var namesOnScreen:Vector.<TextField>;
        private var saveButtons:Vector.<PushButton>;
        
        private var btnOpenZip:PushButton;
        
        private var zipFile:ZipFile;
        private var warningTF:TextField;
        
        private var saveBtnToIndex:Dictionary;
        
        private var scrollPlane:Sprite;
        private var scrollBar:VScrollBar;
        
        public function FlashUnZip():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            loadingSprite = new Sprite;
            loadingSprite.graphics.beginFill(0x000000, 0.6);
            loadingSprite.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
            loadingSprite.graphics.endFill();
            
            var nowLoadingTF:TextField = new TextField;
            nowLoadingTF.autoSize = "left";
            nowLoadingTF.multiline = false;
            nowLoadingTF.text = "Processing...";
            nowLoadingTF.x = stage.stageWidth / 2 - nowLoadingTF.width / 2;
            nowLoadingTF.y = stage.stageHeight / 2 - nowLoadingTF.height / 2;
            loadingSprite.addChild(nowLoadingTF);
            loadingSprite.mouseChildren = false;
            
            btnOpenZip = new PushButton(this, 5, 5, "Open Zip File", _btnOpenZipClicked);
            warningTF = new TextField;
            warningTF.autoSize = "left";
            warningTF.textColor = 0xFF0000;
            warningTF.y = 5;
            warningTF.x = btnOpenZip.x + btnOpenZip.width + 5;
            addChild(warningTF);
            
            namesOnScreen = new Vector.<TextField>();
            saveButtons = new Vector.<PushButton>();
            saveBtnToIndex = new Dictionary;
            
            graphics.lineStyle(1, 0x999999);
            graphics.moveTo(0, btnOpenZip.y + btnOpenZip.height + 5);
            graphics.lineTo(stage.stageWidth, btnOpenZip.y + btnOpenZip.height + 5);
            
            scrollPlane = new Sprite;
            scrollPlane.y = btnOpenZip.y + btnOpenZip.height + 10;
            scrollPlane.scrollRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight - scrollPlane.y);
            addChild(scrollPlane);
            
            scrollBar = new VScrollBar(this, 0, scrollPlane.y, _scrollBarChanged);
            scrollBar.x = stage.stageWidth - scrollBar.width;
            scrollBar.height = scrollPlane.scrollRect.height;
            scrollBar.visible = false;
        }
        
        private function _btnOpenZipClicked(e:MouseEvent):void {
            fr = new FileReference();
            fr.addEventListener(Event.SELECT, _frStartLoad);
            fr.addEventListener(Event.COMPLETE, _frLoadComplete);
            var filter:FileFilter = new FileFilter("Zip files", "*.zip");
            fr.browse([filter]);
        }
        
        private function _frStartLoad(e:Event):void {
            mouseChildren = false;
            addChild(loadingSprite);
            fr.load();
        }
        
        private function _frLoadComplete(e:Event):void {
            var i:int;
            mouseChildren = true;
            if (loadingSprite.parent) {
                removeChild(loadingSprite);
            }
            
            try {
                zipFile = new ZipFile(fr.data);
            } catch (e:ZipError) {
                warningTF.text = "File loaded is not zip file!";
                return;
            }
            
            warningTF.text = "";
            
            var zipEntry:ZipEntry;
            var newSaveButton:PushButton;
            var newTF:TextField;
            
            for (i = 0; i < saveButtons.length; i++) {
                delete saveBtnToIndex[saveButtons[i]];
                scrollPlane.removeChild(saveButtons[i]);
                scrollPlane.removeChild(namesOnScreen[i]);
            }
            saveButtons.splice(0, saveButtons.length);
            namesOnScreen.splice(0, namesOnScreen.length);
            
            saveBtnToIndex
            
            for (i = 0; i < zipFile.entries.length; i++) {
                zipEntry = zipFile.entries[i];
                if (zipEntry.isDirectory()) {
                    continue;
                }
                
                
                newSaveButton = new PushButton(scrollPlane, 5, 0, "Save", _saveClicked);
                saveButtons.push(newSaveButton);
                
                saveBtnToIndex[newSaveButton] = i;
                
                newTF = new TextField;
                newTF.autoSize = "left";
                newTF.text = zipEntry.name;
                newTF.x = newSaveButton.x + newSaveButton.width + 5;
                scrollPlane.addChild(newTF);
                namesOnScreen.push(newTF);
            }
            
            _updateScreen();
        }
        
        private function _saveClicked(e:MouseEvent):void {
            var index:int = saveBtnToIndex[e.currentTarget];
            var fileName:String;
            var lastIndexOfFolder:int;
            mouseChildren = false;
            addChild(loadingSprite);
            
            fr = new FileReference;
            fr.addEventListener(Event.COMPLETE, _frSaveComplete);
            fr.addEventListener(Event.CANCEL, _frSaveComplete);
            fileName = (zipFile.entries[index] as ZipEntry).name;
            lastIndexOfFolder = fileName.lastIndexOf("/");
            if (lastIndexOfFolder != -1) {
                fileName = fileName.substr(lastIndexOfFolder+1);
            }
            trace(lastIndexOfFolder, index, fileName);
            
            fr.save(zipFile.getInput(zipFile.entries[index]), fileName);
        }
        
        private function _updateScreen():void {
            var currentY:Number = 0;
            trace(currentY, saveButtons.length);
            for (var i:int = 0; i < saveButtons.length; i++) {
                saveButtons[i].y = currentY;
                namesOnScreen[i].y = currentY;
                currentY += saveButtons[i].height + 5;
            }
            
            scrollPlane.scrollRect = new Rectangle(0,0,scrollPlane.scrollRect.width, scrollPlane.scrollRect.height);
            if (currentY < scrollPlane.scrollRect.height) {
                scrollBar.visible = false;
                stage.removeEventListener(MouseEvent.MOUSE_WHEEL, _mouseWheel);
            } else {
                scrollBar.visible = true;
                scrollBar.setThumbPercent(scrollPlane.scrollRect.height / currentY);
                scrollBar.setSliderParams(0, (currentY - scrollPlane.scrollRect.height)/10, 0);
                stage.addEventListener(MouseEvent.MOUSE_WHEEL, _mouseWheel);
            }
        }
        
        private function _frSaveComplete(e:Event):void {
            mouseChildren = true;
            if (loadingSprite.parent) {
                removeChild(loadingSprite);
            }
        }
        
        private function _scrollBarChanged(e:Event):void {
            scrollPlane.scrollRect = new Rectangle(0,scrollBar.value * 10+5,scrollPlane.scrollRect.width, scrollPlane.scrollRect.height);
        }
        
        private function _mouseWheel(e:MouseEvent):void {
            scrollBar.value -= e.delta;
            _scrollBarChanged(null);
        }
    }
    
}