package com.czp;
import java.io.File;
import java.util.Properties;
public class UnRarFile {
public static void main(String[] args) {
UnRarFile unrar = new UnRarFile();
unrar.unRarFileCurDir(new File("d:/ftpd/pm/test/*.bz2"));
}
/**
* 解压文件
*
* @param target
* @param oldFile
*/
public void unRarFile(File target, File oldFile) {
try {
if (isWindow()) {
String path = target.getParentFile().getAbsolutePath();
File tmp = target.getParentFile();
if (path.charAt(path.length() - 1) != ((char) File.separatorChar)) {
tmp = new File(path + File.separator);
if (!tmp.exists())
tmp.mkdirs();
}
String cmd2 = "C:\\Program Files\\WinRAR\\winrar.exe x -r -o+ -ibck -y "
+ oldFile + " *.* " + tmp;
Runtime rt = Runtime.getRuntime();
Process pre = rt.exec(cmd2);
if (0 != pre.waitFor()) {
pre.destroy();
}
rt.runFinalization();
System.out.println(cmd2);
} else {
System.out.println("can't get rar command abort");
}
} catch (Exception e) {
System.out.println("解压发生异常");
}
}
/**
* 解压到当前目录
*
* @param filePath
*/
public void unRarFileCurDir(File filePath) {
unRarFile(filePath, filePath);
}
/**
* 是否是window
*
* @return
*/
public boolean isWindow() {
Properties properties = System.getProperties();
String os = properties.getProperty("os.name");
if (os != null && os.contains("Windows"))
return true;
return false;
}
}
java调用winrar解压文件[rar.bz2.zip.....]
最新推荐文章于 2024-12-20 10:29:39 发布
本文介绍了一个使用Java实现的解压文件到指定目录的类,该类能够根据给定的文件路径和目标目录进行解压缩操作。特别地,它提供了针对不同操作系统(如Windows)的命令行调用,确保了跨平台的兼容性。
291

被折叠的 条评论
为什么被折叠?



