前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >xml文件转二进制文件的AIR

xml文件转二进制文件的AIR

作者头像
meteoric
发布2018-11-16 17:44:23
1.3K0
发布2018-11-16 17:44:23
举报
文章被收录于专栏:游戏杂谈游戏杂谈

游戏开发中,有很多XML可能是很大的,比如一个任务配置文件,可能就接近2M(当然全部文件放在一个XML里面本身就有问题,比较好的做法就是分等级分隔XML),这样就需要对XML进行压缩。之前把文件压成ZIP包,然后读取ZIP,但现在读取ZIP文件里面的内容,是很卡的,后面改成读取二进制的XML数据,这样感觉不会卡,速度也很快。

实现思路也比较简单,使用二进制读取文件,然后调用保存。

代码语言:javascript
复制
<?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>>

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-11-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档