尝试使用as3 commons zip库。
http://www.as3commons.org/as3-commons-zip/index.html
为了做到这一点,你会需要通过它的所有内容加载目录,循环和加载每个资产。
此代码片段包含一个批量加载程序来处理您的问题。
警告
我把大多数代码了一个项目,我在做类似的事情,但因为是我没有测试它。可能有一些语法错误!
private var zip:Zip;
zip = new Zip();
zip.addEventListener(IOErrorEvent.IO_ERROR, this.createNewZip); //creates a new zip
zip.addEventListener(Event.COMPLETE, handleZipLoaded); //loads the current zip, this is not shown here
zip.load(new URLRequest(File.applicationStorageDirectory.resolvePath("myZip.zip").url)); //path to your zip file
方法来创建新的zip文件
private function createNewZip(e:IOErrorEvent):void{
trace("no zip");
var stream:FileStream = new FileStream();
stream.open(File.applicationStorageDirectory.resolvePath("myZip.zip"), FileMode.WRITE);
zip.serialize(stream);
stream.close();
}
您可以使用此目录中的所有项目添加到您的zip文件。
private function addDirToZip():void{
var f:File = File.resolvePath("Your Dir");
//this will be called when your directory listing has loaded
f.addEventListener(FileListEvent.DIRECTORY_LISTING, handleDirLoaded);
//you can also get the dir listing inline and not use a listener
//doing it async will prevent ui lock
f.getDirectoryListingAsync();
}
接下来你打算一旦每个项目加载,你可以到需要加载的所有文件
protected function handleDirLoaded(e:FileListEvent):void{
loadExternal = new Vector.<File>; //vector used to keep a handle on all files
e.target.removeEventListener(FileListEvent.DIRECTORY_LISTING, handleDirLoaded);
for(var i:int = 0 ; i < files.length ; i++){
var f:File = files[i] as File;
if(f.extension == "File Types you want"){ //you can do some file type checking here
loadExternal.push(f);
}
}
//start loading in the files
loadFile();
}
这将通过loadExternal载体和加载所有文件
private function loadFile():void{
currentFile = loadExternal.shift(); //returns the first item off the array
//load the file
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoaded);
l.load(new URLRequest(currentFile.url));
}
将其存储以便加入到拉链中
private function handleLoaded(e:Event):void{
var l:Loader = e.target.loader as Loader;
l.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleLoaded);
//storing everything in a dictionary
assets[currentFile.name] = l.content;
//if we still have items to load go and do it all again
if(loadExternal.length != 0){
loadFile();
} else {
//now all files are loaded so lets add them to the zip
addAssetsToZip();
}
}
这是所有加载的文件实际上得到放入拉链,它被保存
private funcion addAssetsToZip():void{
for(var fileName:String in assets){
var ba:ByteArray = new ByteArray(); //going to write this to the zip
//make an object that holds the data and filename
var data:Object = {};
data.name = fileName;
data.content = assets[fileName];
ba.writeObject(data);
//write this file to the zip
zip.addFile(key, ba, false);
}
//and finally save everything out
zip.close();
var stream:FileStream = new FileStream();
stream.open(File.applicationStorageDirectory.resolvePath("myZip.zip"), FileMode.WRITE);
zip.serialize(stream);
stream.close();
}
您可以使用简单的压缩文件创建nochump拉链库,你可以使用的FileStream和文件对象直接写入到文件系统,无需AIR的用户交互。 – shaunhusain
首先,我会承认,让我的头绕着进出的拉链对我来说是一场噩梦。我通常会更快地看到事情,并且发现代码很容易,但现在这已经是我了。 – Tempus
编辑--- NoChump和FZIP是我能找到的唯一的,没有足够的信息和例子给我。其他人可能会发现他们很容易,但事情的压缩方面仍然很难。至于让操作系统这么做,我已经把这个设置放在了一边,但是并不是真的想要这样做。我希望有一个程序能够让所有的东西都变成一堆,并且它会膨胀。但是,谢谢你的信息。 – Tempus