原创

egret加入第三方库zlib

github找一个库,地址如下:https://github.com/imaya/zlib.js


1、创建第三方库项目:
egret create_lib zlib

2、新建bin目录,copy zlib.min.js到bin下

3、编写zlib.min.d.ts
内容:
declare namespace Zlib {
interface Inflate {
constructor(data:any);
decompress(): any;
}
interface Deflate {
constructor(data:any);
compress():any;
}
}

4、将zlib项目剪切到项目同级目录

5、在egretProperties.json里面加入:
{
"name": "zlib",
"path": "./../zlib"
}

6、打开index.html页面嵌入js文件
<script type="text/javascript" src="/libs/modules/zlib/zlib.min.js"></script>

7、编写工具类ByteUtil.ts
class ByteUtil {
public constructor() {
}

/* 压缩 二进制数据
* @param bytes
* @returns Array.<number> or Uint8Array
*/
public static compress(bytes: egret.ByteArray): any {
let byte8 = new Uint8Array(bytes.buffer);
let defate = new Zlib.Deflate(byte8);
let compressed = defate.compress();
return compressed;
}

/**
* 解压 二进制数据
* @param compressed Array.<number> or Uint8Array
*/
public static uncompress(compressed: Uint8Array): egret.ByteArray {
let inflate = new Zlib.Inflate(compressed);
let plain = inflate.decompress();
let plainByte: egret.ByteArray = new egret.ByteArray(plain);
return plainByte;
}

/**
* 解压 返回文本数据
* @param compressed Array.<number> or Uint8Array
*/
public static uncompressTxt(compressed: any): string {
let data = null;
if (compressed instanceof ArrayBuffer) {
data = new Uint8Array(compressed);
} else if (compressed instanceof Uint8Array) {
data = compressed;
}
if (data != null) {
let plainByte: egret.ByteArray = ByteUtil.uncompress(data);
plainByte.position = 0;
return plainByte.readUTFBytes(plainByte.length);
} else {
return null;
}
}
}
正文到此结束
该篇文章的评论功能已被站长关闭