首先要整第三方库 , 在上一篇中已经有所介绍 , 这里不讲。
关于 jszip和AS的aszip差不多一样。
这里我写了三个文件 : a.txt , b.xml , c.json 打包到一个 ziptest.zip的压缩包中。
a.txt:
650) this.width=650;" width="319" height="173" title="03.png" style="width:319px;height:173px;" src="https://s5.51cto.com/wyfs02/M00/A6/9B/wKioL1nU1o6jXiUZAAA5ArNnjNM536.png-wh_500x0-wm_3-wmp_4-s_3980068177.png" border="0" vspace="0" hspace="0" alt="wKioL1nU1o6jXiUZAAA5ArNnjNM536.png-wh_50" />
b.xml:
650) this.width=650;" width="418" height="243" title="04.png" style="width:418px;height:243px;" src="https://s1.51cto.com/wyfs02/M02/07/E9/wKiom1nU11ywh2KqAABQAeDTGqg852.png-wh_500x0-wm_3-wmp_4-s_3805887264.png" border="0" vspace="0" hspace="0" alt="wKiom1nU11ywh2KqAABQAeDTGqg852.png-wh_50" />
c.json:
650) this.width=650;" width="482" height="152" title="05.png" style="width:482px;height:152px;" src="https://s4.51cto.com/wyfs02/M02/07/E9/wKiom1nU176iZ7BtAAAswE6QSJk453.png-wh_500x0-wm_3-wmp_4-s_582588947.png" border="0" vspace="0" hspace="0" alt="wKiom1nU176iZ7BtAAAswE6QSJk453.png-wh_50" />
ziptest.zip的位置:
650) this.width=650;" width="223" height="406" title="06.png" style="width:223px;height:406px;" src="https://s1.51cto.com/wyfs02/M02/A6/9B/wKioL1nU1-KDJOhPAABOsiqzFtg514.png-wh_500x0-wm_3-wmp_4-s_2590404118.png" border="0" vspace="0" hspace="0" alt="wKioL1nU1-KDJOhPAABOsiqzFtg514.png-wh_50" />
核心代码 :::
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");
650) this.width=650;" title="02.png" src="https://s1.51cto.com/wyfs02/M00/07/E9/wKiom1nU2M_wP7_mAAChqqkUzx8596.png-wh_500x0-wm_3-wmp_4-s_567546142.png" alt="wKiom1nU2M_wP7_mAAChqqkUzx8596.png-wh_50" />
结果:
650) this.width=650;" title="07.png" src="https://s1.51cto.com/wyfs02/M00/A6/9B/wKioL1nU2LzBn1Z3AAA9prDS8HY712.png-wh_500x0-wm_3-wmp_4-s_2845227796.png" alt="wKioL1nU2LzBn1Z3AAA9prDS8HY712.png-wh_50" />
本文出自 “Better_Power_Wisdom” 博客,请务必保留此出处http://aonaufly.blog.51cto.com/3554853/1970395
Egret之JSZip基础
原文地址:http://aonaufly.blog.51cto.com/3554853/1970395