`

JAVA使用winrar解压缩和解带有密码的压缩包的一个类

    博客分类:
  • java
阅读更多

 

     通过使用winrar这个工具对文件进行操作。唯一不大好的地方就是需要客户安装winrar,或者在打包成web时候需要将winrar这个软件加载过去

Java代码
package com.hfjh.common;

/**
* 这个类是用来做为解压缩时,对文件的一些操作
* yfyang 080411
*/
import java.io.File;

public class ZipUtil {

public static final String password = "123456";
public static final String winrarPath = "C:\\Program Files\\WinRAR\\WinRAR.exe";

/**
* 将指定的压缩文件解压缩到指定的路径下 解压缩后在指定的路径下生成一个以压缩文件名的文件夹,文件下即为压缩文件的文件
*
* @param zipFile
* 压缩文件路径
* @param folder
* 要解压到何处的路径
* @return
*/
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;
}

/**
* 解压带有密码的压缩文件 默认密码为123456,如果需要更改则只要修改password的值
*
* @param zipFile
* @param folder
* @return
*/
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;
}



/**
* String的方法工具,主要是针对路径中取得压缩文件的名称,不包括后缀名
*
* @param str
* @return 压缩文件的名称
*/
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;
}

/**
* 测试类
*
* @param args
*/
public static void main(String[] args) {
String zipFile = "D:\\企业征信\\2340720080229.rar";
String folder = "D:\\企业征信\\";
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 代码(FileUtil):
package com.hfjh.common.report;

import java.io.File;
import java.util.ArrayList;

/**
* 文件操作类
* 包括产生临时文件夹,删除临时文件夹等操作方法
* @author poplar
*
*/
public class FileUtil {

private static final String linkstr = "\t";

/**
* 删除在解压缩时产生的临时文件夹
* @param folder 临时文件夹路径
* @return true为删除成功,false为失败
*/
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;
}

/**
* 删除文件夹下所有内容
* @param folederPath 文件夹完整路径
* @return
*/
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;
}

/**
* 判断文件是否存在
*
* @param fileName
* @return
*/
public static boolean isFileExist(String folder, String fileName) {
boolean bool = false;
bool = new File(folder, fileName).exists();
return bool;
}

/**
* 测试文件夹是否存在
* @param folder
* @return
*/
public static boolean existFile(String folder){
File file = new File(folder);
if (file.isDirectory()){
return true;
}else {
return false;
}
}

/**
* 对文件路径进行处理,主要是在传递过来的路径下创建一个文件夹
*
* @param folder
* @return 返回新的路径名
*/
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;
}

/**
* 遍历文件夹
* @param file
*/
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;
}
}

 

分享到:
评论

相关推荐

    java调用winrar生成压缩文件路径有空格问题

    java调用winrar生成压缩文件路径有空格问题,今日作了一个项目需要把文件压缩成rar压缩包,一边客户下载,但是路径很有可能存在空格,造成winrar命令把它看成两个元素,无法解析出正确的路径,仔细研究下,终于解决...

    破解WinRAR压缩包密码绿色软件

    WinRAR压缩包密码破解软件,我用了下,破解了个试试,很好用,绝对可以破解。下了用用吧,不会后悔的

    windows 下java调用winrar压缩文件为rar 格式

    NULL 博文链接:https://thinktothings.iteye.com/blog/1436303

    winrar压缩包密码破解

    字典暴力破解winrar压缩包密码工具, 设置教程更新.zip

    Java实现对文件夹的加密码压缩(绝对可用)

    压缩后效果等同于用winrar给压缩包加密码 时间紧迫,暂时存在中文文件夹名称乱码问题 不影响文件夹内各类型文件内容 实现方法见功能说明txt文档,只需传入文件夹路径,Zip文件路径,密码 本人JAVA实习生,因业务...

    c# 利用WinRAR压缩解压缩文件

    c# 利用WinRAR压缩解压缩文件 本例运用c#调用WinRAR进行压缩和解压缩

    VB版仿WinRar解压缩源代码(修正版)

    这是一个仿winrar的解压缩程序的源码,使用了Unrar.dll来实现解压缩功能,目前改dll的版本是3.8,还没有实现压缩功能。使用前,请把压缩包中的unrar.dll 拷贝到系统目录(windows/system32)目录下方可使用。 这个代码下载...

    WinRAR压缩解压缩工具(破解版)

    WinRAR压缩解压缩工具(破解版)WinRAR压缩解压缩工具(破解版)

    JAVA实现对文件夹“加密码压缩”

    压缩后效果等同于用winrar给压缩包加密码 时间紧迫,暂时存在中文文件夹名称乱码问题 不影响文件夹内各类型文件内容 实现方法见功能说明txt文档,只需传入文件夹路径,Zip文件路径,密码 本人JAVA实习生,因...

    java 基于WinRAR6.02封装的压缩及分卷压缩工具

    1,设置压缩密码(设置解压密码或压缩文件打开密码,默认没有密码) 2,设置五种压缩方式(存储、最快、快速、标准、较好、最优,默认为标准) 3,是否排除当前压缩目录层级(默认为排除) 4,设置固实压缩(将待压缩文件当作连串...

    C++编写的压缩解压缩程序(调用WinRAR的命令行程序)

    有很多压缩解压缩的算法,网上也有很多程序,但大多不支持压缩多个文件,本程序调用WinRAR安装后的Rar.exe程序,没什么技术含量,可作为系统调用的练习吧。(内附使用说明)

    WINRAR解压缩软件

    非常好用的一款家压缩软件,比好压功能更强大~~

    去除winrar压缩包密码

    去除winrar压缩包密码

    winrar密码清除工具 V3.2

    winrar密码清除工具是一个简单、易用,且功能强大的rar压缩包密码清除工具,即使不会使用的人也仅需1分钟便能学会使用。winrar密码清除工具支持所有版本的winrar/rar压缩包,并且winrar密码清除工具使用暴力破解、带...

    C#利用Winrar压缩解压缩

    C#利用Winrar来压缩和解压缩 C#利用Winrar来压缩和解压缩

    WinRAR 解压缩软件

    WinRAR 解压缩软件

    VB版仿WinRar解压缩源代码

    现在重新发布一个使用 UnRar.dll进行解压缩、压缩文件测试及注释信息提取等功能的源代码。这个 UnRar.dll目前为最新版,版本是 3.80.2.166,可以向下兼容识别所有Winrar格式的压缩文件。这个示例就是利用这个动态...

    winrar解压缩文件工具

    WinRAR是一款功能强大的压缩和解压缩软件,主要用于备份数据、缩减电子邮件附件的大小以及解压缩从互联网上下载的RAR、ZIP等文件。它支持新建RAR及ZIP格式的文件,让用户能够轻松管理各种类型的压缩文件。 WinRAR在...

    WinRAR 压缩包解压缩软件

    WinRAR软件可以用来解压、压缩文件后缀为 .rar ,.zip 的文件。

    winrar密码清除工具

    winrar密码清除工具是一个简单、易用,且功能强大的rar压缩包密码清除工具,即使不会使用的人也仅需1分钟便能学会使用。winrar密码清除工具支持所有版本的winrar/rar压缩包,并且winrar密码清除

Global site tag (gtag.js) - Google Analytics