//maker 制作压缩包
import fsys; 
import process.popen;
namespace fsys.cab.maker;

folder = function(rootDir,targetPath,CompressionType="LZX"){
	rootDir = ..fsys.longpath(  ..io.fullpath(rootDir) );
	if(!targetPath){
		targetPath = ..string.trimright( rootDir,"\/" ) + ".cab" 
	}
	else {
		targetPath = ..io.fullpath(targetPath)
	}
	
	if( ..io.exist(targetPath) ){
		return null,'cab文件已存在:\r\n' + targetPath
	}
	var tTarget = ..io.splitpath(targetPath)	
  
	var tFiles = {"
.OPTION EXPLICIT
.Set CabinetNameTemplate=""" + tTarget.file + """
.set DiskDirectoryTemplate=CDROM ;All cabinets go in a single directory
.Set CompressionType="+CompressionType+";All files are compressed in cabinet files
.Set UniqueFiles=""OFF""
.Set Cabinet=on
.Set DiskDirectory1="""+..fsys.shortpath(tTarget.dir) +"""
	"};
  
	..fsys.enum(  rootDir, "*.*", 
		function(dir,file,fullpath,findData){ 
			if(file){ 
				..table.push(tFiles,'"'  + fullpath + '"'); 
			}  
			 
		},function(dirpath,dirname){
			..table.push(tFiles,'.Set DestinationDir="'  + ..fsys.path.relative(dirpath,rootDir,false) + '"'); 
			return true;
		} 
	); 
	
	var ddfPath =  ..io.tmpname("ddf") ;
	..string.save(ddfPath, ..string.fromto( ..string.join(tFiles,'\r\n'),65001,0) );
	var parameter = "/V0 /F " + ..fsys.shortpath(ddfPath) + " /D MaxDiskSize=CDROM /D CompressionType="+CompressionType+" /D CompressionMemory=21 /D Cabinet=ON /D Compress=ON /L " +  ..fsys.shortpath(tTarget.dir) +''
	
	var prcs = ..process.popen("makecab.exe",parameter )
	if(!prcs){
		return null,'压缩目录时出错:\r\n' + path;
	}
	prcs.logger = self.logger;
	prcs.logResponse();
	prcs.close();
		
	..io.remove(ddfPath);
	return true,targetPath	 	
}

file = function( path,targetPath,CompressionType="LZX" ){
	path = ..fsys.longpath(  ..io.fullpath(path) )  
	if(!targetPath){
		targetPath = ..string.trimright( path,"\/" ) + ".cab" 
	}
	else {
		targetPath = ..io.fullpath(targetPath)
	} 
	var tTarget = ..io.splitpath(targetPath) 
	var parameter = '/V0 /D MaxDiskSize=CDROM /D CompressionType='+CompressionType+' /D CompressionMemory=21 /D Cabinet=ON /D Compress=ON "' + ..fsys.longpath(path) + '" /L ' +  ..fsys.shortpath(tTarget.dir) + '  "' + tTarget.file + '"'

	var prcs = ..process.popen("makecab.exe",parameter );
	if(!prcs){
		return null,'压缩文件时出错:\r\n' + path;
	} 
	prcs.logger = self.logger;
	prcs.logResponse();
	prcs.close();
	prcs.close();  
	
	return true,targetPath	
}

compress = function(path,targetPath,CompressionType="LZX"){
	path = ..io.fullpath(path)  
	if(!targetPath){
		targetPath = ..string.trimright( path,"\/" ) + ".cab" 
	}
	else {
		targetPath = ..io.fullpath(targetPath)
	}
	if(! ..string.endWith( targetPath,".cab",true) ){
		return "cab文件存储路径错误"; 
	}
	..fsys.delete(targetPath)
	
	if( ! ..fsys.isDir( path ) ){  	
		return file(path,targetPath,CompressionType)
	}
	else{ 
		return folder( path,targetPath,CompressionType ); 
	} 
}

/**intellisense(fsys.cab.maker)
logger = 指定压缩进度默认回显对象\n该对象必须有log或write成员函数用于输出信息
compress(.(待压缩目录或文件,输出目录,"MSZIP") = 压缩文件名目录,生成cab文件,\n支持嵌套子目录\n参数二,参数三可省略,默认使用LZX算法(压缩率较高).\n成功返回true,失败返回 null,错误信息
end intellisense**/
