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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Working with byte arrays<br />

The next part of the code reads the header byte at offset position 8 and stores the value in the variable compMethod.<br />

This byte contains a value indicating the compression method that was used to compress this file. Several compression<br />

methods are allowed, but in practice nearly all .zip files use the DEFLATE compression algorithm. If the curr<strong>en</strong>t file is<br />

compressed with DEFLATE compression, compMethod is 8; if the file is uncompressed, compMethod is 0.<br />

bytes.position = 8;<br />

compMethod = bytes.readByte(); // store compression method (8 == Deflate)<br />

Following the first 30 bytes is a variable-l<strong>en</strong>gth portion of the header that contains the file name and, possibly, an extra<br />

field. The variable offset stores the size of this portion. The size is calculated by adding the file name l<strong>en</strong>gth and extra<br />

field l<strong>en</strong>gth, read from the header at offsets 26 and 28.<br />

offset = 0;// stores l<strong>en</strong>gth of variable portion of metadata<br />

bytes.position = 26; // offset to file name l<strong>en</strong>gth<br />

flNameL<strong>en</strong>gth = bytes.readShort();// store file name<br />

offset += flNameL<strong>en</strong>gth; // add l<strong>en</strong>gth of file name<br />

bytes.position = 28;// offset to extra field l<strong>en</strong>gth<br />

xfldL<strong>en</strong>gth = bytes.readShort();<br />

offset += xfldL<strong>en</strong>gth;// add l<strong>en</strong>gth of extra field<br />

Next the program reads the variable-l<strong>en</strong>gth portion of the file header for the number of bytes stored in the offset<br />

variable.<br />

// read variable l<strong>en</strong>gth bytes betwe<strong>en</strong> fixed-l<strong>en</strong>gth header and compressed file data<br />

zStream.readBytes(bytes, 30, offset);<br />

The program reads the file name from the variable l<strong>en</strong>gth portion of the header and displays it in the text area along<br />

with the compressed (zipped) and uncompressed (original) sizes of the file.<br />

// Flash version<br />

bytes.position = 30;<br />

fileName = bytes.readUTFBytes(flNameL<strong>en</strong>gth); // read file name<br />

taFiles.app<strong>en</strong>dText(fileName + "\n"); // write file name to text area<br />

bytes.position = 18;<br />

compSize = bytes.readUnsignedInt(); // store size of compressed portion<br />

taFiles.app<strong>en</strong>dText("\tCompressed size is: " + compSize + '\n');<br />

bytes.position = 22; // offset to uncompressed size<br />

uncompSize = bytes.readUnsignedInt(); // store uncompressed size<br />

taFiles.app<strong>en</strong>dText("\tUncompressed size is: " + uncompSize + '\n');<br />

// Flex version<br />

bytes.position = 30;<br />

fileName = bytes.readUTFBytes(flNameL<strong>en</strong>gth); // read file name<br />

taFiles.text += fileName + "\n"; // write file name to text area<br />

bytes.position = 18;<br />

compSize = bytes.readUnsignedInt(); // store size of compressed portion<br />

taFiles.text += "\tCompressed size is: " + compSize + '\n';<br />

bytes.position = 22; // offset to uncompressed size<br />

uncompSize = bytes.readUnsignedInt(); // store uncompressed size<br />

taFiles.text += "\tUncompressed size is: " + uncompSize + '\n';<br />

The example reads the rest of the file from the file stream into bytes for the l<strong>en</strong>gth specified by the compressed size,<br />

overwriting the file header in the first 30 bytes. The compressed size is accurate ev<strong>en</strong> if the file is not compressed<br />

because in that case the compressed size is equal to the uncompressed size of the file.<br />

Last updated 6/6/2012<br />

786

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

Saved successfully!

Ooh no, something went wrong!