Skip to content

Instantly share code, notes, and snippets.

@peterblazejewicz
Created May 12, 2011 09:57
Show Gist options
  • Save peterblazejewicz/968268 to your computer and use it in GitHub Desktop.
Save peterblazejewicz/968268 to your computer and use it in GitHub Desktop.
downloading zip file using URLStream for optimized memory use
package
{
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.net.URLStream;
import flash.utils.ByteArray;
import mdm.Application;
import mdm.FileSystem;
import mx.controls.ProgressBar;
import spark.components.Application;
import spark.components.Button;
public class StreamingDownloadExample extends Application
{
// this are instances on screen setup via mxml
public var startButton:Button;
public var progressBar:ProgressBar;
// constants
private static const ZIP_FILE_URL:String = "http://www.multidmedia.com/downloads/exchange/files/Simple_Applescript_Sample_042011.zip";
// variables
[Bindable]
protected var txt:String = "";
private var appendFlag:Boolean = false;
private var path:String = null;
private var zipStream:URLStream = null;
//
// when application starts init {mdm} script
public function initApplication():void
{
mdm.Application.init(this);
};
//
// this is started via user action (button click)
// start downlaoding binary content via URLStream
// (that method setup all required Flash-side implementation
//
public function startDownload():void
{
// reset UI and variables to control state
startButton.enabled = false;
progressBar.enabled = true;
txt = "";
appendFlag = false;
// if for some reason zipStream exists - reuse it
if(zipStream && zipStream.connected)
{
zipStream.close();
} else
{
zipStream = new URLStream();
zipStream.addEventListener(Event.COMPLETE, completeHandler);
zipStream.addEventListener(ProgressEvent.PROGRESS, progressHandler);
zipStream.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
progressBar.source = zipStream;
};
// create request add random hash to avoid caching stricly for this example purpose
var request:URLRequest = new URLRequest(ZIP_FILE_URL);
request.data = {id:(new Date().getTime())};
debug("remote path: "+request.url);
// this will effectively start remote request and download process
zipStream.load(request);
request = null;
}
//
private function progressHandler(progressEvent:ProgressEvent):void
{
debug("event: "+progressEvent.type);
if(zipStream.bytesAvailable == 0) return;
writeBytes();
};
//
private function writeBytes():void
{
if(zipStream.connected)
{
var bytes:ByteArray = new ByteArray();
zipStream.readBytes(bytes);
mdm.FileSystem.BinaryFile.setDataBA(bytes);
var path:String = this.localPath;
if(appendFlag == false)
{
appendFlag = true;
mdm.FileSystem.BinaryFile.writeDataBA(path);
} else
{
mdm.FileSystem.BinaryFile.appendDataBA(path);
}
//
bytes = null;
};
}
//
// on complete reset UI and state and cleanup resources
//
private function completeHandler(completeEvent:Event):void
{
debug("event: "+completeEvent.type);
if(zipStream.connected) zipStream.close();
zipStream.removeEventListener(Event.COMPLETE, completeHandler);
zipStream.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
zipStream.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
progressBar.source = null;
zipStream = null;
startButton.enabled = true;
progressBar.enabled = false;
};
//
// when something goes wrong
// note: there should be no issue with remote content loading via standalone Flash
// player and 3rd party executables (no socket policy is required)
//
private function errorHandler(errorEvent:IOErrorEvent):void
{
debug("event: "+errorEvent.type);
};
//
// construct local path from remote path
//
private function get localPath():String
{
if(path == null)
{
var filename:String = ZIP_FILE_URL.split("/").pop() as String;
debug("filename: "+filename);
path = mdm.Application.path+filename;
debug("local path: "+path);
};
return path;
}
//
private function debug(msg:*):void{txt += msg.toString()+"\n";};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment