本文主要是介绍java Winrar 解压方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
java Winrar 解压方法package util;
import java.io.File;
import java.util.ArrayList;
public class FileUtil {
private static final String linkstr = "t";
public static boolean deleteFolder(String folder) {
boolean bool = false;
try {
deleteAllFile(folder);
String filePath = folder;
filePath = filePath.toString();
File f = new File(filePath);
f.delete();
bool = true;
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
public static boolean deleteAllFile(String folederPath) {
boolean bool = false;
String filePath = null;
File file = new File(folederPath);
if (!file.exists()) {
return bool;
}
if (!file.isDirectory()) {
return bool;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (folederPath.endsWith(File.separator)) {
temp = new File(folederPath + tempList[i]);
filePath = temp.getPath();
} else {
temp = new File(folederPath + File.separator + tempList[i]);
}
if (temp.isFile()) {
boolean b = temp.delete();
if (!b) {
String msg = filePath + "文件删除失败";
bool = false;
}
}
if (temp.isDirectory()) {
deleteAllFile(folederPath + "/" + tempList[i]); //删除文件夹里面的文件
deleteFolder(folederPath + "/" + tempList[i]); //在删除空文件夹
}
}
return bool;
}
public static boolean isFileExist(String folder, String fileName) {
boolean bool = false;
bool = new File(folder, fileName).exists();
return bool;
}
public static boolean existFile(String folder){
File file = new File(folder);
if (file.isDirectory()){
return true;
}else {
return false;
}
}
public static String fileDir(String folder, String folderName) {
String path = null;
if (!FileUtil.isFileExist(folder, folderName)) {
String fullPath = folder + folderName;
File f = new File(fullPath);
f.mkdir();
path = f.getPath();
}
return path;
}
public ArrayList refreshFileList(String strPath) {
ArrayList filelist = new ArrayList();
File dir = new File(strPath);
File[] files = dir.listFiles();
if (files == null) {
filelist = null;
}
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
refreshFileList(files[i].getAbsolutePath());
} else {
String strFileName = files[i].getAbsolutePath().toLowerCase();
System.out.println("---" + strFileName);
filelist.add(files[i].getAbsolutePath());
return filelist;
}
}
return filelist;
}
}
--------------
package util;
import java.io.File;
public class ZipUtil {
public static final String password = "123456";
public static final String winrarPath = "F:\Program Files\WinRAR\WinRAR.exe";
public static boolean zip(String zipFile, String folder) {
boolean bool = false;
folder = folder + stringUtil(zipFile);
String cmd = winrarPath + " x -iext -ow -ver -- " + zipFile + " "
+ folder;
int source = zipFile.lastIndexOf("\") + 1;
String newPath = zipFile.substring(source);
if (FileUtil.isFileExist(folder, newPath)) {
bool = false;
String msg = "在" + folder + "下文件" + newPath + "已经存在";
} else {
try {
Process proc = Runtime.getRuntime().exec(cmd);
if (proc.waitFor() != 0) {
if (proc.exitValue() == 0) {
bool = false;
}
} else {
bool = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return bool;
}
public static boolean zipForPassword(String zipFile, String folder) {
boolean bool = false;
String _folder = """ + folder + stringUtil(zipFile) + "\" + """;
zipFile = """ + zipFile + """;
String cmd = winrarPath + " x -p" + password + " " + zipFile + " "
+ _folder;
int source = zipFile.lastIndexOf("\") + 1;
String newPath = zipFile.substring(source);
String folderName = stringUtil(newPath);
if (FileUtil.isFileExist(folder, folderName)) {
bool = false;
String msg = "在" + folder + "下文件" + newPath + "已经存在";
} else {
try {
Process proc = Runtime.getRuntime().exec(cmd);
if (proc.waitFor() != 0) {
if (proc.exitValue() == 0) {
bool = false;
}
} else {
bool = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return bool;
}
public static String stringUtil(String filePath) {
String fileName = new File(filePath).getName();
String fileRealName = null;
int indexStr = fileName.lastIndexOf(".");
fileRealName = fileName.substring(0, indexStr);
return fileRealName;
}
public static void main(String[] args) {
String zipFile = "G:\test\简历.rar";
String folder = "G:\test\";
boolean b = ZipUtil.zipForPassword(zipFile, folder);
// String path = folder + ZipUtil.stringUtil(zipFile);
// boolean d = ZipUtil.deleteFolder(path);
System.out.println(b);
// System.out.println(d);
}
}
。。。。。。
这篇关于java Winrar 解压方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!