java调用winrar的具体代码
时间: 2023-04-06 11:00:29 浏览: 82
以下是Java调用WinRAR的示例代码:
```java
import java.io.File;
import java.io.IOException;
public class WinRAR {
public static void main(String[] args) throws IOException {
String rarPath = "C:\\Program Files\\WinRAR\\WinRAR.exe"; // WinRAR的安装路径
String sourcePath = "C:\\Users\\user\\Desktop\\test"; // 需要压缩的文件夹路径
String targetPath = "C:\\Users\\user\\Desktop\\test.rar"; // 压缩后的文件路径
// 构造WinRAR命令
String cmd = rarPath + " a -r " + targetPath + " " + sourcePath;
// 执行WinRAR命令
Process process = Runtime.getRuntime().exec(cmd);
// 等待WinRAR执行完成
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 输出WinRAR执行结果
System.out.println(process.exitValue() == 0 ? "压缩成功" : "压缩失败");
}
}
```