Blog Archive

Friday, October 15, 2010

AIR ve Flex ile Unzip yapmak

Bu class'ı yazarken Javascript ile yazılmış http://help.adobe.com/en_US/AIR/1.5/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7d53.html örneğin actionsctipt 3.0 ile yazılmış hali.

package com.egnity
{
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.utils.ByteArray;
import flash.utils.CompressionAlgorithm;
import flash.utils.Endian;

public class UnzipFile
{

public function UnzipFile()
{

}
public function unzip(zipFilePath:String,targetPath:String):void{
var bytes:ByteArray= new ByteArray();
var flNameLenght:int;
var xfldLength:int;
var offset:int;
var compSize:int;
var uncompSize:Number;
var compMethod:Object;
var signature:int;
var output;
var flNameLength:int;
var fileName:String
var zipFile:File = new File(zipFilePath);
var zipStream:FileStream = new FileStream();
zipStream.open(zipFile,FileMode.READ);
bytes.endian = Endian.LITTLE_ENDIAN;
while(zipStream.position
zipStream.readBytes(bytes,0,30);
bytes.position =0;
signature = bytes.readInt();
if(signature!=0x04034b50){
break;
}
bytes.position=8;
compMethod = bytes.readByte();
offset = 0;    // stores length of variable portion of metadata
bytes.position = 26;  // offset to file name length
flNameLength = bytes.readShort();    // store file name
offset += flNameLength;     // add length of file name
bytes.position = 28;    // offset to extra field length
xfldLength = bytes.readShort();
offset += xfldLength;    // add length of extra field
// read compressed file to offset 0 of bytes; for uncompressed files
// the compressed and uncompressed size is the same
// read variable length bytes between fixed-length header and compressed file data
zipStream.readBytes(bytes, 30, offset);
bytes.position = 30;
fileName = bytes.readUTFBytes(flNameLength); // read file name
output += fileName + "
"; // write file name to text area
bytes.position = 18;
compSize = bytes.readUnsignedInt();  // store size of compressed portion
output += "\tCompressed size is: " + compSize + '
';
bytes.position = 22;  // offset to uncompressed size
uncompSize = bytes.readUnsignedInt();  // store uncompressed size
zipStream.readBytes(bytes, 0, compSize);
if (compMethod == 8) // if file is compressed, uncompress
{
bytes.uncompress(CompressionAlgorithm.DEFLATE);
}
outFile(fileName, bytes,targetPath);   // call outFile() to write out the file
}
}
private function outFile(fileName:String, data:ByteArray,targetPath:String)
{
var outFile:File = new File(targetPath+"/"+fileName)
var outStream:FileStream = new FileStream();
// open output file stream in WRITE mode
outStream.open(outFile, FileMode.WRITE);
// write out the file
outStream.writeBytes(data, 0, data.length);
// close it
outStream.close();
}
}
}


kullanımı ise şöyle 

var unzip:UnzipFile = new UnzipFile();
unzip.unzip(ornek.zip.hedefDirectory);

No comments:

Post a Comment

Followers