读书人

创办与读取ZIP/RAR压缩文件

发布时间: 2012-12-25 16:18:28 作者: rapoo

创建与读取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文件
  1. private?var?zip:ZipArchive?=?new?ZipArchive(); ??
  2. zip.addFileFromBytes("test.txt",www.shch8.com,0);//将字符保存到记事本并加入压缩包
  3. var?myFile:File?=?File.documentsDirectory; ??
  4. myFile?=?myFile.resolvePath("D://bag.zip"); ??
  5. var?myFileStream:FileStream?=?new?FileStream(); ??
  6. myFileStream.open(myFile,?FileMode.WRITE); ??
  7. myFileStream.writeBytes(zip.output())??
读取ZIP文件(例子来源于官方)
  1. package?{ ??
  2. ???? ??
  3. ????import?flash.display.Bitmap; ??
  4. ????import?flash.display.Sprite; ??
  5. ????import?flash.events.ProgressEvent; ??
  6. ????import?flash.utils.ByteArray; ??
  7. ????import?flash.events.Event; ??
  8. ????import?flash.events.IOErrorEvent; ??
  9. ????import?riaidea.utils.zip.ZipArchive; ??
  10. ????import?riaidea.utils.zip.ZipEvent; ??
  11. ????import?riaidea.utils.zip.ZipFile; ??
  12. ??
  13. ????public?class?Example?extends?Sprite?{ ??
  14. ???????? ??
  15. ????????private?var?zip1:ZipArchive?=?new?ZipArchive(); ??
  16. ???????? ??
  17. ????????public?function?Example()?{ ??
  18. ????????????//加载一个zip档案????????????zip1.load("test.zip"); ??
  19. ????????????zip1.addEventListener(ProgressEvent.PROGRESS,?loading); ??
  20. ????????????zip1.addEventListener(ZipEvent.ZIP_INIT,?inited); ??
  21. ????????????zip1.addEventListener(ZipEvent.ZIP_FAILED,?failed); ??
  22. ????????????zip1.addEventListener(IOErrorEvent.IO_ERROR,?ioError); ??
  23. ???????????? ??
  24. ????????} ??
  25. ????????private?function?inited(evt:ZipEvent):void?{ ??
  26. ????????????zip1.removeEventListener(ProgressEvent.PROGRESS,?loading); ??
  27. ????????????zip1.removeEventListener(ZipEvent.ZIP_INIT,?inited); ??
  28. ????????????zip1.removeEventListener(ZipEvent.ZIP_FAILED,?failed); ??
  29. ????????????//添加ZIP_CONTENT_LOADED事件侦听器????????????zip1.addEventListener(ZipEvent.ZIP_CONTENT_LOADED,?imgloaded); ??
  30. ????????????trace("原始zip文件内容\r",?zip1); ??
  31. ????????????//读取zip1中的xml文件var?xmlFile:ZipFile?=?zip1.getFileByName("sample.xml"); ??
  32. ????????????var?xml:XML?=?new?XML(xmlFile.data); ??
  33. ????????????trace(xml); ??
  34. ????????????//根据字符串内容创建一个新的txt文件var?txtContent:String?=?"这是一个测试文本文件"; ??
  35. ????????????zip1.addFileFromString("测试.txt",?txtContent); ??
  36. ????????????//trace(zip1.getFileByName("测试.txt").data);????????????//复制zip1中的"girl.jpg"为"张曼玉.jpg"var?zmy:ZipFile?=?zip1.getFileByName("girl.jpg"); ??
  37. ????????????zip1.addFileFromBytes("张曼玉.jpg",?zmy.data); ??
  38. ????????????//加载zip1中的新生成的图片文件的Bitmap对象????????????zip1.getBitmapByName("张曼玉.jpg"); ??
  39. ????????????//删除图片文件logo.gif????????????zip1.removeFileByName("logo.gif"); ??
  40. ????????????trace("\r修改后的zip文件内容\r",?zip1); ??
  41. ????????} ??
  42. ????????private?function?imgloaded(evt:ZipEvent):void?{ ??
  43. ????????????zip1.removeEventListener(ZipEvent.ZIP_CONTENT_LOADED,?imgloaded); ??
  44. ????????????var?img:Bitmap?=?evt.content?as?Bitmap; ??
  45. ????????????addChild(img); ??
  46. ????????} ??
  47. ????????private?function?loading(evt:ProgressEvent):void?{ ??
  48. ????????????//trace(evt.currentTarget,?evt.bytesLoaded,?evt.bytesTotal);????????} ??
  49. ????????private?function?failed(evt:ZipEvent):void?{ ??
  50. ????????????//trace(evt.content);????????private?function?ioError(evt:IOErrorEvent):void?{ ??
  51. ????????????//trace(evt);????????} ??
  52. ????} ??
  53. } ??

?

?

读书人网 >编程

热点推荐