xml文件轉二進制文件的AIR

遊戲開發中,有很多XML可能是很大的,比如一個任務配置文件,可能就接近2M(當然全部文件放在一個XML裏面本身就有問題,比較好的做法就是分等級分隔XML),這樣就需要對XML進行壓縮。之前把文件壓成ZIP包,然後讀取ZIP,但現在讀取ZIP文件裏面的內容,是很卡的,後面改成讀取二進制的XML數據,這樣感覺不會卡,速度也很快。

實現思路也比較簡單,使用二進制讀取文件,然後調用保存。

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
                        width="900" height="350"
                        creationComplete="init()"
                        alwaysInFront="false">
    
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            
            private var filePath:String;
            private var fileBytes:ByteArray;
            
            private function init():void
            {
                var funArr:Array = [];
                
                this.showStatusBar = true;
                this.status = "Copyright©2012 meteoric_cry.net.  Powered by Meteoric_cry";
                
                bindBtnsEvent();
            }
            
            private function showTopHandler():void 
            {
                if (this.stage.nativeWindow.alwaysInFront)
                {
                    this.stage.nativeWindow.alwaysInFront=false;
                    showTopBtn.label="前端顯示";
                }
                else
                {
                    this.stage.nativeWindow.alwaysInFront=true;
                    showTopBtn.label="√前端顯示";
                }
            }
            
            private function bindBtnsEvent():void
            {
                selectBtn.addEventListener(MouseEvent.CLICK, onSelectHandler);
                compressBtn.addEventListener(MouseEvent.CLICK, onCompressHandler);
                saveBtn.addEventListener(MouseEvent.CLICK, onSaveHandler);
            }
            
            private function getTypeFilter():FileFilter
            {
                var str:String = "*.xml;";
                
                var filter:FileFilter = new FileFilter("XML("+str+")", str);
                
                return filter;
            }
            
            private function onSelectHandler(evt:MouseEvent):void
            {
                var file:File = new File();
                file.addEventListener(Event.SELECT, selectFileCallback);
                file.browseForOpen("請選擇一個文件", [getTypeFilter()]);
            }
            
            private function selectFileCallback(evt:Event):void
            {
                clear();
                
                var file:File = File(evt.target);
                file.removeEventListener(Event.SELECT, selectFileCallback);
                
                filePath = file.nativePath;
                
                xmlPathIpt.htmlText = filePath;
            }
            
            private function clear():void
            {
                xmlPathIpt.htmlText = "";
                
                filePath = null;
                fileBytes = null;
            }
            
            private function onCompressHandler(evt:MouseEvent):void
            {
                if (filePath)
                {
                    var file:File = new File(filePath);
                    
                    if (file.isDirectory == false && file.exists)
                    {
                        var fs:FileStream = new FileStream();
                        fileBytes = new ByteArray();
                        
                        fs.open(file, FileMode.READ);
                        fs.position = 0;
                        fs.readBytes(fileBytes, 0, fs.bytesAvailable);
                        fs.close();
                        
                        fileBytes.compress();
                        
                        Alert.show('文件壓縮成功', '溫馨提示', Alert.OK);
                    }
                    else
                    {
                        Alert.show(filePath + "不是一個正確的文件路徑", '溫馨提示', Alert.OK);
                    }
                }
                else
                {
                    Alert.show('請選擇要壓縮的文件', '溫馨提示', Alert.OK);
                }
            }
            
            private function onSaveHandler(evt:MouseEvent):void
            {
                if (fileBytes)
                {
                    var fileName:String = new File(filePath).name.replace(/\.xml/, '');
                    
                    new FileReference().save(fileBytes, fileName + ".swf");
                    
                    Alert.show("文件" + fileName + ".swf保存成功", '溫馨提示', Alert.OK);
                }
                else
                {
                    Alert.show('請先選擇文件並進行壓縮', '溫馨提示', Alert.OK);
                }
            }
            
        ]]>
    </mx:Script>
    
    <mx:Button label="前端顯示" right="10" top="3" click="showTopHandler();" id="showTopBtn"/>
    
    <mx:HBox width="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" 
             paddingTop="100" paddingLeft="50" paddingRight="10" paddingBottom="10" 
             horizontalGap="0" verticalAlign="middle">
        
        <mx:Label text="請選擇要進行壓縮的文件:" />
        <mx:TextInput id="xmlPathIpt" width="400" editable="false" paddingLeft="2" paddingRight="2" />
        
        <mx:HBox width="100%" paddingLeft="20">
            <mx:Button id="selectBtn" label="選擇文件" />
            <mx:Button id="compressBtn" label="壓縮文件" />
            <mx:Button id="saveBtn" label="保存文件" />
        </mx:HBox>
        
    </mx:HBox>
    
</mx:WindowedApplication>

點擊此處下載AIR>>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章