13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with byte arrays<br />

//for each node in orderXML<br />

for each(var child:XML in orderXML) {<br />

// app<strong>en</strong>d child node to text area<br />

myTxt.text += child + "\n";<br />

}<br />

// for Flex version: } // <strong>en</strong>d of init() // read specified file into byte array<br />

function readFile(fileName:String, data:ByteArray) {<br />

var inFile:File = File.desktopDirectory; // source folder is desktop<br />

inFile = inFile.resolvePath(fileName); // name of file to read<br />

var inStream:FileStream = new FileStream();<br />

inStream.op<strong>en</strong>(inFile, FileMode.READ);<br />

inStream.readBytes(data, 0, data.l<strong>en</strong>gth);<br />

inStream.close();<br />

}<br />

/* Add the following lines, minus comm<strong>en</strong>t characters, for Flex:<br />

* ]]><br />

* <br />

*<br />

* <br />

*/<br />

ByteArray example: Reading a .zip file<br />

Adobe AIR 1.0 and later<br />

This example demonstrates how to read a simple .zip file containing several files of differ<strong>en</strong>t types. It does so by<br />

extracting relevant data from the metadata for each file, uncompressing each file into a ByteArray and writing the file<br />

to the desktop.<br />

The g<strong>en</strong>eral structure of a .zip file is based on the specification by PKWARE Inc., which is maintained at<br />

http://www.pkware.com/docum<strong>en</strong>ts/casestudies/APPNOTE.TXT. First is a file header and file data for the first file in<br />

the .zip archive, followed by a file header and file data pair for each additional file. (The structure of the file header is<br />

described later.) Next, the .zip file optionally includes a data descriptor record (usually wh<strong>en</strong> the output zip file was<br />

created in memory rather than saved to a disk). Next are several additional optional elem<strong>en</strong>ts: archive decryption<br />

header, archive extra data record, c<strong>en</strong>tral directory structure, Zip64 <strong>en</strong>d of c<strong>en</strong>tral directory record, Zip64 <strong>en</strong>d of<br />

c<strong>en</strong>tral directory locator, and <strong>en</strong>d of c<strong>en</strong>tral directory record.<br />

The code in this example is writt<strong>en</strong> to only parse zip files that do not contain folders and it does not expect data<br />

descriptor records. It ignores all information following the last file data.<br />

The format of the file header for each file is as follows:<br />

file header signature 4 bytes<br />

required version 2 bytes<br />

g<strong>en</strong>eral-purpose bit flag 2 bytes<br />

compression method 2 bytes (8=DEFLATE; 0=UNCOMPRESSED)<br />

last modified file time 2 bytes<br />

last modified file date 2 bytes<br />

crc-32 4 bytes<br />

Last updated 6/6/2012<br />

783

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!