import java.io.*;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import java.util.*;
/**
* 服务器由于由众多组件构成,组件可能是本次定制的和共享提供的,以及基线的共同构成,故需把共享提供和基线的放在一个目录下
* (该目录下必须以Server,CMS文件夹再分类),然后解压到安装目录中并且重命名,与本次定制的一起进行打包
*
*/
public class UnZipAndRarUtilBase9600
{
/*
* cmd 压缩与解压缩命令
*/
private static String rarCmd = "C:\\Program Files\\WinRAR\\Rar.exe a ";
private static String unrarCmd = "C:\\Program Files\\WinRAR\\UnRar x ";
private static String SOURCE_DIR="F:/test";
private static String DEST_DIR="F:/test/to";
private static String MODULE="pms";
private static String INTSALL_DIR="F:/test/to";
private static String CMS="CMS";
private static String SERVER="SERVER";
/**
* 将1个RAR文件解压
* @param rarFilePath 需要解压的RAR文件(必须包含路径信息以及后缀)
* @param destDir 解压后的文件放置目录
*/
public static void unRar(String rarFilePath, String destDir) {
File f=new File(destDir);
if(!f.exists()){
f.mkdir();
}
unrarCmd ="C:\\Program Files\\WinRAR\\UnRar x ";
unrarCmd += rarFilePath + " " + destDir;
try {
Runtime rt = Runtime.getRuntime();
rt.exec(unrarCmd);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 解压zip
* @param zipFilePath
* @param destDir
*/
public static void unZip(String zipFilePath, String destDir) {
Project proejct = new Project();
Expand expand = new Expand();
expand.setProject(proejct);
expand.setSrc(new File(zipFilePath));
expand.setOverwrite(false);//是否覆盖
expand.setEncoding("GBK");
File file = new File(destDir);
expand.setDest(file);
expand.execute();
}
/**
*重命名文件夹名称
*@param path 需要重命名文件夹的父文件夹
*/
public static void renameFolder(String path){
System.out.println("Debug::开始重命名文件");
File parent=new File(path);
File[]dirs=parent.listFiles(new FileFilter()
{
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return pathname.isDirectory();
}
});
if(null!=dirs){
System.out.println("Debug::dirs length="+dirs.length);
}else{
System.out.println("Debug::dirs length="+dirs);
}
if(dirs!=null){
for(int i=0;i<dirs.length;i++){
String dirName=dirs[i].getName().toLowerCase();
System.out.println("Current Dir="+dirName);
if(dirName.startsWith("ams")&&!dirName.equals("ams")){
File newfile=new File(dirs[i].getParent()+"/ams");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名ams");
}
if(dirName.startsWith("bms")&&!dirName.equals("bms")){
File newfile=new File(dirs[i].getParent()+"/bms");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名bms");
}
if(dirName.startsWith("dc")&&!dirName.equals("dc")){
File newfile=new File(dirs[i].getParent()+"/dc");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名dc");
}
if(dirName.startsWith("dms")&&!dirName.equals("dms")){
File newfile=new File(dirs[i].getParent()+"/dms");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名dms");
}
if(dirName.startsWith("kms")&&!dirName.equals("kms")){
File newfile=new File(dirs[i].getParent()+"/kms");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名kms");
}
if(dirName.startsWith("kps")&&!dirName.equals("kps")){
File newfile=new File(dirs[i].getParent()+"/kps");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名kps");
}
if(dirName.startsWith("tvmc")&&!dirName.equals("tvmc")){
File newfile=new File(dirs[i].getParent()+"/tvmc_Install");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名tvmc");
}
if(dirName.startsWith("ncg")&&!dirName.equals("ncg")){
File newfile=new File(dirs[i].getParent()+"/ncg");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名ncg");
}
if(dirName.startsWith("nvrvod")&&!dirName.equals("nvrvod")){
File newfile=new File(dirs[i].getParent()+"/nvrvod");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名nvrvod");
}
if(dirName.startsWith("pcnvr")&&!dirName.equals("pcnvr")){
File newfile=new File(dirs[i].getParent()+"/pcnvr");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名pcnvr");
}
if(dirName.startsWith("pms")&&!dirName.equals("pms")){
File newfile=new File(dirs[i].getParent()+"/pms");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名pms");
}
if(dirName.startsWith("tvms")&&!dirName.equals("tvms")){
File newfile=new File(dirs[i].getParent()+"/tvms");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名tvms");
}
if(dirName.startsWith("vag")&&!dirName.equals("vag")){
File newfile=new File(dirs[i].getParent()+"/vag"); ;
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名vag");
}
if(dirName.startsWith("vod")&&!dirName.equals("vod")){
File newfile=new File(dirs[i].getParent()+"/vod");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名vod");
}
if(dirName.startsWith("vrb")&&!dirName.equals("vrb")){
File newfile=new File(dirs[i].getParent()+"/vrb");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名vrb");
}
if(dirName.startsWith("vrm")&&!dirName.equals("vrm")){
File newfile=new File(dirs[i].getParent()+"/vrm");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名vrm");
}
if(dirName.startsWith("vtdu")&&!dirName.equals("vtdu")){
File newfile=new File(dirs[i].getParent()+"/vtdu");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名vtdu");
}
if(dirName.startsWith("vtm")&&!dirName.equals("vtm")){
File newfile=new File(dirs[i].getParent()+"/vtm");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名vtm");
}
if(dirName.startsWith("stream")&&!dirName.equals("stream")){
File newfile=new File(dirs[i].getParent()+"/stream");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名stream");
}
if(dirName.startsWith("nms")&&!dirName.equals("nms")){
File newfile=new File(dirs[i].getParent()+"/nms");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名nms");
}
//以下是Server依赖的Common组件
if(dirName.startsWith("sntp_client")&&!dirName.equals("sntp_client")){
File newfile=new File(dirs[i].getParent()+"/sntp_client");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名sntp_client");
}
if((dirName.startsWith("watchdog")&&!dirName.equals("watchdog"))||dirName.startsWith("watch_dog")||dirName.startsWith("看门狗")){
File newfile=new File(dirs[i].getParent()+"/watchdog");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名watchdog");
}
//以下是CMS依赖的Common组件
if(dirName.startsWith("sntp_server")&&!dirName.equals("sntp_server")){
File newfile=new File(dirs[i].getParent()+"/sntp_server");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名sntp_server");
}
if(dirName.startsWith("RecordQueryService")&&!dirName.equals("RecordQueryService")){
File newfile=new File(dirs[i].getParent()+"/RecordQueryService");
dirs[i].renameTo(newfile);
System.out.println("Debug::Parent Folder::"+dirs[i].getParent()+",需要重命名RecordQueryService");
}
}
}else{
System.out.println("没有需要重命名的文件夹");
}
}
/**
*删除需要定制的压缩包
*@param location 基线成果物路径
*@param module 需要删除的成果物 :ams,bms,nms,pms,vrb
*@param sharePath 共享定制成果物所在的文件夹:D:\jenkins\workspace\JobName\成果物文件夹名\Share
*/
public static void deleteCompressFile(String location,String module,String sharePath){
System.out.println("Debug::删除需要定制的压缩包");
String[] component=new String[]{"dc","dms","kms","ncg","nvrvod","pcnvr","tvms","vag","vod","vrm","vtm","vtdu","kps"};
List<String> shareToBeDel=new ArrayList<String>();
File folder=new File(location);
File share = new File(sharePath);
String[] arr=module.split(",");
String[] rarfiles= folder.list(
new FilenameFilter()
{
public boolean accept(File file,String name)
{
return name.endsWith(".rar");
}
}
);
String[] rarSharefiles= share.list(
new FilenameFilter()
{
public boolean accept(File file,String name)
{
return name.endsWith(".rar");
}
}
);
String[] zipfiles= folder.list(
new FilenameFilter()
{
public boolean accept(File file,String name)
{
return name.endsWith(".zip");
}
}
);
String[] zipSharefiles= share.list(
new FilenameFilter()
{
public boolean accept(File file,String name)
{
return name.endsWith(".zip");
}
}
);
System.out.println("Debug::基线文件夹下rar压缩文件数量:"+(rarfiles==null?0:rarfiles.length));
if(rarfiles!=null){
for(int j=0;j<arr.length;j++){
for(int i=0;i<rarfiles.length;i++){
System.out.println("Debug::rar压缩文件名称:"+rarfiles[i]);
if(rarfiles[i].indexOf(arr[j])!=-1){
File f=new File(folder,rarfiles[i]);
System.out.println("Debug::删除:"+f.getAbsolutePath());
f.delete();
}
}
}
}
System.out.println("Debug::基线文件夹下zip压缩文件数量:"+(zipfiles==null?0:zipfiles.length));
if(zipfiles!=null){
for(int j=0;j<arr.length;j++){
for(int i=0;i<zipfiles.length;i++){
System.out.println("Debug::zip压缩文件名称:"+zipfiles[i]);
if(zipfiles[i].indexOf(arr[j])!=-1){
File f=new File(folder,zipfiles[i]);
System.out.println("Debug::删除:"+f.getAbsolutePath());
f.delete();
}
}
}
}
//删除共享提供或上次定制的成果物对应的基线成果物,
System.out.println("Debug::共享文件夹下rar压缩文件数量:"+(rarSharefiles==null?0:rarSharefiles.length));
if(rarSharefiles!=null){
for(int j=0;j<component.length;j++){
for(int i=0;i<rarSharefiles.length;i++){
System.out.println("Debug::rar压缩文件名称:"+rarSharefiles[i]);
if(rarSharefiles[i].toLowerCase().indexOf(component[j])!=-1){
shareToBeDel.add(component[j]);
}
}
}
}
System.out.println("Debug::共享文件夹下zip压缩文件数量:"+(zipSharefiles==null?0:zipSharefiles.length));
if(zipSharefiles!=null){
for(int j=0;j<component.length;j++){
for(int i=0;i<zipSharefiles.length;i++){
System.out.println("Debug::zip压缩文件名称:"+zipSharefiles[i]);
if(zipSharefiles[i].toLowerCase().indexOf(component[j])!=-1){
shareToBeDel.add(component[j]);
}
}
}
}
System.out.println(shareToBeDel.toArray().toString());
System.out.println(shareToBeDel.toString());
for(int i=0;i<rarfiles.length;i++){
for(int j=0;j<shareToBeDel.size();j++){
System.out.println("Debug::"+rarfiles[i]+",S:"+shareToBeDel.get(j));
if(rarfiles[i].indexOf(shareToBeDel.get(j))!=-1){
File f=new File(folder,rarfiles[i]);
System.out.println("Debug::删除rar::"+f.getAbsolutePath());
f.delete();
}
}
}
for(int i=0;i<zipfiles.length;i++){
for(int j=0;j<shareToBeDel.size();j++){
if(zipfiles[i].indexOf(shareToBeDel.get(j))!=-1){
File f=new File(folder,zipfiles[i]);
System.out.println("Debug::删除zip:"+f.getAbsolutePath());
f.delete();
}
}
}
}
/**
* 解压压缩文件
*/
public static void uncompressFiles(){
System.out.println("Debug::开始解压缩压缩文件");
File folder=new File(SOURCE_DIR);
String[]rarfiles= folder.list(
new FilenameFilter()
{
public boolean accept(File file,String name)
{
return name.endsWith(".rar");
}
}
);
String[]zipfiles= folder.list(
new FilenameFilter()
{
public boolean accept(File file,String name)
{
return name.endsWith(".zip");
}
}
);
System.out.println("Debug::rar压缩文件数量:"+(rarfiles==null?0:rarfiles.length));
if(rarfiles!=null){
for(int i=0;i<rarfiles.length;i++){
System.out.println("Debug::rar压缩文件名称:"+rarfiles[i]);
unRar(SOURCE_DIR+"/"+rarfiles[i], DEST_DIR);
}
}
System.out.println("Debug::zip压缩文件数量:"+(zipfiles==null?0:zipfiles.length));
if(zipfiles!=null){
for(int i=0;i<zipfiles.length;i++){
System.out.println("Debug::zip压缩文件名称:"+zipfiles[i]);
unZip(SOURCE_DIR+"/"+zipfiles[i], DEST_DIR);
}
}
System.out.println("Debug::解压结束!");
}
public static void main(String[] args) throws Exception{
System.out.println("args length="+args.length);
System.out.println("源文件路径:args[0]=="+args[0]);
System.out.println("目标文件夹路径:args[1]=="+args[1]);
System.out.println("task:args[2]=="+args[2]);//0 denote compress, 1 rename else del
System.out.println("所选服务器编译模块:args[3]=="+args[3]);
System.out.println("共享定制成果物所在的文件夹:args[4]=="+args[4]);
if(args.length<5){
return;
}
UnZipAndRarUtilBase9600.SOURCE_DIR=args[0];
UnZipAndRarUtilBase9600.DEST_DIR=args[1];
UnZipAndRarUtilBase9600.MODULE=args[3];
if(args[2].equals("0")){
System.out.println("uncompress");
uncompressFiles();
}else if(args[2].equals("1")){
System.out.println("rename");
renameFolder(UnZipAndRarUtilBase9600.DEST_DIR);
}else{
System.out.println("del");
deleteCompressFile(args[0],args[3],args[4]);
}
}
}