ASZip文件库是开源的AS3版–Zip压缩算法,具体示例应用可见http://code.google.com/p/aszip/。目前的最新版本是0.2版。最近在项目中需要用到该第三方类库来支持Flash对图片文件的批量打包上传。由于是外国友人写的,所以对中文命名的图片文件进行压缩时,就会报错,只能支持用非中文的命名的图片文件。

 
下面是我在作者原有代码的基础上做了些改进,使其能良好的支持中英文命名的文件,进而对文件打包压缩。
   
   
   
   
  1. /** 
  2. * This class lets you generate zip files in AS3 
  3. * AS3 implementation of the following PHP script : 
  4. * http://www.zend.com/zend/spotlight/creating-zip-files1.php?article=creating-zip-files1&kind=sl&id=274&open=1&anc=0&view=1 
  5. * @author Thibault Imbert (bytearray.org) 
  6. * @usage Compression methods for the files : 
  7. * CompressionMethod.NONE = no compression is applied 
  8. * CompressionMethod.GZIP = native GZIP compression is applied 
  9. first parameter : compression method 
  10. * var myZip:ASZIP = new ASZIP ( CompressionMethod.GZIP ); 
  11. * @version 0.1 First release 
  12. * @version 0.2 ASZip.saveZIP method added 
  13. */ 
  14.   
  15. package org.aszip.zip 
  16.   
  17. import flash.accessibility.Accessibility; 
  18. import flash.utils.ByteArray; 
  19. import flash.utils.Endian; 
  20. import flash.net.URLRequest; 
  21. import flash.net.URLRequestHeader; 
  22. import flash.net.URLRequestMethod; 
  23. import flash.net.navigateToURL; 
  24. import org.aszip.saving.Method; 
  25. import org.aszip.compression.CompressionMethod; 
  26. import org.aszip.crc.CRC32; 
  27.   
  28. /** 
  29. * The ASZip class represents a Zip file 
  30. */ 
  31. public class ASZip 
  32.   
  33. /** 
  34. * The compressed data buffer 
  35. */ 
  36. private var compressedData:ByteArray; 
  37. /** 
  38. * The central directory 
  39. */ 
  40. private var centralDirectory:ByteArray; 
  41. /** 
  42. * The central index 
  43. */ 
  44. private var oldOffset:Number 
  45. /** 
  46. * Number of directories in the zip 
  47. */ 
  48. private var nbDirectory:Array; 
  49. /** 
  50. * The final zip stream 
  51. */ 
  52. private var output:ByteArray; 
  53. /** 
  54. * The compression method used 
  55. */ 
  56. private var compressionMethod:String; 
  57. /** 
  58. * The comment string 
  59. */ 
  60. private var comment:String; 
  61.   
  62. /** 
  63. * Lets you create a Zip file 
  64. * @param pCompression Compression method 
  65. * @example 
  66. * This example shows how to create a valid ZIP file : 
  67.  
  68.  
  69. * var myZip:ASZip = new ASZip ( CompressionMethod.GZIP ); 
 
  •  
  • */ 
  • public function ASZip ( pCompression:String='GZIP' ) 
  •   
  • compressedData = new ByteArray; 
  • centralDirectory = new ByteArray; 
  • output = new ByteArray; 
  • nbDirectory = new Array 
  • comment = new String;; 
  • oldOffset = 0; 
  • compressionMethod = pCompression; 
  •   
  • /** 
  • * Lets you create a directory for the current Zip 
  • * @param directoryName Name of the directory 
  • * @example 
  • * This example shows how to create a directory and subdirectory : 
  •  
  •  
  • * myZip.addDirectory ( "p_w_picpaths" ); 
  • * myZip.addDirectory ( "p_w_picpaths/funk" ); 
  • *