as3从app - 文件路径获取手机上的zip文件然后解压缩

时间:2012-09-04 06:03:24

标签: android actionscript-3 flash actionscript zip

以下代码在空中运行时在桌面上运行良好。它允许您选择文件并解压缩并写入。

然而,当我在手机上运行时,只有“音频”,“视频”,“图像”文件可用,我无法访问我想从手机解压缩的zip文件。这是完整的代码:

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

1 个答案:

答案 0 :(得分: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.
        }
}

我希望它会对你有所帮助,

最好的问候