创办与读取ZIP/RAR压缩文件
创建与读取ZIP/RAR压缩文件使用AS3创建与读取ZIP文件,最早也是最流行的是Fzip开源包了,但Fzip没有提供生成
创建与读取ZIP/RAR压缩文件
使用AS3创建与读取ZIP文件,最早也是最流行的是Fzip开源包了,但Fzip没有提供生成ByteArray二进制数据的方法,在AIR中创建压缩包并成了问题,不过这个开源类用在基于WEB的服务器上通过插件方式还是不错。
一些Fzip资料
官方站:http://codeazur.com.br/lab/
后来,发现有个国内的开源包,ZipArchive基于AIR的包可以使用output()方法输出成ByteArray数据,再使用AIR中FileStream的writeBytes方法就能写入成ZIP或RAR文件了。
http://www.riaidea.com/blog/archives/34.html
?
ZipArchive组件部分API如下(来源于官方站):
?
1、constructor [ZipArchive构造函数,创建一个新的zip档案]
2、load [加载一个外部zip档案,如.zip/.air/.docx/.xlsx等采用zlib压缩的档案]
3、open [打开一个二进制流的zip档案]
4、output [把ZipArchive档案实例输出二进制的zip文件,用来生成zip文件]
5、getFileByName [根据文件名获取zip档案中的某个文件]
6、getFileAt [根据文件位置序号获取zip档案中的某个文件]
7、getBitmapByName [根据文件名获取zip档案中的某个图片文件的Bitmap]
8、removeFileByName [根据文件名删除zip档案中的某个文件]
9、removeFileAt [根据文件位置序号删除zip档案中的某个文件]
10、addFile [添加文件到zip档案]
11、addFileFromBytes [从二进制数据添加文件到zip档案]
12、addFileFromString [根据指定的字符串内容添加文件到zip档案,比如.txt/.xml文件]
?
例子:
?
创建ZIP文件
- private?var?zip:ZipArchive?=?new?ZipArchive(); ??
- zip.addFileFromBytes("test.txt",www.shch8.com,0);//将字符保存到记事本并加入压缩包
- var?myFile:File?=?File.documentsDirectory; ??
- myFile?=?myFile.resolvePath("D://bag.zip"); ??
- var?myFileStream:FileStream?=?new?FileStream(); ??
- myFileStream.open(myFile,?FileMode.WRITE); ??
- myFileStream.writeBytes(zip.output())??
读取ZIP文件(例子来源于官方)
- package?{ ??
- ???? ??
- ????import?flash.display.Bitmap; ??
- ????import?flash.display.Sprite; ??
- ????import?flash.events.ProgressEvent; ??
- ????import?flash.utils.ByteArray; ??
- ????import?flash.events.Event; ??
- ????import?flash.events.IOErrorEvent; ??
- ????import?riaidea.utils.zip.ZipArchive; ??
- ????import?riaidea.utils.zip.ZipEvent; ??
- ????import?riaidea.utils.zip.ZipFile; ??
- ??
- ????public?class?Example?extends?Sprite?{ ??
- ???????? ??
- ????????private?var?zip1:ZipArchive?=?new?ZipArchive(); ??
- ???????? ??
- ????????public?function?Example()?{ ??
- ????????????//加载一个zip档案????????????zip1.load("test.zip"); ??
- ????????????zip1.addEventListener(ProgressEvent.PROGRESS,?loading); ??
- ????????????zip1.addEventListener(ZipEvent.ZIP_INIT,?inited); ??
- ????????????zip1.addEventListener(ZipEvent.ZIP_FAILED,?failed); ??
- ????????????zip1.addEventListener(IOErrorEvent.IO_ERROR,?ioError); ??
- ???????????? ??
- ????????} ??
- ????????private?function?inited(evt:ZipEvent):void?{ ??
- ????????????zip1.removeEventListener(ProgressEvent.PROGRESS,?loading); ??
- ????????????zip1.removeEventListener(ZipEvent.ZIP_INIT,?inited); ??
- ????????????zip1.removeEventListener(ZipEvent.ZIP_FAILED,?failed); ??
- ????????????//添加ZIP_CONTENT_LOADED事件侦听器????????????zip1.addEventListener(ZipEvent.ZIP_CONTENT_LOADED,?imgloaded); ??
- ????????????trace("原始zip文件内容\r",?zip1); ??
- ????????????//读取zip1中的xml文件var?xmlFile:ZipFile?=?zip1.getFileByName("sample.xml"); ??
- ????????????var?xml:XML?=?new?XML(xmlFile.data); ??
- ????????????trace(xml); ??
- ????????????//根据字符串内容创建一个新的txt文件var?txtContent:String?=?"这是一个测试文本文件"; ??
- ????????????zip1.addFileFromString("测试.txt",?txtContent); ??
- ????????????//trace(zip1.getFileByName("测试.txt").data);????????????//复制zip1中的"girl.jpg"为"张曼玉.jpg"var?zmy:ZipFile?=?zip1.getFileByName("girl.jpg"); ??
- ????????????zip1.addFileFromBytes("张曼玉.jpg",?zmy.data); ??
- ????????????//加载zip1中的新生成的图片文件的Bitmap对象????????????zip1.getBitmapByName("张曼玉.jpg"); ??
- ????????????//删除图片文件logo.gif????????????zip1.removeFileByName("logo.gif"); ??
- ????????????trace("\r修改后的zip文件内容\r",?zip1); ??
- ????????} ??
- ????????private?function?imgloaded(evt:ZipEvent):void?{ ??
- ????????????zip1.removeEventListener(ZipEvent.ZIP_CONTENT_LOADED,?imgloaded); ??
- ????????????var?img:Bitmap?=?evt.content?as?Bitmap; ??
- ????????????addChild(img); ??
- ????????} ??
- ????????private?function?loading(evt:ProgressEvent):void?{ ??
- ????????????//trace(evt.currentTarget,?evt.bytesLoaded,?evt.bytesTotal);????????} ??
- ????????private?function?failed(evt:ZipEvent):void?{ ??
- ????????????//trace(evt.content);????????private?function?ioError(evt:IOErrorEvent):void?{ ??
- ????????????//trace(evt);????????} ??
- ????} ??
- } ??
?
?