当在空气中运行下面的代码在桌面上的伟大工程。 它可以让你选择文件,将它解压缩,并把它写。
然而,当我在我的手机只“音频”上运行,“视频”,“图片”文件变得无济于事能和我无法访问一个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();
您可以选择使用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.
}
}
我希望它会帮助你一点,
最好的祝福