java调用shell执行数据压缩

项目需求:

将迁移来的数据文件进行压缩。要求压缩过程耗时尽量少,尽量降低资源消耗。

 

实现思路:

经过测试决定采用tar命令进行压缩。有一个问题:数据文件的目录和压缩后存放路径非同一路径,压缩时需要先cd到压缩目录下,然后才能将压缩包存放到指定目录下。经过查阅资料,决定采用java程序调用shell脚本的方式,执行目录的切换和压缩。

 

示例代码:

public File tar(String caseId,List baseTypeIdList,String caseTime,String localPath) throws Exception{
		String tarPkgName = "";
		localPath = localPath.replaceAll("//", "\\/");
		
		Map dirMap = new HashMap();
		Map zipDirs = this.getZipDirs(localPath,dirMap);
		
		String tarDir = MoveConstants.PATH_TAR_PKG_PREFIX + caseTime + File.separator;
		File zipDirObj = new File(tarDir);
		if(!zipDirObj.exists()){
			zipDirObj.mkdirs();
		}
		
		Set mapKeys = zipDirs.keySet();
		Iterator it = mapKeys.iterator();
		while (it.hasNext()) {
			String lastDir = (String) it.next();
			String allPath = (String) zipDirs.get(lastDir);
			
			String[] dirs = allPath.split("\\/");
			for(int i = 1; i < dirs.length; i++){
				if(i == 1){
					tarPkgName = dirs[i];
				}else{
					tarPkgName = tarPkgName +"_"+ dirs[i];
				}
			}
			String tarFileName = tarDir + tarPkgName + ".tar.gz";
			
			//如何定位到压缩目录
			//采用调用shell脚本的方式	
			String shellCmd = "sh /root/test.sh "+ tarDir +" "+ tarFileName +" "+ allPath;
			logger.debug("\n\n^^^^^^^^^^^^^^^^^^^ shellCmd = "+ shellCmd);
			
			int rtn = new JavaShellUtil().executeShell(shellCmd);
			logger.debug("\n\n^^^^^^^^^^^^^^^^^^^ shell rtn = "+ rtn);
		}
		
		return new File(MoveConstants.PATH_TAR_PKG_PREFIX + caseTime + File.separator);
	}

 

ublic class JavaShellUtil {
	// 基本路径
	private static final String basePath = "/usr/move_test/tar/";
	
	// 记录Shell执行状况的日志文件的位置(绝对路径)
	private static final String EXEC_SHELL_LOG = basePath + "execShell.log";
	
	
	public int executeShell(String shellCommand) throws IOException {
		int success = 0;
		StringBuffer stringBuffer = new StringBuffer();
		BufferedReader bufferedReader = null;
		
		// 格式化日期时间,记录日志时使用
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS"); 
		try {
			stringBuffer.append(dateFormat.format(new Date())).append("准备执行Shell命令 ").append(shellCommand).append(" \r\n"); 
			Process pid = null;
			String[] cmd = {"/bin/sh", "-c", shellCommand};
		
			// 执行Shell命令
			pid = Runtime.getRuntime().exec(cmd);
			if (pid != null) {
				stringBuffer.append("进程号:").append(pid.toString()).append("\r\n");
				// bufferedReader用于读取Shell的输出内容
				bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024);
				pid.waitFor();
			} else {
				stringBuffer.append("没有pid\r\n");
			}
			
			stringBuffer.append(dateFormat.format(new Date())).append("Shell命令执行完毕\r\n执行结果为:\r\n");
			String line = null;
			// 读取Shell的输出内容,并添加到stringBuffer中
			while (bufferedReader != null &&(line = bufferedReader.readLine()) != null) {
				stringBuffer.append(line).append("\r\n");
			}
		}catch (Exception ioe) {
			stringBuffer.append("执行Shell命令时发生异常:\r\n").append(ioe.getMessage()).append("\r\n");
			
		}finally {if (bufferedReader != null) {
			OutputStreamWriter outputStreamWriter = null;
			try {
				bufferedReader.close();
				//将Shell的执行情况输出到日志文件中
				OutputStream outputStream = new FileOutputStream(EXEC_SHELL_LOG);
				outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
				outputStreamWriter.write(stringBuffer.toString());
			}catch (Exception e) {
				e.printStackTrace();
			}finally {
				outputStreamWriter.close();
			}
		}
			success = 1;
		}
		return success;
	}

}

 

shell脚本:

注意尽量在linux下编写,在windows上写好上传到linux上往往会有问题

 

#!/bin/bash

if [ $# != 3]; then
 echo "Please input 3 parameters!"
 exit
fi

cd $1
tar -zcvf $2 $3

 

 

http://moppet.taobao.com/

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值