问题:目前这个代码执行第一次的时候就可以解压rar文件,但是第二次就无效了。测试可以执行一次后servlet操作,删除解压后解压后的文件,在执行,rar文件就不解压了。我用的是tomcat服务器。
文件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;
}
}
文件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();
}
}
}
问题补充:我之前也用 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实现解压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来操作文件