Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Adding and Deleting Files Inside AIR's ApplicationDirectory

Tech 1

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();
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.