winRar解压、压缩文件

package rar;

import java.util.Date;
/**
 * 利用winRar解压、压缩文件或文件夹
 * @author 研发中心开发部-wisdomLee
 * @company 中国航信
 * @date Jan 7, 2013
 */
public class RarUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String path = "C:\\Program Files (x86)\\WinRAR\\";
		String fileFrom = "D:/bcpFile/";
		String fileTo = "D:/bcpFileRaR.rar";
		winRar(path,fileFrom,fileTo);
	}
	
	/**
	 * 压缩文件
	 * 
	 * @param path
	 *            WinRaR.exe文件路径
	 * @param fileFrom
	 *            要压缩文件路径
	 * @param fileTo
	 *            压缩后文件路径
	 * @return
	 */
	public static boolean winRar(String path,String fileFrom,String fileTo){
		boolean b = false;
		Runtime rt = Runtime.getRuntime();
		Process p = null;
		int exitVal;
		try{
			p = rt.exec(path+"WinRaR.exe a -ep1 -r- -ibck -o+ "+fileTo+" "+fileFrom);//压缩文件
			exitVal = p.waitFor();
			if (exitVal == 0) {
				System.out.println("压缩成功!");
			}
		}catch (Exception e) {
			System.out.println(new Date()+",压缩失败:"+e.getMessage());
		}finally{
			p.destroy();
			rt.freeMemory();
		}
		return b;
	}
	
	/**
	 * 解压文件
	 * 
	 * @param path
	 *            WinRaR.exe文件路径
	 * @param fileFrom
	 *            要解压文件路径
	 * @param fileTo
	 *            解压后文件路径
	 * @return
	 */
	public static boolean unWinRar(String path,String fileFrom,String fileTo){
		boolean b = false;
		Runtime rt = Runtime.getRuntime();
		Process p = null;
		int exitVal;
		try{
			p = rt.exec(path+"WinRaR.exe x -ibck -y -o+ "+fileFrom+" "+fileTo);//解压文件
			exitVal = p.waitFor();
			if (exitVal == 0) {
				System.out.println("解压成功!");
			}
		}catch (Exception e) {
			System.out.println(new Date()+",解压失败:"+e.getMessage());
		}finally{
			p.destroy();
			rt.freeMemory();
		}
		return b;
	}

}

猜你喜欢

转载自lizhihui19871127.iteye.com/blog/1768517