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 />

// read compressed file to offset 0 of bytes; for uncompressed files<br />

// the compressed and uncompressed size is the same<br />

if (compSize == 0) continue;<br />

zStream.readBytes(bytes, 0, compSize);<br />

Next, the example uncompresses the compressed file and calls the outfile() function to write it to the output file<br />

stream. It passes outfile() the file name and the byte array containing the file data.<br />

if (compMethod == 8) // if file is compressed, uncompress<br />

{<br />

bytes.uncompress(CompressionAlgorithm.DEFLATE);<br />

}<br />

outFile(fileName, bytes); // call outFile() to write out the file<br />

In the previously m<strong>en</strong>tioned example, bytes.uncompress(CompressionAlgorithm.DEFLATE) will work only in<br />

AIR applications. To get deflated data uncompressed for both AIR and Flash Player, invoke ByteArray’s inflate()<br />

function.<br />

The closing braces indicate the <strong>en</strong>d of the while loop, and of the init() method and the Flex application code, except<br />

for the outFile() method. Execution loops back to the beginning of the while loop and continues processing the next<br />

bytes in the .zip file—either extracting another file or <strong>en</strong>ding processing of the .zip file if the last file has be<strong>en</strong> processed.<br />

} // <strong>en</strong>d of while loop<br />

} // for Flex version, <strong>en</strong>d of init() method and application<br />

The outfile() function op<strong>en</strong>s an output file in WRITE mode on the desktop, giving it the name supplied by the<br />

fil<strong>en</strong>ame parameter. It th<strong>en</strong> writes the file data from the data parameter to the output file stream (outStream) and<br />

closes the file.<br />

// Flash version<br />

function outFile(fileName:String, data:ByteArray):void<br />

{<br />

var outFile:File = File.desktopDirectory; // destination folder is desktop<br />

outFile = outFile.resolvePath(fileName); // name of file to write<br />

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

// op<strong>en</strong> output file stream in WRITE mode<br />

outStream.op<strong>en</strong>(outFile, FileMode.WRITE);<br />

// write out the file<br />

outStream.writeBytes(data, 0, data.l<strong>en</strong>gth);<br />

// close it<br />

outStream.close();<br />

}<br />

private function outFile(fileName:String, data:ByteArray):void<br />

{<br />

var outFile:File = File.desktopDirectory; // dest folder is desktop<br />

outFile = outFile.resolvePath(fileName); // name of file to write<br />

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

// op<strong>en</strong> output file stream in WRITE mode<br />

outStream.op<strong>en</strong>(outFile, FileMode.WRITE);<br />

// write out the file<br />

outStream.writeBytes(data, 0, data.l<strong>en</strong>gth);<br />

// close it<br />

outStream.close();<br />

}<br />

Last updated 6/6/2012<br />

787

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

Saved successfully!

Ooh no, something went wrong!