zip文件内容没有数据

时间:2015-11-22 17:37:36

标签: actionscript-3 flash air

我有一段时间试图制作一个压缩脚本。 我最接近的就是这个。

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();

问题是所有文件都没有数据(没有大小)。

1 个答案:

答案 0 :(得分:1)

我不使用FZIP,但由于你没有回答我检查过,似乎正确的用法如下所示(或至少让你更近)..

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();