Java目录处理及遍历解压
2015-09-02 10:13
417 查看
import java.io.File; import java.io.FileFilter; import java.io.IOException; public class DirProcessor { private FileFilter dirFileFilter = new FileFilter() { public boolean accept(File f) { if (f.isDirectory()) { return true; } return false; } }; private FileFilter txtFilefilter = new FileFilter() { public boolean accept(File f) { if (f.isFile() && isTxt(f.getName().toLowerCase())) { return true; } return false; } }; private FileFilter htmlFilefilter = new FileFilter() { public boolean accept(File f) { if (f.isFile() && isHtml(f.getName().toLowerCase())) { return true; } return false; } }; private FileFilter docFilefilter = new FileFilter() { public boolean accept(File f) { if (f.isFile() && isDoc(f.getName().toLowerCase())) { return true; } return false; } }; private FileFilter mhtFilefilter = new FileFilter() { public boolean accept(File f) { if (f.isFile() && isMht(f.getName().toLowerCase())) { return true; } return false; } }; private FileFilter compressFilefilter = new FileFilter() { public boolean accept(File f) { if (f.isFile() && isCompress(f.getName().toLowerCase())) { return true; } return false; } }; private FileFilter otherFilefilter = new FileFilter() { public boolean accept(File f) { if (f.isFile() && isOtherFile(f.getName().toLowerCase())) { return true; } return false; } }; private FileFilter countFilefilter = new FileFilter() { public boolean accept(File f) { if (f.isFile()) { return true; } return false; } }; private boolean isLinux = CmdUtil.isLinux(); private String destRootDir; // XXX 统计目录下文件数 public void count(String srcDir) { File srcDirFile = new File(srcDir); if (!srcDirFile.isDirectory()) { System.out.println(srcDir + " is not a directory!"); return; } System.out.println(srcDirFile.getName() + "/ " + countSubLevel(srcDirFile, 0)); } private int countSubLevel(File srcDirFile, int level) { File[] files = srcDirFile.listFiles(countFilefilter); int fileNum = files.length; files = srcDirFile.listFiles(dirFileFilter); for (File file : files) { int subFileNum = countSubLevel(file, level+1); System.out.println(getLevelStr(level) + file.getName() + "/ " + subFileNum); fileNum += subFileNum; } return fileNum; } // XXX 显示目录结构, level 深度 public void tree(String srcDir, int showLevel) { File srcDirFile = new File(srcDir); if (!srcDirFile.isDirectory()) { System.out.println(srcDir + " is not a directory!"); return; } System.out.println(srcDirFile.getName()); showDirLevel(srcDirFile, 0, showLevel); } private void showDirLevel(File srcDirFile, int level, int showLevel) { if (showLevel > 0 && level >= showLevel) { return; } File[] files = srcDirFile.listFiles(); for (File file : files) { String fileName = file.getName(); if (file.isDirectory()) { fileName += "/"; } System.out.println(getLevelStr(level) + fileName); if (file.isDirectory()) { showDirLevel(file, level+1, showLevel); } } } // XXX 递归解压目录下压缩文件 public void decompressDir(String rootDir, boolean display) { File rootDirFile = new File(rootDir); if (!rootDirFile.isDirectory()) { System.out.println(rootDir + " is not a directory!"); return; } String backupRootDir = rootDir+"_backup"; File backupDirFile = new File(backupRootDir); if (backupDirFile.exists()) { System.out.println(backupRootDir + " dir exists!"); return; } //backupDirFile.mkdirs(); if (display) { System.out.println(rootDir); } decompress(rootDirFile.getPath(), 0, display); } private void decompress(String parentDirPath, int level, boolean display) { File parentDirFile = new File(parentDirPath); //System.out.println("# " + parentDirPath); // 解压目录下压缩文件 File[] files = parentDirFile.listFiles(compressFilefilter); for (File file : files) { String fileName = file.getName(); if (file.isFile()) { if (display) { System.out.println(getLevelStr(level) + fileName); // structure } // 去除文件名空格(重命名) String fileNameNew = fileName.replaceAll("\\s*", ""); if (!fileNameNew.equals(fileName)) { File fileNew = new File(file.getPath().replaceAll("\\s*", "")); file.renameTo(fileNew); fileName = fileNew.getName(); } String srcFile = parentDirPath+"/"+fileName; String destDir = parentDirPath+"/"+getName(fileName); if (!isLinux) { srcFile = parentDirPath+"\\"+fileName; destDir = parentDirPath+"\\"+getName(fileName); } String suffix = getSuffix(fileName); if (suffix.equalsIgnoreCase("rar")) { File dirnameFile = new File(destDir); if (!dirnameFile.exists()) { if (dirnameFile.mkdirs()) { if (isLinux) { CmdUtil.exec("unrar x -o+ " + srcFile + " -d " + destDir); } else { CmdUtil.exec("winrar e -o+ \"" + srcFile + "\" " + destDir); } } else { System.out.println("Error: Failed to create dir: " + destDir); } } else { System.out.println("unrar: exists " + destDir); } } else if (suffix.equalsIgnoreCase("zip")) { File dirnameFile = new File(destDir); if (!dirnameFile.exists()) { if (isLinux) { //CmdUtil.exec("unzip \"" + srcFile + "\" -d " + destDir); CmdUtil.exec("7za x " + srcFile + " -o" + destDir); String convFileName = "convmv -r -f utf8 -t iso88591 "+destDir+"/* --notest --nosmart"; String convFileName2 = "convmv -r -f gbk -t utf8 "+destDir+"/* --notest --nosmart"; CmdUtil.exec(convFileName); CmdUtil.exec(convFileName2); } else { if (dirnameFile.mkdirs()) { CmdUtil.exec("winrar e -o+ \"" + srcFile + "\" " + destDir); } else { System.out.println("Error: Failed to create dir: " + destDir); } } } else { System.out.println("unzip: exists " + destDir); } } } } // 处理子目录 files = parentDirFile.listFiles(dirFileFilter); for (File file : files) { if (file.isDirectory()) { String fileName = file.getName(); String filePath = file.getPath(); // rename String fileNameNew = fileName.replaceAll("\\s*", ""); if (!fileNameNew.equals(fileName)) { File fileNew = new File(file.getPath().replaceAll("\\s*", "")); file.renameTo(fileNew); fileName = fileNew.getName(); filePath = fileNew.getPath(); } if (display) { System.out.println(getLevelStr(level) + fileName + "/"); // structure } decompress(filePath, level+1, display); } } } // XXX 拷贝目录结构,采样 public void copy(String srcDir, String destDir, int fileNum, int dirNum) { File srcDirFile = new File(srcDir); if (!srcDirFile.isDirectory()) { System.out.println(srcDir + " is not a directory!"); return; } File destDirFile = new File(destDir); if (destDirFile.exists()) { System.out.println(destDir + " exists!"); return; } destRootDir = destDir; System.out.println(srcDirFile.getName()+"/"); doCopy(srcDirFile, srcDirFile.getName(), fileNum, dirNum, 0); } private String getCopyDestDir(String destRelativeDir) { if (destRelativeDir == null || destRelativeDir.isEmpty()) { return destRootDir; } return destRootDir + File.separator + destRelativeDir; } private void doCopy(File srcDirFile, String destRelativeDir, int fileNum, int dirNum, int level) { File[] files = srcDirFile.listFiles(); // 采样文件 String destDir = getCopyDestDir(destRelativeDir); new File(destDir).mkdirs(); int i = 0; for (File file : files) { if (file.isFile()) { if (++i > fileNum) { break; } //System.out.println(getLevelStr(level) + file.getName()); // file if (isLinux) { CmdUtil.exec("cp -f "+file.getPath()+" "+destDir); } else { CmdUtil.exec("cmd.exe /c copy /y \""+file.getPath()+"\" "+destDir); } } } // 子目录 files = srcDirFile.listFiles(dirFileFilter); i = 0; for (File file : files) { if (++i > dirNum) { break; } System.out.println(getLevelStr(level) + file.getName() + "/"); // dir doCopy(file, destRelativeDir+File.separator+file.getName(), fileNum, dirNum, level+1); } } // XXX 目录按类型分类 public void classify(String srcDir, String destDir, boolean rename) { File srcDirFile = new File(srcDir); if (!srcDirFile.isDirectory()) { System.out.println(srcDir + " is not a directory!"); return; } File destDirFile = new File(destDir); if (destDirFile.exists()) { System.out.println(destDir + " exists!"); try { System.out.println("Press anykey to continue!"); System.in.read(); } catch (IOException e) { e.printStackTrace(); } } else { if (!destDirFile.mkdirs()) { System.out.println("Failed to create dir: " + destDir); } } destRootDir = destDir; classifyByType(srcDirFile, srcDirFile.getName(), 0, rename); } private void classifyByType(File srcDirFile, String destRelativeDir, int level, boolean rename) { // html File[] files = srcDirFile.listFiles(htmlFilefilter); copyFilesToDestDir("html", files, destRelativeDir, level, rename); // txt files = srcDirFile.listFiles(txtFilefilter); copyFilesToDestDir("txt", files, destRelativeDir, level, rename); // doc files = srcDirFile.listFiles(docFilefilter); copyFilesToDestDir("doc", files, destRelativeDir, level, rename); // mht files = srcDirFile.listFiles(mhtFilefilter); copyFilesToDestDir("mht", files, destRelativeDir, level, rename); // compressed file files = srcDirFile.listFiles(compressFilefilter); copyFilesToDestDir("compressed", files, destRelativeDir, level, false); // other files = srcDirFile.listFiles(otherFilefilter); copyFilesToDestDir("other", files, destRelativeDir, level, false); // 子目录 files = srcDirFile.listFiles(dirFileFilter); for (File file : files) { System.out.println(getLevelStr(level) + file.getName() + "/"); classifyByType(file, destRelativeDir+File.separator+file.getName(), level+1, rename); } } private void copyFilesToDestDir(String typeDir, File[] files, String destRelativeDir, int level, boolean rename) { if (files.length > 0) { String destDir = destRootDir + File.separator + typeDir + File.separator + destRelativeDir; File destDirFile = new File(destDir); if (destDirFile.exists()) { System.out.println("Exists dir: " + destDir); } else { if (!destDirFile.mkdirs()) { System.out.println("Failed to mkdirs: " + destDir); } } for (File file : files) { String destFile; if (rename) { int pos = file.getName().lastIndexOf("."); destFile = destDir + File.separator + IDManager.getInstance().genID() + file.getName().substring(pos); } else { destFile = destDir; } //System.out.println(getLevelStr(level) + file.getName()); if (isLinux) { CmdUtil.exec("cp -f "+file.getPath()+" "+destFile); } else { //System.out.println("cmd.exe /c copy /y "+file.getPath()+" "+destDir); CmdUtil.exec("cmd.exe /c copy /y \""+file.getPath()+"\" "+destFile); } } } } private String getLevelStr(int level) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < level; i++) { sb.append("+ "); } sb.append("+-- "); return sb.toString(); } private String getSuffix(String fileName) { if (fileName != null) { int pos = fileName.lastIndexOf('.'); if (pos > -1) { return fileName.substring(pos+1); } } return ""; } private String getName(String fileName) { if (fileName != null) { int pos = fileName.lastIndexOf('.'); if (pos > -1) { return fileName.substring(0, pos); } } return ""; } private boolean isTxt(String fname) { return fname.endsWith(".txt"); } private boolean isHtml(String fname) { return (fname.endsWith(".html") || fname.endsWith(".htm")); } private boolean isDoc(String fname) { return (fname.endsWith(".doc") || fname.endsWith(".docx")); } private boolean isMht(String fname) { return (fname.endsWith(".mht")); } private boolean isCompress(String fname) { return (fname.endsWith(".zip") || fname.endsWith(".rar") || fname.endsWith(".gz") || fname.endsWith(".tar")); } private boolean isOtherFile(String fname) { return !( isTxt(fname) || isHtml(fname) || isDoc(fname) || isMht(fname) || isCompress(fname)); } }
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CmdUtil {
private static String OS = System.getProperty("os.name").toLowerCase();
private static Runtime run = Runtime.getRuntime();
public static boolean showCmdDetail = false;
public static String getOS() {
return OS;
}
public static boolean isLinux(){
return OS.indexOf("linux")>=0;
}
public static boolean isWindows(){
return OS.indexOf("windows")>=0;
}
public static void exec(String cmd) {
System.out.println("exec: " + cmd);
try {
Process p = run.exec(cmd);
try {
if (showCmdDetail) {
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String inline;
while ((inline = br.readLine()) != null) {
System.out.println(inline);
}
}
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class IDManager { private int start = 10000000; private static IDManager instance = new IDManager(); public static IDManager getInstance() { return instance; } private IDManager() { } public int getStart() { return start; } public void setStart(int start) { this.start = start; } public int genID() { return ++start; } }
public class Main
{
public static final int CPDIR_FILE_NUM = 5;
public static final int CPDIR_DIR_NUM = 10;
public static void usage()
{
System.out.println("Usage:");
System.out.println(" tree {rootDir} [level>0]");
System.out.println(" count {rootDir}");
System.out.println(" decomp {rootDir} [displayDir:0/1]");
System.out.println(" classify {srcDir} {destDir} [nameid:10000000]");
System.out.println(" cpdir {srcDir} {destDir} [fileNum=5] [dirNum=10]");
}
public static void process(String[] args)
{
System.out.println(CmdUtil.getOS());
if (args.length < 2) {
System.out.println("Error -- too few parametes");
usage();
return;
}
String option = args[0];
String srcDir = args[1];
DirProcessor dirProc = new DirProcessor();
if (args[args.length-1].equals("v")) {
CmdUtil.showCmdDetail = true;
String [] args2 = new String[args.length-1];
System.arraycopy(args, 0, args2, 0, args.length-1);
args = args2;
}
if (option.equals("count")) {
dirProc.count(srcDir);
}
else if (option.equals("tree")) {
int level = 0;
if (args.length >= 3) {
level = Integer.parseInt(args[2]);
}
dirProc.tree(srcDir, level);
}
else if (option.equalsIgnoreCase("decomp")) {
boolean display = false;
if (args.length >= 3) {
int displayPath = Integer.parseInt(args[2]);
display = (displayPath==1)?true:false;
}
dirProc.decompressDir(srcDir, display);
}
else if (option.equalsIgnoreCase("cpdir")) {
if (args.length < 3) {
System.out.println("Error -- cpdir {destDir} missing.");
usage();
return;
}
String destDir = args[2];
int fileNum = CPDIR_FILE_NUM;
if (args.length >= 4) {
fileNum = Integer.parseInt(args[3]);
}
int dirNum = CPDIR_DIR_NUM;
if(args.length >= 5) {
dirNum = Integer.parseInt(args[4]);
}
dirProc.copy(srcDir, destDir, fileNum, dirNum);
}
else if (option.equalsIgnoreCase("classify")) {
if (args.length < 3) {
System.out.println("Error -- classify {destDir} missing.");
usage();
return;
}
boolean rename = false;
int nameidStart = -1;
if (args.length >= 4) {
if (args[3].startsWith("nameid")) {
nameidStart = Integer.parseInt(args[3].substring(args[3].indexOf(":")+1));
IDManager.getInstance().setStart(nameidStart);
rename = true;
System.out.println("# file nameid start: " + nameidStart);
}
}
String destDir = args[2];
dirProc.classify(srcDir, destDir, rename);
System.out.println("### nameid: " + nameidStart + " -> " + IDManager.getInstance().getStart());
}
else {
System.out.println("Error -- unkown option: " + option);
usage();
}
}
public static void main( String[] args )
{
process(args);
}
}
相关文章推荐
- springmvc 乱码问题
- java excel转dbf
- mac下配置eclipse的maven环境
- JDK 1.7源码阅读笔记(三)集合类之LinkedList
- 在Eclipse中使用JUnit4进行单元测试(下)
- Eclipse 扩大内存
- 在Eclipse中使用JUnit4进行单元测试(中)
- 在Eclipse中使用JUnit4进行单元测试(上)
- Java经典实例(第二版)
- Java经典实例(第二版)
- Java经典实例(第二版)
- java 中的匿名内部类
- Java 调用OPENOFFIC 转换文档类型
- eclipse plugin 菜单
- Java String intern() 方法解析 常量池
- 相遇java
- Java中a+=b和a=a+b的区别
- fedora上安装sun jdk
- JAVA中的多线程操作 线程等待、阻塞、让步等
- Eclipse中使用正则表达式搜索替换