Flash. utils. bytearray compressing 4.1 MB to 20 K

Source: Internet
Author: User
Tags cdata uncompress
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 >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.