首页 | 联系我们 | 叶凡网络官方QQ群:323842844
游客,欢迎您! 请登录 免费注册 忘记密码
您所在的位置:首页 > 开发语言 > Java开发 > 正文

java借助rar的cmd命令完成压缩和解压

作者:cocomyyz 来源: 日期:2013-06-26 23:16:15 人气:10 加入收藏 评论:0 标签:java

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.");
  }

}
}


本文网址:http://www.mingyangnet.com/html/java/70.html
读完这篇文章后,您心情如何?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
更多>>网友评论
发表评论
编辑推荐
  • 没有资料