java Winrar 解压方法

2024-01-26 09:58
文章标签 java 方法 解压 winrar

本文主要是介绍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 解压方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/646437

相关文章

Java中对象的创建和销毁过程详析

《Java中对象的创建和销毁过程详析》:本文主要介绍Java中对象的创建和销毁过程,对象的创建过程包括类加载检查、内存分配、初始化零值内存、设置对象头和执行init方法,对象的销毁过程由垃圾回收机... 目录前言对象的创建过程1. 类加载检查2China编程. 分配内存3. 初始化零值4. 设置对象头5. 执行

SpringBoot整合easy-es的详细过程

《SpringBoot整合easy-es的详细过程》本文介绍了EasyES,一个基于Elasticsearch的ORM框架,旨在简化开发流程并提高效率,EasyES支持SpringBoot框架,并提供... 目录一、easy-es简介二、实现基于Spring Boot框架的应用程序代码1.添加相关依赖2.添

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Python使用Pandas对比两列数据取最大值的五种方法

《Python使用Pandas对比两列数据取最大值的五种方法》本文主要介绍使用Pandas对比两列数据取最大值的五种方法,包括使用max方法、apply方法结合lambda函数、函数、clip方法、w... 目录引言一、使用max方法二、使用apply方法结合lambda函数三、使用np.maximum函数

SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程

《SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程》本文详细介绍了如何在虚拟机和宝塔面板中安装RabbitMQ,并使用Java代码实现消息的发送和接收,通过异步通讯,可以优化... 目录一、RabbitMQ安装二、启动RabbitMQ三、javascript编写Java代码1、引入

spring-boot-starter-thymeleaf加载外部html文件方式

《spring-boot-starter-thymeleaf加载外部html文件方式》本文介绍了在SpringMVC中使用Thymeleaf模板引擎加载外部HTML文件的方法,以及在SpringBoo... 目录1.Thymeleaf介绍2.springboot使用thymeleaf2.1.引入spring

Qt 中集成mqtt协议的使用方法

《Qt中集成mqtt协议的使用方法》文章介绍了如何在工程中引入qmqtt库,并通过声明一个单例类来暴露订阅到的主题数据,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一,引入qmqtt 库二,使用一,引入qmqtt 库我是将整个头文件/源文件都添加到了工程中进行编译,这样 跨平台

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满