1

The following code when run in air works great on the desktop. It allows you to select the file and unzip it and write it.

However, when I run it on my phone only "Audio", "Video" , "Images" files become avail able and I can't access a zip file I want to unzip from my phone. Here is the full code:

import com.coltware.airxzip.ZipEntry;
import com.coltware.airxzip.ZipError;
import com.coltware.airxzip.ZipFileReader;
import com.coltware.airxzip.*;

import flash.filesystem.File;
import flash.utils.ByteArray;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;

var file:File;
var fileReference:FileReference;
var fileFilter:FileFilter;
var baseDir:File = File.documentsDirectory;

function unzip_init():void{

    fileFilter = new FileFilter("selected your zip file.", "*");
    file = new File();

    **// This next line did not work and the one below that either. mistakes here!
    //file.browseForDirectory("Choose a directory");**
    file.browseForOpen("file://mnt/sdcard/", [fileFilter]);
    file.addEventListener(Event.SELECT, onSelectedFile);
}

function onSelectedFile(e:Event):void
{

    trace(file.nativePath);
    var reader:ZipFileReader = new ZipFileReader();
    reader.open(file);

    var list:Array = reader.getEntries();
    var i:int = 0;

    for each(var entry:ZipEntry in list)
    {
        var filename:String = entry.getFilename();

        if(entry.isDirectory())
        {

            trace("DIR  --->" + entry.getFilename());
            //  If entry is directory
            var dir:File = baseDir.resolvePath(filename);
            dir.createDirectory();
        }
        else
        {
            trace("FILE --->" + entry.getFilename() + "(" + entry.getCompressRate() + ")");
            var unzippedBytes:ByteArray = reader.unzip(entry);
            //trace("btyes --->" + unzippedBytes);
            var file:File = File.userDirectory.resolvePath(entry.getFilename());
            var fs:FileStream = new FileStream();
            fs.open(file,FileMode.WRITE);
            fs.writeBytes(unzippedBytes,0,unzippedBytes.length);
            fs.close();
        }
    }
    reader.close();
}

unzip_init();
4

1 回答 1

1

您必须使用 flash.filesystem.File 类编写自己的文件浏览器。

以下是我自己的 FileBrowser 类中的一些方法:

/**
 * Displays root directories
 */
public function showRootFolders():void
{
    if (!m_oLocation) m_oLocation = new File();
    var rootDirs:Array = File.getRootDirectories();
    if (!m_vRootDirs) m_vRootDirs = new Vector.<File>();

    for (var i:int = 0; i < rootDirs.length; i++)
    {
        var item:File = rootDirs[i];
        if (i < m_vRootDirs.length) m_vRootDirs[i] = item;
        else m_vRootDirs.push(item);
    }

    if (contains(m_oLevelUp)) removeChild(m_oLevelUp);
    m_oFolderName.setLabel("ROOT");
    m_oFolderInfo.setLabel(rootDirs.length + " Folder(s), " + 0 + " File(s)");

    displayList(m_vRootDirs);
}


/**
 * <p>Gets all files from requested location (File) and updates list view.</p>
 * @param    p_oLocation
 * @param    p_bLinkToParent
 */
public function listLocation(p_oLocation:File, p_bLinkToParent:Boolean=false):void
{
    trace("listLocation(p_oLocation:File):void");
    //folder or file
    if (p_oLocation.isDirectory)
    {
                    clear()://clear list view
        var _vItems:Vector.<File> = new Vector.<File>();

        var files:Array = p_oLocation.getDirectoryListing();

        trace("files.length:"+files.length);
        var _nFiles:int = 0;
        var _nFolders:int = 0;

        for (var i:int = 0; i < files.length; i++)
        {
            var item:File = files[i];

            if (item.isDirectory) _nFolders++;
            else _nFiles++;

            _vItems.push(item);
        }

        if (p_bLinkToParent)
        {
            if (p_oLocation.parent != null || m_vRootDirs.length > 0)
            {
                //return button
                m_oParent = p_oLocation.parent;

                addChild(m_oLevelUp);
            }
            else
            {
                if (contains(m_oLevelUp)) removeChild(m_oLevelUp);
            }
        }
        else
        {
            m_vRootDirs.length = 0;
            if (contains(m_oLevelUp)) removeChild(m_oLevelUp);
            m_oParent = null;
        }

        m_oFolderInfo.setLabel(_nFolders + " Folder(s), " + _nFiles + " File(s)");

        m_oFolderName.setLabel(p_oLocation.name);

        displayList(_vItems);
    }
}


/**
 * <p>Display given file list in the list view</p>
 * @param    p_vFileList
 */
protected function displayList(p_vFileList:Vector.<File>):void
{
    trace("displayList(p_vFileList:Vector.<File>):void");
    clear();//clear list view
    //sort
    p_vFileList = p_vFileList.sort(sortByName)
    //[...]
    //build
    var data:Vector.<ListItemData> = new Vector.<ListItemData>();

    var p:Point = new Point();
    for (var i:int = 0; i < p_vFileList.length; i++)
    {
        var item:File = p_vFileList[i];
                    //here do what ever you like with the list of files
                    //e.g. populate the list view with different type based on the file extension, type etc.
        }
}

希望对你有一点帮助

此致

于 2012-09-04T09:49:36.180 回答