winrar java_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);

}

}

。。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值