zoukankan      html  css  js  c++  java
  • 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>>

  • 相关阅读:
    python学习之路---day16--面向对象
    python-day15---面向对象
    python---day14( 内置函数二)
    day13--------python 内置函数(一)
    python学习之路---day12
    python学习之路---day09
    python学习之路---day07
    【转帖】嵌入式4412开发板QT5.7编译安装到arm
    【转载】4412开发板嵌入式QtE应用开发环境搭建
    S5P4418开发板介绍
  • 原文地址:https://www.cnblogs.com/meteoric_cry/p/2789766.html
Copyright © 2011-2022 走看看