AS3commons zip与子目录

时间:2014-05-12 00:02:26

标签: actionscript-3 flash zip

我一直在使用as3commons zip库打包图片以供用户下载,并且发现它到目前为止很容易使用,但是我需要将这些文件组织在目录中 - 经过一天的拖网搜索后我什么都找不到这表明如何在zip文件中创建子目录,我发现的所有内容都集中在向根目录添加文件。有谁知道我会怎么做呢?

由于

1 个答案:

答案 0 :(得分:2)

as3commons为你做了子目录:只需用参数传递文件的完整路径。这是我的一个旧的(部分)样本。它需要一个文件夹和一个zip文件(:File)作为参数(在这里它是tempDir:File

private var zip:Zip = new Zip();
private var zipFile:File = new File();
private var filesToCompress:int = 1;
private var filesCompressed:int = 0;


    /*** further on ***/

    /*** 
     * Waits until all files are compressed and then serializes the zip.
     * */

    public function fileAddedToZip():void{
        filesCompressed++;          
        if (filesToCompress == filesCompressed){

            var stream:FileStream = new FileStream();
            stream.openAsync(zipFile, FileMode.WRITE);

            zip.serialize(stream);                  
            stream.close();
            tempDir.deleteDirectoryAsync(true);         

        }
    }       


    /*** Adds file to zip but doesn't serialize the zip yet 
     * 
     * @param file current file 
     * @param zip save destination
     * @param path current folder nativepath
     * **/


    public function addFileToZip(file:File, zip:Zip, path:String=""):void{
        if(file.isDirectory){
            var directory:Array = file.getDirectoryListing();
            filesToCompress = filesToCompress + directory.length;

            for each (var f:File in directory){
                addFileToZip(f, zip, path + "/" + file.name);
            }
            fileAddedToZip();
        }else{

            var request:URLRequest = new URLRequest(file.nativePath);
            var urlLoader:URLLoader = new URLLoader(request);

            urlLoader.addEventListener(Event.COMPLETE, function (event:Event):void {

                var pathSplit:Array = file.nativePath.split(".tmp\\",2);
                var fileNamePath:String = pathSplit[1] as String;                   
                zip.addFile(fileNamePath, event.target.data);

                fileAddedToZip();

            });
            urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
            urlLoader.load(request);


        }
    }