public class RarToFile {
/*
* cmd 压缩与解压缩命令
*/
private static String rarCmd = "C:\\Program Files\\WinRAR\\Rar.exe a ";
private static String unrarCmd = "C:\\Program Files\\WinRAR\\UnRar x ";
public static void main(String[] args) throws Exception {
RARFile("RARRemote", "E:\\RemoteXml.xml", "E:\\");
}
/**
* 将1个文件压缩成RAR格式
* rarName 压缩后的压缩文件名(不包含后缀)
* fileName 需要压缩的文件名(必须包含路径)
* destDir 压缩后的压缩文件存放路径
*/
public static void RARFile(String rarName, String fileName, String destDir) {
rarCmd += destDir + rarName + ".rar " + fileName;
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(rarCmd);
if (p.waitFor() == 0) {
System.out.println("压缩成功");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 将1个RAR文件解压
* rarFileName 需要解压的RAR文件(必须包含路径信息以及后缀)
* destDir 解压后的文件放置目录
*/
public static void unRARFile(String rarFileName, String destDir) {
unrarCmd += rarFileName + " " + destDir;
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(unrarCmd);
if (p.waitFor() == 0) {
System.out.println("解压成功");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
java 实现 rar压缩和解压
最新推荐文章于 2024-07-12 00:14:45 发布