- 浏览: 1451639 次
- 性别:
- 来自: 广东
文章分类
- 全部博客 (190)
- eclipse (5)
- struts (15)
- spring (1)
- hibernate (0)
- myeclipse (3)
- java (35)
- apache (1)
- PHP (7)
- 虚拟机 (0)
- 网络安全 (3)
- 防火墙 (1)
- linux (7)
- SVN (22)
- 测试文档 (1)
- 测试经验 (0)
- 项目管理 (0)
- BUG管理工具 (3)
- 安装配置 (3)
- 性能工具 (7)
- 脚本学习 (0)
- 协议选择 (0)
- loadrunner错误处理 (0)
- 相关监控配置 (0)
- 框架的认识 (0)
- 手动关联 (0)
- 性能调优 (1)
- 负载均衡 (0)
- 代码学习 (1)
- Windows (5)
- 软件开发安全 (0)
- 考研经验 (1)
- SQL SERVER (1)
- MySQL (6)
- LVS (0)
- ORACLE (1)
- TOMCAT (0)
- 开源框架 (1)
- EOS (3)
- web (5)
- JEECMS (7)
- XML (1)
- LDAP (3)
- ehcache (1)
- Ajax (3)
- OpenSourceTools (1)
- Exception (1)
- 密码学 (1)
- os-centos (1)
- os-ubuntu (0)
- os-FreeBSD (0)
- os-Fedora (0)
- 浏览器-chrome (1)
- flex (1)
- 数据结构与算法 (0)
最新评论
-
joedan0104:
挺方便的,谢谢
JDK1.6官方下载_JDK6官方下载地址:http://www.java.net/download -
naruik:
非常感谢,不用自己找了。收藏和关注了。
JDK1.6官方下载_JDK6官方下载地址:http://www.java.net/download -
scd01234:
感谢!
JDK1.6官方下载_JDK6官方下载地址:http://www.java.net/download -
qingcheng123:
大虾,5.3这个版本有没有下载地址呀,谢谢!
EOS5.3+Tomcat5.0.28升级JDK1.5解决方案 -
1021082712:
JDK1.6官方下载_JDK6官方下载地址:http://www.java.net/download
java 关于解压rar文件 代码有点问题 帮改正
问题:目前这个代码执行第一次的时候就可以解压rar文件,但是第二次就无效了。测试可以执行一次后servlet操作,删除解压后解压后的文件,在执行,rar文件就不解压了。我用的是tomcat服务器。
文件1:UnrarServlet
代码
文件2:StreamGobbler.java
问题补充:
我之前也用 java-unrar-0.2 这种方式。但是没有弄好。
这个例子给我灵感了,谢谢。
-----------------------------------------------
public class Test {
/**
* 解压缩rar文件
*
* @param rarFileName
* @param extPlace
*/
public static boolean decompressionRarFiles(String rarFileName, String extPlace) {
boolean flag = false;
Archive archive = null;
File out = null;
File file = null;
File dir = null;
FileOutputStream os = null;
FileHeader fh = null;
String path, dirPath = "";
try {
file = new File(rarFileName);
archive = new Archive(file);
} catch (RarException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (file != null) {
file = null;
}
}
if (archive != null) {
try {
fh = archive.nextFileHeader();
while (fh != null) {
path = (extPlace + fh.getFileNameString().trim()).replaceAll("\\\\", "/");
int end = path.lastIndexOf("/");
if (end != -1) {
dirPath = path.substring(0, end);
}
try {
dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}
} catch (RuntimeException e1) {
e1.printStackTrace();
} finally {
if (dir != null) {
dir = null;
}
}
if (fh.isDirectory()) {
fh = archive.nextFileHeader();
continue;
}
out = new File(extPlace + fh.getFileNameString().trim());
try {
os = new FileOutputStream(out);
archive.extractFile(fh, os);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (RarException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
out = null;
}
}
fh = archive.nextFileHeader();
}
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
fh = null;
if (archive != null) {
try {
archive.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
flag = true;
}
return flag;
}
public static void main(String[] args) {
String absPath="D:\\apache-tomcat-6.0.20\\webapps\\upload\\test\\系统管理.rar"; //文件绝对目录
String toPath ="D:\\apache-tomcat-6.0.20\\webapps\\upload\\test\\"; //文件目录
boolean flag = new Test().decompressionRarFiles(absPath, toPath);
System.out.println("flag ---"+flag);
}
}
--------------------------------------------------------------
上边是实现方式可以看一下。
文件1:UnrarServlet
代码
- package com.jh.upload.servlet;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.tools.zip.ZipEntry;
- import org.apache.tools.zip.ZipFile;
- @SuppressWarnings("serial")
- public class UnrarServlet extends HttpServlet {
- private static String unrarCmd = "C:\\Program Files\\WinRAR\\unrar x";
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- @SuppressWarnings( { "static-access", "deprecation" })
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("gbk");
- response.setContentType("text/html;charset=gbk");
- // 设置字符编码为UTF-8, 这样支持汉字显示
- response.setCharacterEncoding("gbk");
- //文件名 传进来或者取过来
- String file = "系统管理.rar";
- PrintWriter out = response.getWriter();
- String rarFileName = request.getRealPath("/test") + "\\" + file;
- String destDir = request.getRealPath("/test");
- File f = new File(rarFileName);
- if ((!f.exists()) && (f.length() <= 0)) {
- out.println("要解压的文件不存在!<p />");
- out.println("<a href='MyJsp.jsp' >返回</a>");
- return;
- }
- unrarCmd += " " + rarFileName + " " + destDir;
- try {
- Runtime rt = Runtime.getRuntime();
- Process p = rt.exec(unrarCmd);
- StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),"ERROR");
- errorGobbler.start();
- StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(),"STDOUT");
- // kick off stdout
- outGobbler.start();
- p.waitFor();
- System.out.println("进程:--"+p.exitValue());
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- //判断目录是否存在
- String fileName = file.substring(0, file.lastIndexOf("."));
- File filePath = new File(destDir + "\\" + fileName);
- if (filePath.isDirectory()) {
- out.println("<p />解压成功!");
- } else {
- out.println("<p />解压失败,请手工解压!");
- }
- out.println("<p /><a href='MyJsp.jsp' >返回</a>");
- return;
- }
- }
package com.jh.upload.servlet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
@SuppressWarnings("serial")
public class UnrarServlet extends HttpServlet {
private static String unrarCmd = "C:\\Program Files\\WinRAR\\unrar x";
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@SuppressWarnings( { "static-access", "deprecation" })
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
response.setContentType("text/html;charset=gbk");
// 设置字符编码为UTF-8, 这样支持汉字显示
response.setCharacterEncoding("gbk");
//文件名 传进来或者取过来
String file = "系统管理.rar";
PrintWriter out = response.getWriter();
String rarFileName = request.getRealPath("/test") + "\\" + file;
String destDir = request.getRealPath("/test");
File f = new File(rarFileName);
if ((!f.exists()) && (f.length() <= 0)) {
out.println("要解压的文件不存在!<p />");
out.println("<a href='MyJsp.jsp' >返回</a>");
return;
}
unrarCmd += " " + rarFileName + " " + destDir;
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(unrarCmd);
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),"ERROR");
errorGobbler.start();
StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(),"STDOUT");
// kick off stdout
outGobbler.start();
p.waitFor();
System.out.println("进程:--"+p.exitValue());
} catch (Exception e) {
System.out.println(e.getMessage());
}
//判断目录是否存在
String fileName = file.substring(0, file.lastIndexOf("."));
File filePath = new File(destDir + "\\" + fileName);
if (filePath.isDirectory()) {
out.println("<p />解压成功!");
} else {
out.println("<p />解压失败,请手工解压!");
}
out.println("<p /><a href='MyJsp.jsp' >返回</a>");
return;
}
}
文件2:StreamGobbler.java
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- public class StreamGobbler extends Thread {
- InputStream is;
- String type;
- OutputStream os;
- StreamGobbler(InputStream is, String type) {
- this(is, type, null);
- }
- StreamGobbler(InputStream is, String type, OutputStream redirect) {
- this.is = is;
- this.type = type;
- this.os = redirect;
- }
- public void run() {
- try {
- PrintWriter pw = null;
- if (os != null)
- pw = new PrintWriter(os);
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- String line = null;
- while ((line = br.readLine()) != null) {
- if (pw != null)
- pw.println(line);
- System.out.println(type + ">" + line);
- }
- if (pw != null)
- pw.flush();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- }
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
public class StreamGobbler extends Thread {
InputStream is;
String type;
OutputStream os;
StreamGobbler(InputStream is, String type) {
this(is, type, null);
}
StreamGobbler(InputStream is, String type, OutputStream redirect) {
this.is = is;
this.type = type;
this.os = redirect;
}
public void run() {
try {
PrintWriter pw = null;
if (os != null)
pw = new PrintWriter(os);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (pw != null)
pw.println(line);
System.out.println(type + ">" + line);
}
if (pw != null)
pw.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
问题补充:
我之前也用 java-unrar-0.2 这种方式。但是没有弄好。
这个例子给我灵感了,谢谢。
-----------------------------------------------
public class Test {
/**
* 解压缩rar文件
*
* @param rarFileName
* @param extPlace
*/
public static boolean decompressionRarFiles(String rarFileName, String extPlace) {
boolean flag = false;
Archive archive = null;
File out = null;
File file = null;
File dir = null;
FileOutputStream os = null;
FileHeader fh = null;
String path, dirPath = "";
try {
file = new File(rarFileName);
archive = new Archive(file);
} catch (RarException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (file != null) {
file = null;
}
}
if (archive != null) {
try {
fh = archive.nextFileHeader();
while (fh != null) {
path = (extPlace + fh.getFileNameString().trim()).replaceAll("\\\\", "/");
int end = path.lastIndexOf("/");
if (end != -1) {
dirPath = path.substring(0, end);
}
try {
dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}
} catch (RuntimeException e1) {
e1.printStackTrace();
} finally {
if (dir != null) {
dir = null;
}
}
if (fh.isDirectory()) {
fh = archive.nextFileHeader();
continue;
}
out = new File(extPlace + fh.getFileNameString().trim());
try {
os = new FileOutputStream(out);
archive.extractFile(fh, os);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (RarException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
out = null;
}
}
fh = archive.nextFileHeader();
}
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
fh = null;
if (archive != null) {
try {
archive.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
flag = true;
}
return flag;
}
public static void main(String[] args) {
String absPath="D:\\apache-tomcat-6.0.20\\webapps\\upload\\test\\系统管理.rar"; //文件绝对目录
String toPath ="D:\\apache-tomcat-6.0.20\\webapps\\upload\\test\\"; //文件目录
boolean flag = new Test().decompressionRarFiles(absPath, toPath);
System.out.println("flag ---"+flag);
}
}
--------------------------------------------------------------
上边是实现方式可以看一下。
发表评论
-
java中split要注意的问题
2010-12-31 18:06 1507java中split要注意的问 ... -
tomcat6.0 中怎么配置admin包 [转载]
2010-11-18 15:05 2170tomcat6.0 中怎么配置admin包 [转载] ... -
TOMCAT "Access to the requested resource has been denied" 的解决办法
2010-11-18 14:25 2981TOMCAT "Access to the r ... -
new Date() 时间与系统时间不相同 如何解决?
2010-11-02 10:55 2781java时区问题 问题描述:Window XP ... -
项目报错java.net.bindexception: address already in use: jvm_bind:808
2010-10-19 09:19 1620项目报错java.net.bindexception: add ... -
安装与设置JDK
2010-07-25 15:43 1878安装与设置JDK Sun JDK的安装基本 ... -
【插件控】我的Chrome插件和其他一些推荐
2010-07-22 12:07 2178【插件控】我的Chrome ... -
jdk 6 update 20
2010-06-29 22:06 1274https://cds.sun.com/is ... -
java中如何获取某个范围内的随机数
2010-06-29 14:45 3657java中如何获取某个范围内的随机数 ... -
TestNG 解决方案
2010-06-25 18:39 1680TestNG 解决方案 (2008-07-31 1 ... -
Java DTO(DataTransferObject)
2010-06-24 17:57 1592<!-- [if gte mso 9]>&l ... -
Java DTO(DataTransferObject) chendq@startimes.com.cn 分布式系统
2010-06-24 16:43 1309<!-- [if !mso]> <styl ... -
PO BO VO DTO POJO DAO概念及其作用
2010-06-24 16:39 1015PO: persistant object持久对象 最形象 ... -
J2SE 5.0 提供的简单的批注类型
2010-06-22 11:53 1444J2SE 5.0 提供的简单的批注类型 ... -
用java解压缩rar文件 de.innosystec.unrar解压缩rar文件
2010-05-21 19:35 3585用java解压缩rar文件 2009-10-23 14:11 ... -
关于java解压zip与rar的问题
2010-05-21 17:33 2054关于java解压zip与rar的问题 这两天, ... -
java实现zip与unzip
2010-05-21 16:27 1631java实现zip与unzip 关键字: 转载自http:/ ... -
转 Java实现批量PNG图片透明合成2009-06-02分类:学习java
2010-05-20 20:34 1870转 Java实现批量PNG图片透明合成 2009-06- ... -
jdk 1.5新特性
2010-05-18 18:39 1080jdk 1.5新特性说明 2010年04月13日 星期二 1 ... -
jdk1.6新特性
2010-05-18 18:37 2607jdk1.6新特性 2010年04月13日 星期二 15:3 ...
相关推荐
一个用java实现解压RAR压缩文件的例子,由于RAR的压缩算法被专利,所以借助了第三方的包(此文件里包含三方包)。
java 解压 rar 文件,压缩包为测试工程,包含 java-unrar-0.3.jar
java程序,压缩与解压rar文件. java程序,压缩与解压rar文件.
使用Java解压文件 支持RAR5,Zip,7z ,代码只实现了这个三种,但是要想支持更多的可以查看代码。 支持跨平台。 以前的通用java解压都是坑人的,无语了。
Java解压RAR文件,压缩包内包含源码、Jar包、依赖包和示例程序,也可以从http://code.google.com/p/java-unrar/downloads/list这里下载支持密码的unrar项目(包括源码和jar包),其依赖包也很容易搜到。 RAR 密码
java上传文件 实时获取上传进度 上传RAR,ZIP 并指定解压路劲 上传之后会按照路劲进行解压
使用java解压带密码的rar压缩文件,解压目录下所有的rar文件
基于java的p2p文件传输代码.rar基于java的p2p文件传输代码.rar基于java的p2p文件传输代码.rar基于java的p2p文件传输代码.rar基于java的p2p文件传输代码.rar基于java的p2p文件传输代码.rar基于java的p2p文件传输代码....
Java批量解压RAR或zip文件,压缩包内包含包括源码和jar包。 如发现其他人或组织有商用目的,必追究其法律责任。
NULL 博文链接:https://lbovinl.iteye.com/blog/2389307
用java实现rar文件的压缩解压。
java 解压zip,rar文件 ,java 解压缩包,把压缩包的文件及文件夹的子文件全部压到指定目录
NULL 博文链接:https://hogwartsrow.iteye.com/blog/1853355
我这个资源是也是从网上下的,下下来之后在单个测试类里面解压rar是没问题的,但是在Action里面调就抛java.lang.OutOfMemoryError: Java heap space,所以自己换了种解压rar的方法,以前的方法注释掉了。action里面...
Java解压RAR文件,压缩包内包含源码、Jar包、依赖包和示例程序,也可以从http://code.google.com/p/java-unrar/downloads/list这里下载支持密码的unrar项目(包括源码和jar包),其依赖包也很容易搜到。
本篇文章主要介绍了详解Java无需解压直接读取Zip文件和文件内容,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
jUnrar解压rar文件例子 以及java-unrar-0.3.jar类库
java项目中常使用到的工具类:zip压缩解压缩、rar解压(有密码的文件)、gz解压、FTP上传与下载
mac 上解压rar文件超好用免费的
java rar解压工具类 // 第一个参数是需要解压的压缩包路径,第二个参数参考JdkAPI文档的RandomAccessFile //r代表以只读的方式打开文本,也就意味着不能用write来操作文件