Search code examples
actionscript-3flashair

zip file contents have no data


I have for a while been trying to make a zipping script. The closest I have come to what I am needing is this one.

import flash.filesystem.File;
import flash.events.Event;
import FZIP.*;

var directory:File = File.desktopDirectory.resolvePath("Test File");

var zip:FZip = new FZip(); 
var files:Array = directory.getDirectoryListing();
for(var i:uint = 0; i < files.length; i++)
{
var file_2:File = new File(files[i].nativePath);
//  zip.addFile(file_2.name, file_2.data);
zip.addFile(file_2.name, ba);
trace(file_2.name);
trace(files[i].nativePath);
trace(file_2.data);
trace(file_2.size);
}

var ba:ByteArray = new ByteArray(); 
zip.serialize(ba); 
ba.position = 0; 
var finalZIP:File = File.desktopDirectory.resolvePath("TEST.zip"); 
var fs:FileStream = new FileStream(); 
fs.open(finalZIP, FileMode.WRITE); 
fs.writeBytes(ba); 
trace("BA POS: "+ba.position);
fs.close();

The problem is that all files are without data (have no size).


Solution

  • I don't use FZIP but since you got no answer I checked around and it seems the correct usage would be something like this below (or at least get you closer)..

    import flash.filesystem.File;
    import flash.events.Event;
    import FZIP.*;
    import FZIP;
    
    var directory:File = File.desktopDirectory.resolvePath("Test File");
    var files:Array = directory.getDirectoryListing();
    var zip:FZip = new FZip(); 
    
    var fs:FileStream; 
    var temp_BA:ByteArray = new ByteArray(); //general use and recycle
    
    var fileCount:int = 1; //for file count as file name (assumes 1 file at least)
    
    var temp_Str:String = ""; // a temp String to hold a file's name before adding it to ZIP
    var extension_Str : String = ""; //set file extension placed inside the ZIP
    
    for(var i:uint = 0; i < files.length; i++)
    {
        //# reseting for each iteration of the FOR loop
        temp_BA.clear(); fs = new FileStream(); 
    
        //# add file's bytes into a byte array
        var file_2:File = new File(files[i].nativePath);
        fs.open(file_2, FileMode.READ);
        fs.readBytes( temp_BA ); //temp_BA now holds file content as byte array
        //temp_BA.writeBytes( fs ); //is faster than readbytes method if can be done
    
        //# (1) If this line you had before works for you, keep it
        //zip.addFile(file_2.name, temp_BA); //addFile needs the content byte array and a name for it
    
        //# (2) or use try like this below version instead (1) above
        extension_Str = "jpg" ; //or get String from actual file extension on disk
        temp_Str = "file" + String(fileCount) + "." + extension_Str; //eg: makes temp_Str = "file1.jpg"
        zip.addFile( temp_Str, temp_BA); //make ZIP contain a file-name [temp_Str] & file-data is from [temp_BA]
    
        fs.close();
        fileCount++; //increment by one
    }
    
    //var finalZIP:File = File.desktopDirectory.resolvePath("TEST.zip"); 
    var finalZIP:File = File.applicationStorageDirectory;
    finalZIP = finalZIP.resolvePath("TEST.zip");
    
    var fs:FileStream = new FileStream();
    fs.open(finalZIP, FileMode.WRITE);
    zip.serialize(fs); //generate ZIP file data
    fs.close();