根据XML解压文件夹

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()
	{
		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"));
		} 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"))
			{
				zip.Decompression(TITLE);
				System.out.println(TITLE);
			}
			else if(getKind(URL).equalsIgnoreCase("zip"))
			{
				zip.Decompression1(TITLE);
				System.out.println(TITLE);
			}
			}	
		}
	}
	public static void main(String[] args) {
		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) {  
        	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);  
        }
    } 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值