压缩文件内容没有数据 [英] zip file contents have no data

查看:505
本文介绍了压缩文件内容没有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一段时间试图做一个压缩脚本。
最接近我所需要的就是这个。

  import flash.filesystem.File; 
import flash.events.Event;
导入FZIP。*;

var目录: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();

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

解决方案

我不使用FZIP,但由于你没有答案,我检查了周围,似乎正确的用法是这样的下面(或至少让你更靠近)..

  import flash.filesystem.File; 
import flash.events.Event;
导入FZIP。*;
导入FZIP;

var目录: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(); //通用和回收

var fileCount:int = 1; //用于文件计数作为文件名(至少假定1个文件)

var temp_Str:String =; //一个临时的字符串来保存文件的名称,然后将其添加到ZIP
var extension_Str:String =; / /设置文件扩展名放置在ZIP

中(var i:uint = 0; i< files.length; i ++)
{
//#迭代FOR循环
temp_BA.clear(); fs = new FileStream();

#将文件的字节添加到字节数组
var file_2:File = new File(files [i] .nativePath);
fs.open(file_2,FileMode.READ);
fs.readBytes(temp_BA); // temp_BA现在将文件内容保存为字节数组
//temp_BA.writeBytes(fs); //比readbytes方法更快如果可以完成

//#(1)如果你以前的这一行适用于你,保存它
//zip.addFile(file_2.name ,temp_BA); // addFile需要内容字节数组和名称

//#(2)或者像下面这样使用try(1)
extension_Str =jpg; //或从磁盘上的实际文件扩展名中获取字符串
temp_Str =file+ String(fileCount)+。 + extension_Str; //例:使temp_Str =file1.jpg
zip.addFile(temp_Str,temp_BA); //使ZIP包含一个文件名[temp_Str]&文件数据来自[temp_BA]

fs.close();
fileCount ++; //增加一个
}

// 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); //生成ZIP文件数据
fs.close();


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).

解决方案

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

这篇关于压缩文件内容没有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆