ASZip0.2版本解决中文文件名乱码问题
2013-09-09 11:06
543 查看
ASZip文件库是开源的AS3版–Zip压缩算法,具体示例应用可见http://code.google.com/p/aszip/。目前的最新版本是0.2版。最近在项目中需要用到该第三方类库来支持Flash对图片文件的批量打包上传。由于是外国友人写的,所以对中文命名的图片文件进行压缩时,就会报错,只能支持用非中文的命名的图片文件。
下面是我在作者原有代码的基础上做了些改进,使其能良好的支持中英文命名的文件,进而对文件打包压缩。
使用方法是复制以上代码,保存为ASZip.as,然后下载http://code.google.com/p/aszip/downloads/list 解压ASZip 0.2版本后,把保存的ASZip.as直接覆盖到org/aszip/zip 目录下,即可使用。
下面是我在作者原有代码的基础上做了些改进,使其能良好的支持中英文命名的文件,进而对文件打包压缩。
/** * This class lets you generate zip files in AS3 * AS3 implementation of the following PHP script : * http://www.zend.com/zend/spotlight/creating-zip-files1.php?article=creating-zip-files1&kind=sl&id=274&open=1&anc=0&view=1 * * @author Thibault Imbert (bytearray.org) * @usage Compression methods for the files : * * CompressionMethod.NONE = no compression is applied * CompressionMethod.GZIP = native GZIP compression is applied * * first parameter : compression method * * var myZip:ASZIP = new ASZIP ( CompressionMethod.GZIP ); * * @version 0.1 First release * @version 0.2 ASZip.saveZIP method added */ package org.aszip.zip { import flash.accessibility.Accessibility; import flash.utils.ByteArray; import flash.utils.Endian; import flash.net.URLRequest; import flash.net.URLRequestHeader; import flash.net.URLRequestMethod; import flash.net.navigateToURL; import org.aszip.saving.Method; import org.aszip.compression.CompressionMethod; import org.aszip.crc.CRC32; /** * The ASZip class represents a Zip file */ public class ASZip { /** * The compressed data buffer */ private var compressedData:ByteArray; /** * The central directory */ private var centralDirectory:ByteArray; /** * The central index */ private var oldOffset:Number /** * Number of directories in the zip */ private var nbDirectory:Array; /** * The final zip stream */ private var output:ByteArray; /** * The compression method used */ private var compressionMethod:String; /** * The comment string */ private var comment:String; /** * Lets you create a Zip file * * @param pCompression Compression method * @example * This example shows how to create a valid ZIP file : * <div> * <pre> * * var myZip:ASZip = new ASZip ( CompressionMethod.GZIP ); * </pre> * </div> */ 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 : * <div> * <pre> * * myZip.addDirectory ( "images" ); * myZip.addDirectory ( "images/funk" ); * </pre> * </div> */ public function addDirectory ( directoryName:String ):void { directoryName = directoryName.split ('\\').join ('/'); var feedArrayRow:ByteArray = new ByteArray; feedArrayRow.endian = Endian.LITTLE_ENDIAN; feedArrayRow.writeUnsignedInt ( 0x04034b50 ); feedArrayRow.writeShort ( 0x000a ); feedArrayRow.writeShort ( 0x0000 ); feedArrayRow.writeShort ( 0x0000 ); feedArrayRow.writeUnsignedInt ( unixToDos ( new Date().getTime() ) ); feedArrayRow.writeUnsignedInt (0); feedArrayRow.writeUnsignedInt (0); feedArrayRow.writeUnsignedInt (0); feedArrayRow.writeShort ( directoryName.length ); feedArrayRow.writeShort ( 0 ); feedArrayRow.writeUTFBytes ( directoryName ); feedArrayRow.writeUnsignedInt ( 0 ); feedArrayRow.writeUnsignedInt ( 0 ); feedArrayRow.writeUnsignedInt ( 0 ); compressedData.writeBytes ( feedArrayRow ); var newOffset:int = this.compressedData.length; // Directory header var addCentralRecord:ByteArray = new ByteArray; addCentralRecord.endian = Endian.LITTLE_ENDIAN; addCentralRecord.writeUnsignedInt ( 0x02014b50 ); addCentralRecord.writeShort ( 0x0000 ); addCentralRecord.writeShort ( 0x000a ); addCentralRecord.writeShort ( 0x0000 ); addCentralRecord.writeShort ( 0x0000 ); addCentralRecord.writeUnsignedInt ( 0x00000000 ); addCentralRecord.writeUnsignedInt ( 0 ); addCentralRecord.writeUnsignedInt ( 0 ); addCentralRecord.writeUnsignedInt ( 0 ); addCentralRecord.writeShort ( directoryName.length ); addCentralRecord.writeShort ( 0 ); addCentralRecord.writeShort ( 0 ); addCentralRecord.writeShort ( 0 ); addCentralRecord.writeShort ( 0 ); addCentralRecord.writeUnsignedInt ( 16 ); addCentralRecord.writeUnsignedInt ( this.oldOffset ); this.oldOffset = newOffset; addCentralRecord.writeUTFBytes (directoryName); this.nbDirectory.push ( addCentralRecord ); this.centralDirectory.writeBytes ( addCentralRecord ); } /** * Lets you add a file into a specific directory * * @param pBytes File stream * @param pDirectory Directory name * @example * This example shows how to add files into directories : * <div> * <pre> * * myZip.addFile ( imageByteArray, "images/image.jpg" ); * myZip.addFile ( imageByteArray, "images/funk/image.jpg" ); * </pre> * </div> */ public function addFile ( pBytes:ByteArray, pDirectory:String ):void { pDirectory = pDirectory.split ('\\').join ('/'); var pDirectory_byte:* = new ByteArray(); pDirectory_byte.writeMultiByte(pDirectory, "GBK"); var feedArrayRow:ByteArray = new ByteArray; feedArrayRow.endian = Endian.LITTLE_ENDIAN; // Local File Header feedArrayRow.writeUnsignedInt ( 0x04034b50 ); feedArrayRow.writeShort ( 0x0014 ); feedArrayRow.writeShort ( 0x0000 ); // File is deflated feedArrayRow.writeShort ( this.compressionMethod == CompressionMethod.GZIP ? 0x0008 : 0x0000 ); feedArrayRow.writeUnsignedInt ( unixToDos ( new Date().getTime() ) ); var uncompressedLength:Number = pBytes.length; // CRC32 checksum var crc:CRC32 = new CRC32; crc.generateCRC32 ( pBytes ); var compression:int = crc.getCRC32(); // If GZIP compression if ( compressionMethod == CompressionMethod.GZIP ) { pBytes.compress(); var copy:ByteArray = new ByteArray; copy.writeBytes ( pBytes, 0, pBytes.length - 4 ); var finalCopy:ByteArray = new ByteArray; finalCopy.writeBytes ( copy, 2 ); pBytes = finalCopy; } var compressedLength:int = pBytes.length; feedArrayRow.writeUnsignedInt ( compression ); feedArrayRow.writeUnsignedInt ( compressedLength ); feedArrayRow.writeUnsignedInt ( uncompressedLength ); feedArrayRow.writeShort ( pDirectory_byte.length ); feedArrayRow.writeShort ( 0 ); feedArrayRow.writeBytes ( pDirectory_byte ); feedArrayRow.writeBytes ( pBytes ); // Data Descriptor feedArrayRow.writeUnsignedInt ( compression ); feedArrayRow.writeUnsignedInt ( compressedLength ); feedArrayRow.writeUnsignedInt ( uncompressedLength ); compressedData.writeBytes ( feedArrayRow ); var newOffset:int = compressedData.length; // File header var addCentralRecord:ByteArray = new ByteArray; addCentralRecord.endian = Endian.LITTLE_ENDIAN; addCentralRecord.writeUnsignedInt ( 0x02014b50 ); addCentralRecord.writeShort ( 0x0000 ); addCentralRecord.writeShort ( 0x0014 ); addCentralRecord.writeShort ( 0x0000 ); addCentralRecord.writeShort ( this.compressionMethod == CompressionMethod.GZIP ? 0x0008 : 0x0000 ); addCentralRecord.writeUnsignedInt ( unixToDos ( new Date().getTime() ) ); addCentralRecord.writeUnsignedInt ( compression ); addCentralRecord.writeUnsignedInt ( compressedLength ); addCentralRecord.writeUnsignedInt ( uncompressedLength ); addCentralRecord.writeShort(pDirectory_byte.length); addCentralRecord.writeShort ( 0 ); addCentralRecord.writeShort ( 0 ); addCentralRecord.writeShort ( 0 ); addCentralRecord.writeShort ( 0 ); addCentralRecord.writeUnsignedInt( 32 ); addCentralRecord.writeUnsignedInt ( this.oldOffset ); this.oldOffset = newOffset; addCentralRecord.writeBytes ( pDirectory_byte ); this.nbDirectory.push ( addCentralRecord ); this.centralDirectory.writeBytes ( addCentralRecord ); } /** * Lets you add a comment into the Zip file * * @param pComment The comment string to add * @example * This example shows how to add a comment for the current zip : * <div><pre>myZip.addComment ( "Hello there !");</pre></div> */ public function addComment ( pComment:String ):void { comment = pComment; } /** * Lets you finalize and save the ZIP file and make it available for download * * @param pMethod Can be se to Method.LOCAL, the saveZIP will return the ZIP ByteArray. When Method.REMOTE is passed, just specify the path to the create.php file * @param pURL The url of the create.php file * @param pDownload Lets you specify the way the ZIP is going to be available. Use Download.INLINE if you want the ZIP to be directly opened, use Download.ATTACHMENT if you want to make it available with a save-as dialog box * @param pName The name of the ZIP, only available when Method.REMOTE is used * @return The ByteArray ZIP when Method.LOCAL is used, otherwise the method returns null * @example * This example shows how to save the ZIP with a download dialog-box : * <div> * <pre> * * myZIP.saveZIP ( Method.REMOTE, 'create.php', Download.ATTACHMENT, 'archive.zip' ); * </pre> * </div> */ public function saveZIP ( pMethod:String, pURL:String='', pDownload:String='inline', pName:String='archive.zip' ):* { output = new ByteArray; output.endian = Endian.LITTLE_ENDIAN; output.writeBytes ( this.compressedData ); output.writeBytes ( this.centralDirectory ); output.writeUnsignedInt ( 0x06054b50 ); output.writeUnsignedInt ( 0x00000000 ); output.writeShort ( this.nbDirectory.length ); output.writeShort ( this.nbDirectory.length ); output.writeUnsignedInt ( this.centralDirectory.length ); output.writeUnsignedInt ( this.compressedData.length ); var _loc_3:* = new ByteArray(); _loc_3.writeMultiByte(comment, "GBK"); output.writeShort(_loc_3.length); output.writeBytes(_loc_3); if ( pMethod == Method.LOCAL ) return output; var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream"); var myRequest:URLRequest = new URLRequest ( pURL+'?name='+pName+'&method='+pDownload ); myRequest.requestHeaders.push (header); myRequest.method = URLRequestMethod.POST; myRequest.data = saveZIP ( Method.LOCAL ); navigateToURL ( myRequest, "_blank" ); return null; } private function unixToDos( pTimeStamp:Number ):Number { var currentDate:Date = new Date ( pTimeStamp ); if ( currentDate.getFullYear() < 1980 ) currentDate = new Date (1980, 1, 1, 0, 0, 0); return ( (currentDate.getFullYear() - 1980) << 25) | (currentDate.getMonth() << 21) | (currentDate.getDate() << 16) | (currentDate.getHours() << 11) | (currentDate.getMinutes() << 5) | (currentDate.getSeconds() >> 1); } } }
使用方法是复制以上代码,保存为ASZip.as,然后下载http://code.google.com/p/aszip/downloads/list 解压ASZip 0.2版本后,把保存的ASZip.as直接覆盖到org/aszip/zip 目录下,即可使用。
相关文章推荐
- Ubuntu下亲测解决zip 文件中中文文件名乱码的问题
- Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)
- 解决swfupload上传控件文件名中文乱码问题 三种方法 flash及最新版本11.8.800.168
- 关于ZIP压缩问题 解决中文文件名乱码
- 解决python中用zipfile解压缩文件时中文文件名乱码的问题
- 【成功解决】ubuntu 12.04 解压 zip 7z rar jar 文件,文件名中文乱码的问题
- Ubuntu下解决zip 文件中中文文件名乱码的问题
- Linux 学习解决归档管理器打开rar和zip中文文件名乱码问题
- linux 学习解决归档管理器打开rar和zip中文文件名乱码问题
- 解压文件源码(需要引入ant.jar包,解决zip文件名为中文出现乱码问题)
- Linux 学习解决归档管理器打开rar和zip中文文件名乱码问题
- Fedora 20中解决zip解压文件时中文文件名的乱码问题[已解决]
- Ubuntu下解决zip 文件中中文文件名乱码的问题
- 【成功解决】ubuntu 12.04 解压 zip 7z rar jar 文件,文件名中文乱码的问题
- 解决swfupload上传控件文件名中文乱码问题 三种方法 flash及最新版本11.8.800.168
- Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)
- 解决ubuntu中zip解压的中文乱码问题
- PHP文件上传并解决中文文件名乱码问题
- 解决smartupload获取中文文件名乱码的问题
- jsp实现文件下载与中文文件名乱码问题解决