检查文件目录结构

package com.me;

 

 


public class Check {

 private static final String path = "D://WorkSpace//ec2";
 
 /**
  * @param args
  * @throws WrongPathException
  */
 public static void main(String[] args) throws WrongPathException {
  CheckBundle cb = new CheckBundle();
  cb.checkAll(path); 
  
 } 
 
}
-----------------------------------------------------------------------------------------

package com.me;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CheckBundle {

 String[] str_need = {"service","templates","metadata","packages"};
 
 String[] str_needfile = {"csd","ttd","ntd","gz","zip"};
 
 private Map<String, Boolean> mp_dir = new HashMap<String, Boolean>();
 
 private Map<String, Boolean> mp_file = new HashMap<String, Boolean>();
 
 private List<String> lt_soft = new ArrayList<String>();
 
 private List<String> lt_meta = new ArrayList<String>();
 
 public CheckBundle() {
  // TODO Auto-generated constructor stub
  for(String st : str_need){
   mp_dir.put(st, false);
  }
  for(String str : str_needfile){
   mp_file.put(str, false);
  }
 }
 
 public void checkAll(String path) throws WrongPathException
 {
  check(path);
  checkResult();
 }
 
 public void check(String path) throws WrongPathException {
  File file = new File(path);
  String[] files = file.list();
  for (String fileOrdir : files) {
   File file_t = new File(path+"//"+fileOrdir);
   if (file_t.isDirectory()) {
    checkDirIsNeeded(path,fileOrdir);
    check(path+"//"+fileOrdir);
   } else {
    checkFileIsNeeded(path,fileOrdir);
   }
  }
 }
 
 public void checkDirIsNeeded(String path,String dir) throws WrongPathException
 {
  if(mp_dir.containsKey(dir))
  {
   if(dir.equals("metadata") && !path.endsWith("service"))
   {
    throw new WrongPathException(dir);
   }
   if(dir.equals("packages") && !path.endsWith("service"))
   {
    throw new WrongPathException(dir);
   }
   mp_dir.remove(dir);
   mp_dir.put(dir, true);
   
  }
  
 }
 
 public void checkFileIsNeeded(String path,String fileName) throws WrongPathException
 {
  for(String str : str_needfile)
  {
   if(fileName.endsWith(str))
   {
    if(fileName.endsWith("csd") && !path.endsWith("service"))
    {
     throw new WrongPathException(fileName);
    }
    if(fileName.endsWith("ttd") && !path.endsWith("templates"))
    {
     throw new WrongPathException(fileName);
    }
    if(fileName.endsWith("ntd") && !path.endsWith("templates"))
    {
     throw new WrongPathException(fileName);
    }
    if(fileName.endsWith("gz") && !path.endsWith("packages"))
    {
     throw new WrongPathException(fileName);
    }else if(fileName.endsWith("gz") && path.endsWith("packages")){
     String[] ff = fileName.split("-");
     fileName = ff[0];
     lt_soft.add(fileName);
    }
    if(fileName.endsWith("zip") && !path.endsWith("metadata"))
    {
     throw new WrongPathException(fileName);
    }else if(fileName.endsWith("zip") && path.endsWith("metadata")){
     String[] ff = fileName.split("-");
     fileName = ff[0];
     lt_meta.add(fileName);
    }
    mp_file.remove(str);
    mp_file.put(str, true);
    
   }
  }
 }
 
 public void checkResult()
 {
  java.util.Iterator<String> it_dir = mp_dir.keySet().iterator();
  while(it_dir.hasNext())
  {
   String ss = it_dir.next();
   boolean bol = mp_dir.get(ss);
   if(!bol)
   {
    System.out.println("缺少必要的目录:"+ss);
   }
  }

  Set<String> set_file = mp_file.keySet();
  Iterator<String> it_file = set_file.iterator();
  while(it_file.hasNext())
  {
   String ss = it_file.next();
   boolean bol = mp_file.get(ss);
   if(!bol)
   {
    System.out.println("缺少必要的文件:"+ss);
   }
  }
  
  int n = lt_soft.size();
  for(int i=0; i< n; i++)
  {
   String str_soft = lt_soft.get(i);
//   System.out.println("111"+str_soft);
//   System.out.println("222"+lt_meta.get(0));
   String str_meta = str_soft+"_meta";
   if(!lt_meta.contains(str_meta))
   {
    System.out.println("软件:"+str_soft+" 缺少对应的元数据包:"+str_meta);
   }
  }
 }
}
--------------------------------------------------------------

 

package com.me;

public class WrongPathException extends Exception {

 /**
  *
  */
 private static final long serialVersionUID = 2468393428172451669L;
 
 public WrongPathException(String message) {
  System.out.println(message+" 文件或文件夹处于不正确的位置!");
 }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值