- 浏览: 326757 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
y_715:
document.getElementById("e ...
JSTL EC TABLE extremeTable 学习 -
wangyj0898:
学习了。。。
display table 标签用法 -
hejiajie2008:
没有图啊
图解myeclipse把maven普通java项目变成web项目的全程操作 -
zhangyu_2100:
[align=center][/align][color=br ...
request.getRealPath()的替代方法 -
senhui19:
LZ写的真不错呀,现在开发中都经常是用DetachedCrit ...
Hibernate的Criteria用法总结
通过使用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);
}
}
/**
* 这个类是用来做为解压缩时,对文件的一些操作
* 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;
}
}
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;
}
}
发表评论
-
智力题
2010-08-27 10:56 1786A说:是B偷吃的,B说:是D偷吃的。C说;我没有偷吃。D说:B ... -
experience
2009-12-28 12:05 11511.报错信息 servlet-mapping配置的问题200 ... -
JDBC高级应用二
2009-12-28 11:33 983JDBC高级应用(二)本来想继续谈JDBC的高级连结方式,事务 ... -
在JAVA文件中获取该项目的相对路径
2009-11-12 14:36 14021.基本概念的理解 绝对路径:绝对路径就是你的主页上的文 ... -
明明白白Unsupported major.minor version 49.0的错误
2009-10-26 17:19 956一:要解决的问题 我们在尝鲜 JDK1.5 的时候,相信不少人 ... -
log4j详解
2009-09-02 13:04 833log4j.properties 使用 一.参数意义说明 输出 ... -
java 文件读取 路径问题
2009-08-25 15:23 15181.如何获得当前文件路径 常用: 字符串类型:System ... -
图解myeclipse把maven普通java项目变成web项目的全程操作
2009-08-05 20:06 3944图解myeclipse把maven普通java项目变成web项 ... -
浅谈java输入输出流(二)
2009-07-09 18:05 2386导读: stream代表的是任何有能力产出数据的数据源,或 ... -
浅谈java输入输出流(一)
2009-07-09 18:01 3089Java语言的输入输出功能是十分强大而灵活的,美中不足 ... -
常用Java开源库(新手必看)
2008-12-15 12:46 1752Jakarta common: Commons Logging ... -
浅谈Java内部类的四个应用场景
2008-12-15 12:44 920Java内部类是Java言语的一个很重要的概念,《Java编程 ... -
使用 HttpClient 和 HtmlParser 实现简易爬虫
2008-12-02 20:51 1425这篇文章介绍了 HtmlParse ... -
htmlparser使用指南
2008-11-28 13:10 2003转载:http://www.yeeach.com/2008/0 ... -
file的getPath getAbsolutePath和getCanonicalPath的不同
2008-09-16 09:12 1084file的这几个取得path的 ... -
window.event的属性
2008-08-07 18:03 1618<script language="JavaS ... -
十个习惯让你精通新的开发技术(转)
2008-05-29 17:44 1038这篇文章,是从我的《 ... -
java汉字转换为拼音
2008-05-09 17:14 2025import java.util.Iterator;impor ... -
把JSP放到WEB-INF后以保护JSP源代码
2008-04-17 08:52 4615本人在看《J2EE核心模式》(“Core J2ee Patte ... -
关于VO、PO的理解-java的(PO,VO,TO,BO,DAO,POJO)解释
2008-03-30 17:00 1607O/R Mapping 是 Object Relational ...
相关推荐
java调用winrar生成压缩文件路径有空格问题,今日作了一个项目需要把文件压缩成rar压缩包,一边客户下载,但是路径很有可能存在空格,造成winrar命令把它看成两个元素,无法解析出正确的路径,仔细研究下,终于解决...
WinRAR压缩包密码破解软件,我用了下,破解了个试试,很好用,绝对可以破解。下了用用吧,不会后悔的
NULL 博文链接:https://thinktothings.iteye.com/blog/1436303
字典暴力破解winrar压缩包密码工具, 设置教程更新.zip
压缩后效果等同于用winrar给压缩包加密码 时间紧迫,暂时存在中文文件夹名称乱码问题 不影响文件夹内各类型文件内容 实现方法见功能说明txt文档,只需传入文件夹路径,Zip文件路径,密码 本人JAVA实习生,因业务...
c# 利用WinRAR压缩解压缩文件 本例运用c#调用WinRAR进行压缩和解压缩
这是一个仿winrar的解压缩程序的源码,使用了Unrar.dll来实现解压缩功能,目前改dll的版本是3.8,还没有实现压缩功能。使用前,请把压缩包中的unrar.dll 拷贝到系统目录(windows/system32)目录下方可使用。 这个代码下载...
WinRAR压缩解压缩工具(破解版)WinRAR压缩解压缩工具(破解版)
压缩后效果等同于用winrar给压缩包加密码 时间紧迫,暂时存在中文文件夹名称乱码问题 不影响文件夹内各类型文件内容 实现方法见功能说明txt文档,只需传入文件夹路径,Zip文件路径,密码 本人JAVA实习生,因...
1,设置压缩密码(设置解压密码或压缩文件打开密码,默认没有密码) 2,设置五种压缩方式(存储、最快、快速、标准、较好、最优,默认为标准) 3,是否排除当前压缩目录层级(默认为排除) 4,设置固实压缩(将待压缩文件当作连串...
有很多压缩解压缩的算法,网上也有很多程序,但大多不支持压缩多个文件,本程序调用WinRAR安装后的Rar.exe程序,没什么技术含量,可作为系统调用的练习吧。(内附使用说明)
非常好用的一款家压缩软件,比好压功能更强大~~
去除winrar压缩包密码
winrar密码清除工具是一个简单、易用,且功能强大的rar压缩包密码清除工具,即使不会使用的人也仅需1分钟便能学会使用。winrar密码清除工具支持所有版本的winrar/rar压缩包,并且winrar密码清除工具使用暴力破解、带...
C#利用Winrar来压缩和解压缩 C#利用Winrar来压缩和解压缩
WinRAR 解压缩软件
现在重新发布一个使用 UnRar.dll进行解压缩、压缩文件测试及注释信息提取等功能的源代码。这个 UnRar.dll目前为最新版,版本是 3.80.2.166,可以向下兼容识别所有Winrar格式的压缩文件。这个示例就是利用这个动态...
WinRAR是一款功能强大的压缩和解压缩软件,主要用于备份数据、缩减电子邮件附件的大小以及解压缩从互联网上下载的RAR、ZIP等文件。它支持新建RAR及ZIP格式的文件,让用户能够轻松管理各种类型的压缩文件。 WinRAR在...
WinRAR软件可以用来解压、压缩文件后缀为 .rar ,.zip 的文件。
winrar密码清除工具是一个简单、易用,且功能强大的rar压缩包密码清除工具,即使不会使用的人也仅需1分钟便能学会使用。winrar密码清除工具支持所有版本的winrar/rar压缩包,并且winrar密码清除