java 壓縮rar文件

//壓縮是通過命令來調用的,需要服務器安裝rar
package com.gs.system.util.clear;

import com.gs.system.util.Config;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by gj.hu on 2016/3/22.
 */
public class RARUtil {

    /**
     * @param
     * @throws InterruptedException
     * @throws IOException
     * @throws IOException
     * @throws InterruptedException
     */
    public static int fileToRar(String aimpath,String realPath) throws Exception {
        String rarPaht= Config.readConfig("rar_path");
        StringBuffer cmd= new StringBuffer(rarPaht);//安裝的winrar路徑
        cmd.append(" a -ep ");
        cmd.append(aimpath);
        cmd.append(" ");
        cmd.append(realPath);
        Process proc;
        try {
            proc = Runtime.getRuntime().exec(cmd.toString());
            BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream(), "UTF-8"));
            String line;
            while ((line = br.readLine()) != null) {
                //System.out.println(line);
            }

            br = new BufferedReader(new InputStreamReader(proc.getErrorStream(), "UTF-8"));
            while ((line = br.readLine()) != null) {
                //System.err.println(line);
            }
            // 當前線程等待該process結束,既掛起主線程
            proc.waitFor();
            return 1;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }


    }

    public static void main(String[] args) throws Exception {
        String aimpath="E:/7.rar ";
        String realPath="E:/1/20160323/tar";
        int dd=fileToRar(aimpath,realPath);
        System.out.println("11");
        System.out.println(dd);
    }

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章