ArticleDirectory
- Zlibcompressor. mxml
- Unziptest. mxml
Flash. utils. bytearray compressing 4.1 MB to 20 K
PostedDaniel wanja 3 hours ago
I am currently preparing a demo using an MX: olapcube and
Olapdatagrid which analyze the rails SVN commit log. However I don't
Want to deploy a specific server side application as the cube can load
Data fromXML. So I have an report. XML that is 4.1 MB. I created the followingAir
Application (zlibcompressor. mxml) that use the standard Compression
Provided by the bytearray class to compress this file down to 20kb.
Application that consumes this file (unziptest. mxml) uses the urlloader
To read this file straight into a bytearray and uncompress the data.
It's fast!
The key code for compression is the 'computing' and 'uncomputeds'
Method provided by the bytearray. Note the urlloader dataformat is set
To "binary ".
Zlibcompressor. mxml
<? XML version = "1.0" encoding = "UTF-8" ?>
< MX: windowedapplication Xmlns: MX = "Http://www.adobe.com/2006/mxml" Layout = "Vertical"
Nativedragdrop = "Ondrop (event )"
Nativedragenter = "Ondragin (event )" >
< MX: script >
<! [CDATA [
Import flash. desktop. clipboardformats;
Import flash. utils. compressionalgorithm;
Public Function ondragin (Event: nativedragevent): void {
VaR transferable: clipboard = event. Clipboard;
If (transferable. hasformat (clipboardformats. file_list_format )){
Dragmanager. acceptdragdrop (this );
}
}
Public Function ondrop (Event: nativedragevent): void {
VaR filelist: array = event. clipboard. dataforformat (clipboardformats. file_list_format) as array;
If (filelist. Length = 0) return;
VaR infile: file = filelist [0];
VaR filestream: filestream = new filestream ();
Filestream. Open (infile, filemode. Read );
VaR BA: bytearray = new bytearray ();
Filestream. readbytes (BA, 0, filestream. bytesavailable );
Filestream. Close ();
VaR newfilename: String = infile. nativepath + ". zlib ";
Ba. Compress ();
VaR OUTFILE: file = new file (newfilename );
Filestream = new filestream ();
Filestream. Open (OUTFILE, filemode. Write );
Filestream. writebytes (BA, 0, ba. Length );
Filestream. Close ();
}
]>
</ MX: script >
</ MX: windowedapplication >
Unziptest. mxml
<? XML version = "1.0" encoding = "UTF-8" ?>
< MX: Application Xmlns: MX = "Http://www.adobe.com/2006/mxml" Layout = "Vertical" Creationcomplete = "Loaddata ()" >
< MX: script >
<! [CDATA [
Import MX. rpc. Events. resultevent;
Import flash. utils. bytearray;
Import flash. Events .*;
Import flash.net .*;
Private function loaddata (): void {
VaR Loader: urlloader = new urlloader ();
Loader. dataformat = "binary ";
Loader. addeventlistener (event. Complete, completehandler );
VaR request: URLRequest = new URLRequest ("../data/report. xml. zlib ");
Loader. Load (request );
}
Private function completehandler (Event: Event): void {
VaR Loader: urlloader = urlloader(event.tar get );
VaR BA: bytearray = loader. Data;
Ba. uncompress ();
VaR S: String = ba. tostring ();
VaR XML: xml = New XML (s );
}
]>
</ MX: script >
</ MX: Application >