动态解析文件夹下所有的压缩文件

来源:互联网 发布:subversion mac 编辑:程序博客网 时间:2024/05/17 02:38
package com.hfjh.common;import java.io.File;import java.util.Iterator;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class readXML {    ZipUtil zip = new ZipUtil();    public String getName(String site) {//获得文件名int num = site.lastIndexOf(".");int NUM = num;while(site.charAt(num)!= '/')--num;char[] result = new char[NUM-num-1];site.getChars(num+1, NUM, result, 0);StringBuffer sb = new StringBuffer();sb.append(result);return sb.toString();}public String getKind(String site)//获得文件类型{int num = site.lastIndexOf(".")+1;int length = site.length();char[] result = new char[length - num];site.getChars(num, length, result, 0);StringBuffer sb = new StringBuffer();sb.append(result);return sb.toString();} public void  readXML()//XML读取方法{String XmlContent = null;String XmlName = null; String title = null;String kind = null;int num = 0;String url = null;Element element = null;String ID = "";String URL = "";String TITLE="";/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////SAXReader saxReader = new SAXReader();                                                             //org.dom4j.Document doc = null;try {doc = saxReader.read(new File("WEBsearch\\test.xml"));//指定XML文件的相对路径} catch (DocumentException e1) {e1.printStackTrace();}                                       //Element root = doc.getRootElement();@SuppressWarnings("rawtypes")Iterator iter = root.elementIterator();while(iter.hasNext()){Element rootElement = (Element)iter.next();@SuppressWarnings("rawtypes")Iterator childElementIter = rootElement.elementIterator();if(childElementIter.hasNext()){while(childElementIter.hasNext()){Element childElement = (Element)childElementIter.next();                                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////XmlContent = childElement.getText();  XmlName = childElement.getName();if(XmlName.equals("url")){URL=XmlContent;TITLE=getName(XmlContent);}}if(getKind(URL).equalsIgnoreCase("rar"))//指定要解压的文件类型为rar{zip.Decompression(TITLE);System.out.println(TITLE);}else if(getKind(URL).equalsIgnoreCase("zip"))//指定要解压的文件类型为zip{zip.Decompression1(TITLE);System.out.println(TITLE);}}}}public void read() {new readXML().readXML();}}

 package com.hfjh.common;            import java.io.File;      import java.util.ArrayList;            /**      * 文件操作类      * 包括产生临时文件夹,删除临时文件夹等操作方法      * @author poplar      *      */      public class FileUtil {                private static final String linkstr = "\t";                /**          * 删除在解压缩时产生的临时文件夹          * @param folder 临时文件夹路径          * @return true为删除成功,false为失败          */          public static boolean deleteFolder(String folder) {              boolean bool = false;              try {                  deleteAllFile(folder);                  String filePath = folder;                  filePath = filePath.toString();                  File f = new File(filePath);                  f.delete();                  bool = true;              } catch (Exception e) {                  e.printStackTrace();              }              return bool;          }                /**          * 删除文件夹下所有内容          * @param folederPath 文件夹完整路径          * @return          */          public static boolean deleteAllFile(String folederPath) {              boolean bool = false;              String filePath = null;              File file = new File(folederPath);              if (!file.exists()) {                  return bool;              }              if (!file.isDirectory()) {                  return bool;              }              String[] tempList = file.list();              File temp = null;              for (int i = 0; i < tempList.length; i++) {                  if (folederPath.endsWith(File.separator)) {                      temp = new File(folederPath + tempList[i]);                      filePath = temp.getPath();                  } else {                      temp = new File(folederPath + File.separator + tempList[i]);                  }                  if (temp.isFile()) {                      boolean b = temp.delete();                      if (!b) {                          String msg = filePath + "文件删除失败";                          bool = false;                      }                  }                  if (temp.isDirectory()) {                      deleteAllFile(folederPath + "/" + tempList[i]); //删除文件夹里面的文件                            deleteFolder(folederPath + "/" + tempList[i]);  //在删除空文件夹                        }              }              return bool;          }                /**          * 判断文件是否存在          *          * @param fileName          * @return          */          public static boolean isFileExist(String folder, String fileName) {              boolean bool = false;              bool = new File(folder, fileName).exists();              return bool;          }                    /**          * 测试文件夹是否存在          * @param folder          * @return          */          public static boolean existFile(String folder){              File file = new File(folder);              if (file.isDirectory()){                  return true;              }else {                  return false;              }          }                /**          * 对文件路径进行处理,主要是在传递过来的路径下创建一个文件夹          *          * @param folder          * @return 返回新的路径名          */          public static String fileDir(String folder, String folderName) {              String path = null;              if (!FileUtil.isFileExist(folder, folderName)) {                  String fullPath = folder + folderName;                  File f = new File(fullPath);                  f.mkdir();                  path = f.getPath();              }              return path;          }                /**          * 遍历文件夹          * @param file          */          public ArrayList refreshFileList(String strPath) {              ArrayList filelist = new ArrayList();              File dir = new File(strPath);              File[] files = dir.listFiles();              if (files == null) {                  filelist = null;              }              for (int i = 0; i < files.length; i++) {                  if (files[i].isDirectory()) {                      refreshFileList(files[i].getAbsolutePath());                  } else {                      String strFileName = files[i].getAbsolutePath().toLowerCase();                      System.out.println("---" + strFileName);                      filelist.add(files[i].getAbsolutePath());                      return filelist;                  }              }              return filelist;          }      } 

 package com.hfjh.common;            /**      * 这个类是用来做为解压缩时,对文件的一些操作      * yfyang 080411      */      import java.io.File;            public class ZipUtil {                public static final String password = "123456";          public static final String winrarPath = "C:\\Program Files\\WinRAR\\WinRAR.exe";                /**          * 将指定的压缩文件解压缩到指定的路径下 解压缩后在指定的路径下生成一个以压缩文件名的文件夹,文件下即为压缩文件的文件          *          * @param zipFile          *            压缩文件路径          * @param folder          *            要解压到何处的路径          * @return          */          public static boolean zip(String zipFile, String folder) {              boolean bool = false;              folder = folder + stringUtil(zipFile);              String cmd = winrarPath + " x -iext -ow -ver -- " + zipFile + " "                      + folder;              int source = zipFile.lastIndexOf("\\") + 1;              String newPath = zipFile.substring(source);              if (FileUtil.isFileExist(folder, newPath)) {                  bool = false;                  String msg = "在" + folder + "下文件" + newPath + "已经存在";              } else {                  try {                      Process proc = Runtime.getRuntime().exec(cmd);                      if (proc.waitFor() != 0) {                          if (proc.exitValue() == 0) {                              bool = false;                          }                      } else {                          bool = true;                      }                  } catch (Exception e) {                      e.printStackTrace();                  }              }              return bool;          }                /**          * 解压带有密码的压缩文件 默认密码为123456,如果需要更改则只要修改password的值          *          * @param zipFile          * @param folder          * @return          */          public static boolean zipForPassword(String zipFile, String folder) {              boolean bool = false;              String _folder = "\"" + folder + stringUtil(zipFile) + "\\" +  "\"";                      zipFile = "\"" + zipFile + "\"";              String cmd = winrarPath + " x -p" + password + " " + zipFile + " "                      + _folder;              int source = zipFile.lastIndexOf("\\") + 1;              String newPath = zipFile.substring(source);              String folderName = stringUtil(newPath);                    if (FileUtil.isFileExist(folder, folderName)) {                  bool = false;                  String msg = "在" + folder + "下文件" + newPath + "已经存在";              } else {                  try {                      Process proc = Runtime.getRuntime().exec(cmd);                      if (proc.waitFor() != 0) {                          if (proc.exitValue() == 0) {                              bool = false;                          }                      } else {                          bool = true;                      }                  } catch (Exception e) {                      e.printStackTrace();                  }              }              return bool;          }                            /**          * String的方法工具,主要是针对路径中取得压缩文件的名称,不包括后缀名          *          * @param str          * @return 压缩文件的名称          */          public static String stringUtil(String filePath) {              String fileName = new File(filePath).getName();              String fileRealName = null;              int indexStr = fileName.lastIndexOf(".");              fileRealName = fileName.substring(0, indexStr);              return fileRealName;          }                /**          * 测试类          *          * @param args          */          public void Decompression(String st) {          String s;        s="WEBsearch\\testFiles\\"+st+".rar";            String zipFile = s;              String folder = "WEBsearch\\winrar\\";              boolean b = ZipUtil.zipForPassword(zipFile, folder);              // String path = folder + ZipUtil.stringUtil(zipFile);              // boolean d = ZipUtil.deleteFolder(path);              System.out.println(b);              // System.out.println(d);          }        public void Decompression1(String st) {  //解压zip格式的文件        String s;        s="WEBsearch\\testFiles\\"+st+".zip";            String zipFile = s;              String folder = "WEBsearch\\winrar\\";              boolean b = ZipUtil.zipForPassword(zipFile, folder);              // String path = folder + ZipUtil.stringUtil(zipFile);              // boolean d = ZipUtil.deleteFolder(path);              System.out.println(b);              // System.out.println(d);          }        public void Decompression2(String st) {              String zipFile = st;              String folder = st;              boolean b = ZipUtil.zipForPassword(zipFile, folder);              // String path = folder + ZipUtil.stringUtil(zipFile);              // boolean d = ZipUtil.deleteFolder(path);              System.out.println(b);              // System.out.println(d);          }    }  

package com.hfjh.common;import java.io.File;public class bianli { public static void main(String[] args) throws Exception {//调用ZipUtil.java中的read方法来对test.xml文件解压其中的压缩文件       readXML r=new readXML();       r.read();    String filePath = "WEBsearch/winrar";    getFiles(filePath); }   static void getFiles(String filePath){//调用ZipUtil.java中的Decompression2方法来解压文件夹,并放在同一级目录下。 ZipUtil zip = new ZipUtil();  File root = new File(filePath);    File[] files = root.listFiles();    for(File file:files){              if(file.isDirectory()){              getFiles(file.getPath());              if("rar".equals(file.getName().substring(file.getName().lastIndexOf(".")+1))){                     zip.Decompression2(file.getPath());             System.out.println("显示"+filePath+"下所有子目录"+file.getPath()+"解压成功");                  }         }         else{              if("rar".equals(file.getName().substring(file.getName().lastIndexOf(".")+1))){             zip.Decompression2(file.getPath());             System.out.println("显示"+filePath+"下所有子目录"+file.getPath()+"解压成功");          }             }         } }}