gzip --rsyncable不使用Runtime.exec()从java压缩

时间:2015-03-01 22:02:40

标签: gzip rsync runtime.exec

我正在尝试使用gzip选项运行--rsyncable - 它在终端窗口中运行时工作正常(我在Mac OS上) - 但是从我运行它时它不起作用java使用以下代码。

知道问题可能是什么?

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


public class RsyncIssue {
    public static void printOutput(Process p) throws IOException{
            String ss;
            BufferedReader inReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while((ss = inReader.readLine()) != null ){
                    System.out.println("[IN] "+ ss);
            }
        }

    public static void main(String[] args) throws IOException {
        File f = new File("Test.ext");
        if( !f.exists())
        {
            f.createNewFile();
        }
        String zipCommand = "gzip --rsyncable " + f.getCanonicalPath();
        System.out.println("Zipping file : " + f.getName() );
        System.out.println("Zipping command:  " + zipCommand);
        Process p = Runtime.getRuntime().exec(zipCommand);
        printOutput(p);
        File zipfile = new File(f.getCanonicalPath()+".gz");
        if( !zipfile.exists())
        {
            throw new RuntimeException("zip file does not exist " + zipfile.getAbsolutePath());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你应该做的事情:

  • 调用p.waitFor(),以便执行阻止,直到执行完成。
  • 然后致电p.exitValue()并与0进行比较,看看是否有错误。
  • printOutput中,您还可以阅读流程getErrorStream()以查看错误。
  • 检查您的平台上是否有--rsyncable,它不是普遍可用的gzip选项。