package com.sunz.fileUpload;
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 ";
/**
* 将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);
} 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);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@SuppressWarnings("static-access")
public static void main(String[] ags){
try{
RarToFile rtf = new RarToFile();
rtf.unRARFile("d:/ss.rar", "d:/");
}catch(Exception ex){System.out.println(ex.getMessage());}
System.out.println("成功!");
}
}
Java压缩与解压rar文件
本文介绍了一个简单的Java程序,用于实现RAR文件的压缩和解压缩功能。该程序利用WinRAR的命令行工具完成操作,适用于需要在Java应用中集成文件压缩解压功能的场景。


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



