<?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init()"> <fx:Script> <![CDATA[ import com.coltware.airxzip.*; import com.coltware.airxzip.ZipEntry; import com.coltware.airxzip.ZipError; import com.coltware.airxzip.ZipFileReader; import flash.utils.ByteArray; import flash.utils.setTimeout; import mx.collections.ArrayCollection; import mx.controls.Alert; [Bindable] private var arrColl:ArrayCollection; private var outputPath:String; private var urlVars:URLVariables; private var downloadFile:File; private var urlReq:URLRequest; private const FILE_URL:String = "http://192.168.2.3/david/plugin/oldversion/2.zip"; private var downloadPath:String; private function init():void { downloadFile = new File(); arrColl = new ArrayCollection(); downloadFile = new File(); downloadFile.addEventListener(Event.CANCEL, doEvent); downloadFile.addEventListener(Event.COMPLETE, doEvent); downloadFile.addEventListener(Event.SELECT, doEvent); downloadFile.addEventListener(HTTPStatusEvent.HTTP_STATUS, doEvent); downloadFile.addEventListener(IOErrorEvent.IO_ERROR, doEvent); downloadFile.addEventListener(ProgressEvent.PROGRESS, doEvent); downloadFile.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doEvent); } protected function downloadZip(event:MouseEvent):void { urlReq = new URLRequest(FILE_URL); downloadFile.download(urlReq,"indesign.zip"); } private function doEvent(evt:Event):void { var fl:File = evt.currentTarget as File; //trace(evt.type); switch(evt.type.toLowerCase()){ case "complete": downloadPath = fl.url; uncompress(); break; case "ioerror": Alert.show("网络地址错误"); return; break; } /* Add event order and type to the DataGrid control. */ arrColl.addItem({data:arrColl.length+1, type:evt.type, eventString:evt.toString()}); try { /* Update the Model. */ fileRefModel.creationDate = fl.creationDate; fileRefModel.creator = fl.creator; fileRefModel.modificationDate = fl.modificationDate; fileRefModel.name = fl.name; fileRefModel.path = fl.url; fileRefModel.size = fl.size; fileRefModel.type = fl.type; /* Display the Text control. */ txt.visible = true; } catch (err:*) { /* uh oh, an error of sorts. */ } } private function showAlert(item:Object):void { Alert.show(item.eventString, item.type); } private function uncompress():void { var reader:ZipFileReader = unzip_init(fileRefModel.path); var list:Array = reader.getEntries(); var file:File = new File(); var path:String = fileRefModel.path as String; outputPath = path.substr(0, path.indexOf(fileRefModel.name)) + "indesign/"; for each(var entry:ZipEntry in list){ //创建文件 if(entry.isDirectory()){ file.url = outputPath + entry.getFilename(); if(!file.exists) file.createDirectory(); //trace("DIR --->" + entry.getFilename()); } else{ var bytes:ByteArray = reader.unzip(entry); file.url = outputPath + entry.getFilename(); file.resolvePath(outputPath + entry.getFilename()); var fileStream:FileStream = new FileStream(); fileStream.open(file, FileMode.WRITE); fileStream.writeBytes(bytes); fileStream.close(); } } reader.close();//关闭字节流。否则无法删除zip openIndesign(); setTimeout(delZip,1000); } //删除 zip文件 private function delZip():void { var zipFile:File = new File(fileRefModel.path); zipFile.deleteFile(); } private function openIndesign():void { var file:File = new File(); file.url = outputPath + "2/demo/staticImage.html"; file.openWithDefaultApplication(); } private function unzip_init(filepath:String):ZipFileReader{ var reader:ZipFileReader = new ZipFileReader(); var file:File = new File(filepath); reader.open(file); return reader; } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> <fx:Model id="fileRefModel"> <file> <creationDate>{""}</creationDate> <creator>{""}</creator> <modificationDate>{""}</modificationDate> <name>{""}</name> <path>{""}</path> <size>{""}</size> <type>{""}</type> </file> </fx:Model> </fx:Declarations> <s:VGroup> <s:Button id="downloadBtn" label="download" click="downloadZip(event)" toolTip="预览"/> <mx:DataGrid id="debug" dataProvider="{arrColl}" width="{downloadBtn.width}" rowCount="5" rowHeight="22" itemClick="showAlert(event.currentTarget.selectedItem)"> <mx:columns> <mx:DataGridColumn dataField="data" headerText="#" width="20" /> <mx:DataGridColumn dataField="type" headerText="Type" showDataTips="true" dataTipField="eventString" /> </mx:columns> </mx:DataGrid> <mx:Text id="txt" condenseWhite="true" visible="false"> <mx:text> creationDate: {fileRefModel.creationDate} creator: {fileRefModel.creator} modificationDate: {fileRefModel.modificationDate} name: {fileRefModel.name} path: {fileRefModel.path} size: {fileRefModel.size} type: {fileRefModel.type} </mx:text> </mx:Text> </s:VGroup> </s:WindowedApplication>
[转]
在现在开发的游戏中,由于战斗数据比较大,所以尝试对战斗数据进行压缩,然后输出到客户端flash端再解压。
Google到一篇文章,对照翻译工具翻译一下
在我的一些项目中,经常需要对数据做一些转换操作,所以积累一些很有意思的用于数据压缩/解压缩的第三方类库。
当然ByteArray类本身就带了数据压缩和解压缩的方法,可以用在flash player使用zlib算法和AIR程序使用多种算法。在FLASH跟PHP作为后台的编程中,我后来选择了ByteArray的compress方法来做zlib算法的压缩,用这个方法用的比较顺手,而且很快。
下面是一些第三方的类库地址以及介绍:
AS3 Zip: AS 3 下用来读取和写入zip文件的类库
FZip: FZip 是一个用于AS 3 下读取、创建、修改zip压缩包的类库
ASZip: AS 3 用于创建zip文件的类库
LZMA Encoder: AS3下使用LZMA算法压缩数据的类库.
LZMA Decoder: 跟上面类库对应的用于LZMA算法解压缩数据.
AsCompress: AS3下 GZIP压缩和解压缩类库,好像需要SDK版本在4.x以上,flash cs3下不可用。
Gzip for HTTPService/URLLoader: 给你的 Flex/AIR HTTPService/URLLoader增加gzip支持
airxzip: AIR的zip类库
如果你还知道更多的类库或者其他好东东,欢迎告知!
翻译自:http://blog.yoz.sk/2011/01/quick-tip-compression-in-flash/
While working on one of my projects where I needed compression for transfered data, I hit some very interesting compression libraries. Also the ByteArray class contains compress method, using zlib algorithm in flash player or multiple algorithms in AIR. At the end I decided to useByteArray.compress() method for encoding vs. PHP gzuncompress for decoding, what works correctly, fast and smooth.
Here is a list of 3rd party compression libraries and other good stuff:
AS3 Zip: ActionScript 3 based library for reading and writing zip files
FZip: FZip is an Actionscript 3 class library to load, modify and create standard ZIP archives.
ASZip: ActionScript 3 library to generate ZIP files
LZMA Encoder: AS3 class to compress data using LZMA algorithm.
LZMA Decoder: A part of the apparat framework.
GZIP: ActionScript GZIP compression library
Gzip for HTTPService/URLLoader: Adding Gzip support for Flex/AIR HTTPService/URLLoader
airxzip: Zip library for ActionScript3 on AIR
If you know some more, please let me know.
from: http://www.cnblogs.com/rob0121/articles/2300556.html