package com.mingyangnet.test;
import java.io.IOException;
/**
* @author www.mingyangnet.com
*/
public class CmdRarOperater {
// Pay attention to the blank in string.
private static String rarCmd = "C:\\Program Files\\winrar\\Rar.exe a ";
private static String unRarCmd = "C:\\Program Files\\winrar\\UnRAR.exe x ";
// The command in wins' dos.
// Create rar package : rar c:\liming c:\test.ppt
// UnCreate rar package : unrar x d:\test.rar d:\
/**
* Create Rar package.
*
* @param destDir
* the destination folder name.
* @param destFileName
* the destination file name.
* @param fileDir
* the folder name which has the needing rar file.
* @param fileName
* the file name and folder and if it is a file which must
* include file extension.
* @return whether create rar file successfully.
*/
private boolean rarFile(String destDir, String destFileName,
String fileDir, String fileName) {
boolean flag = false;
// Pay attention to the blank after .rar.
rarCmd += destDir + destFileName + ".rar ";
rarCmd += fileDir + fileName;
Runtime rt = Runtime.getRuntime();
try {
rt.exec(rarCmd);
flag = true;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
/**
* Un rar file.
*
* @param destDir
* @param destFileName
* @param fileDir
* @param fileName
* @return
*/
private boolean unRarFile(String destDir, String destFileName,
String fileDir, String fileName) {
boolean flag = false;
unRarCmd += destDir + destFileName + ".rar ";
unRarCmd += fileDir + fileName;
Runtime rt = Runtime.getRuntime();
try {
rt.exec(unRarCmd);
flag = true;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
public static void main(String args[]) {
CmdRarOperater rar = new CmdRarOperater();
// Create rar package example.
String destDir = "d:\\";
String destFileName = "test";
String fileDir = "c:\\";
String fileName = "db";
boolean flag = rar.rarFile(destDir, destFileName, fileDir, fileName);
if (flag == true) {
System.out.println("Create rar file successfully!");
} else {
System.out.println("Failed to create rar file!");
}
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// UnCreate rar package example.
destDir = "d:\\";
destFileName = "test";
fileDir = "d:\\";
fileName = "";
flag = rar.unRarFile(destDir, destFileName, fileDir, fileName);
if (flag = true) {
System.out.println("Un rar the file successfully.");
} else {
System.out.println("Failed to un rar the file.");
}
}
}