Adding and Deleting Files Inside AIR's ApplicationDirectory
Adobe AIR exposes five static refreences that point to common filesystem locations:
File.userDirectory
File.documentsDirectory
File.desktopDirectory
File.applicationStorageDirectory
File.applicationDirectory
Content stored under File.applicationDirectory is protected by the runtime sandbox and is read-only by default. Using the standard AIR reference to delete, rename, or write to these entries triggers a security exception.
Reading bundled assets through the resolved reference remains permitted:
var manifest:File = File.applicationDirectory.resolvePath("config/settings.xml");
To bypass the sandbox restriction for mutating operations, construct the File object from the underlying native path rather than the restricted AIR reference:
var target:File = new File(File.applicationDirectory.nativePath + "/config/settings.xml");
target.deleteFile();
For general read-write tasks, target a user-writable location such as the documents directory. The runtime creates the file automatical when opened in write mode if it does not already exist:
var outFile:File = File.documentsDirectory.resolvePath("HelloWorld.txt");
var fs:FileStream = new FileStream();
fs.open(outFile, FileMode.WRITE);
var payload:String = "Sample text written through AIR.";
fs.writeUTFBytes(payload);
fs.close();
When extracting an archive or recursively deploying nested assets, iterate over the payload and branch between directory markers and file blobs:
private var cursor:uint = 0;
private var pipe:FileStream;
private function flushBundle():void
{
if (cursor >= batch.length)
{
trace("Write operations finished");
return;
}
var item:Array = batch[cursor];
var raw:ByteArray = item[0];
var entryName:String = item[1];
var destination:File = item[2];
cursor++;
if (entryName.charAt(entryName.length - 1) == "/")
{
if (!destination.exists)
{
destination.createDirectory();
}
flushBundle();
}
else
{
pipe = new FileStream();
pipe.open(destination, FileMode.WRITE);
pipe.writeBytes(raw);
pipe.close();
flushBundle();
}
}