Egret之JSZip基础

本文介绍如何利用JSZip库加载并解析ZIP格式文件中的多种类型资源,包括文本、XML和JSON文件,并演示了具体的代码实现。

首先要整第三方库 , 在上一篇中已经有所介绍 , 这里不讲。

关于 jszip和AS的aszip差不多一样。

这里我写了三个文件 : a.txt , b.xml , c.json 打包到一个 ziptest.zip的压缩包中。

a.txt:

wKioL1nU1o6jXiUZAAA5ArNnjNM536.png-wh_50

b.xml:

wKiom1nU11ywh2KqAABQAeDTGqg852.png-wh_50

c.json:

wKiom1nU176iZ7BtAAAswE6QSJk453.png-wh_50


ziptest.zip的位置:

wKioL1nU1-KDJOhPAABOsiqzFtg514.png-wh_50


核心代码 :::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   private loadZip() : void{
    RES.getResByUrl( "resource/ziptest.zip" function (data):void{
           let zipj : JSZip =  new  JSZip(data);
     this .analysisJSZip( zipj );
    }, this , RES.ResourceItem.TYPE_BIN);
   }
   private analysisJSZip( zipj : JSZip ) : void{
    //a.txt解析
    let txtstr : JSZipObject = zipj.file( "ziptest/a.txt" );
    console.log( 'Txt content is '  + txtstr.asText());
    //b.xml解析
    let xmlstr : JSZipObject = zipj.file( "ziptest/b.xml" );
    let xmlConfig : egret.XML = egret.XML.parse(xmlstr.asText());
    console.log( "the xml root name is : "  + xmlConfig.name);
    let xmlChileren : Array<egret.XMLNode> = xmlConfig.children;
    let xmlChildrenLen : number = xmlChileren.length;
    let xmlCell : egret.XML =  null ;
    for ( let i : number = 0 ; i < xmlChildrenLen ; i ++ ){
     xmlCell = <egret.XML><any>xmlChileren[i];
     if ( xmlCell.name ==  "role"  ){
      let xmlName : egret.XML = <egret.XML><any>xmlCell.children[0]; //获得<name str="Aonaufly"/>
      console.log( "xml role name is : "  + xmlName[ "$str" ]);
      break ;
     }
    }
    //c.json解析
    let jsonstr : JSZipObject = zipj.file( "ziptest/c.json" );
    let jsonc : any = JSON.parse( jsonstr.asText() );
    console.log( "json name is : "  + jsonc.name);
   }

关于 zipj.file("ziptest/a.txt");

wKiom1nU2M_wP7_mAAChqqkUzx8596.png-wh_50


结果:

wKioL1nU2LzBn1Z3AAA9prDS8HY712.png-wh_50

















本文转自Aonaufly51CTO博客,原文链接: http://blog.51cto.com/aonaufly/1970395,如需转载请自行联系原作者



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值