Question

I've been using the as3commons zip library to package images for user download, and have found it very easy to use so far, but I need the files to be organised in directories - after a day of trawling I can find nothing which indicates how one creates subdirectories in a zip file, everything I've found focuses on adding files to the root. Does anyone know how I'd go about doing this?

Thanks

Was it helpful?

Solution

as3commons does the subdirectories for you: just pass the full path to the file with the arguments. Here's an old (partial) sample of mine. It takes an folder and a zip file (:File) as an argument (here it's 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);


        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top