在现在开发的游戏中,由于战斗数据比较大,所以尝试对战斗数据进行压缩,然后输出到客户端flash端再解压。

Google到一篇文章,对照翻译工具翻译一下 :)

在我的一些项目中,经常需要对数据做一些转换操作,所以积累一些很有意思的用于数据压缩/解压缩的第三方类库。

当然ByteArray类本身就带了数据压缩和解压缩的方法,可以用在flash player使用zlib算法和AIR程序使用多种算法。在FLASH跟PHP作为后台的编程中,我后来选择了ByteArray的compress方法来做zlib算法的压缩,用这个方法用的比较顺手,而且很快。

下面是一些第三方的类库地址以及介绍: 

如果你还知道更多的类库或者其他好东东,欢迎告知!

 


翻译自:​​http:///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:

If you know some more, please let me know.




===============================================================================



 



附一个自己平时经常用的类库ZipArchive,以下是主页及帮助文档:



​http:///p/as3-ziparchive/​



​http://www.riaidea.com/as3/zip/asdoc/riaidea/utils/zip/ZipArchive.html​



​ZipArchive​​是一个Zip档案处理类,可读写各种zip格式文件。如zip/swc/air/docx/xlsx等。






Features

  • 轻松创建或加载一个zip档案;
  • 多种方式读取和删除zip档案中的文件;
  • 支持中文文件名;
  • 非常容易序列化一个zip档案,如有AIR、PHP等的支持下就可以把生成的zip档案保存在本地或服务器上。



Usage

package  
{
import flash.display.*;
import flash.events.*;
import flash.utils.*;
import com.riaidea.utils.zip.*;


[SWF(width =500, height =400, frameRate =24, backgroundColor =0xFFFFFF)]
publicclassExampleextendsSprite
{
privatevar zip:ZipArchive;
privatevar swc:ZipArchive;

publicfunctionExample()
{
testZip();
testSWC();
}

privatefunction testZip():void
{
zip =newZipArchive();
handleEvents(zip,true);
zip.load("assets/test.zip");
}

privatefunction processZip():void
{
//显示zip文件的详细文件信息
trace(zip.toComplexString());

//读取zip中的xml文件
var xmlFile:ZipFile= zip.getFileByName("sample.xml");
var xml:XML =new XML(xmlFile.data);
trace(xml);

//复制zip中的"girl.jpg"为"张曼玉.jpg"
var zmy:ZipFile= zip.getFileByName("girl.jpg");
zip.addFileFromBytes("张曼玉.jpg", zmy.data);

//异步加载并显示zip中的新生成的图片"张曼玉.jpg"
zip.getAsyncDisplayObject("张曼玉.jpg",
function(img:DisplayObject):void
{
addChild(img);
img.x =10;
img.y =10;
});

//删除zip中的文件"girl.jpg"
zip.removeFileByName("girl.jpg");

//异步加载并显示zip中的SWF文件"loading.swf"
zip.getAsyncDisplayObject("loading.swf",
function(swf:DisplayObject):void
{
addChild(swf);
swf.x =150;
swf.y =10;
});

//根据字符串内容创建一个新的txt文件
var txtContent:String="这是一个测试文本文件";
zip.addFileFromString("empty_dir/test.txt", txtContent);

//显示修改后的zip文件信息
trace(zip.toComplexString());
}

privatefunction testSWC():void
{
swc =newZipArchive();
handleEvents(swc,true);
swc.load("assets/puremvc.swc");
}

privatefunction processSWC():void
{
//显示swc文件的详细文件信息
trace(swc.toComplexString());

//读取swc文件中的所有类定义
var catalog:ZipFile= swc.getFileByName("catalog.xml");
var catalogXML:XML = XML(catalog.data);
trace(catalogXML..(catalogXML.namespace())::def);
}

privatefunction handleEvents(zip:ZipArchive, add:Boolean):void
{
if(add)
{
zip.addEventListener(ZipEvent.PROGRESS, onProgress);
zip.addEventListener(ZipEvent.LOADED, onLoaded);
zip.addEventListener(ZipEvent.INIT, onInit);
zip.addEventListener(ZipEvent.ERROR, onError);
}else
{
zip.removeEventListener(ZipEvent.PROGRESS, onProgress);
zip.removeEventListener(ZipEvent.LOADED, onLoaded);
zip.removeEventListener(ZipEvent.INIT, onInit);
zip.removeEventListener(ZipEvent.ERROR, onError);
}
}

privatefunction onInit(evt:ZipEvent):void
{
handleEvents(evt.target asZipArchive,false);
switch(evt.target)
{
case zip: processZip();break;
case swc: processSWC();break;
}
}

privatefunction onProgress(evt:ZipEvent):void
{
//trace(evt.message.bytesLoaded, evt.message.bytesTotal);
}

privatefunction onLoaded(evt:ZipEvent):void
{
//trace(evt);
}

privatefunction onError(evt:ZipEvent):void
{
//trace(evt);
}
}
}