资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java解压rar代码 java解压rar的工具类

java解压RAR文件的问题

进程冲突吧?

创新互联建站主要从事成都网站设计、做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务巴东,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

两个命令一起执行的应该。。两个rar进程应该可以同事啊~

在LINUX下 用JAVA如何解压rar文件

楼主试试这个代码~~

package decompress;

import java.io.File;

import java.io.FileOutputStream;

import org.apache.tools.ant.Project;

import org.apache.tools.ant.taskdefs.Expand;

import de.innosystec.unrar.Archive;

import de.innosystec.unrar.rarfile.FileHeader;

public class DeCompressUtil {

/**

* 解压zip格式压缩包

* 对应的是ant.jar

*/

private static void unzip(String sourceZip,String destDir) throws Exception{

try{

Project p = new Project();

Expand e = new Expand();

e.setProject(p);

e.setSrc(new File(sourceZip));

e.setOverwrite(false);

e.setDest(new File(destDir));

/*

ant下的zip工具默认压缩编码为UTF-8编码,

而winRAR软件压缩是用的windows默认的GBK或者GB2312编码

所以解压缩时要制定编码格式

*/

e.setEncoding("gbk");

e.execute();

}catch(Exception e){

throw e;

}

}

/**

* 解压rar格式压缩包。

* 对应的是java-unrar-0.3.jar,但是java-unrar-0.3.jar又会用到commons-logging-1.1.1.jar

*/

private static void unrar(String sourceRar,String destDir) throws Exception{

Archive a = null;

FileOutputStream fos = null;

try{

a = new Archive(new File(sourceRar));

FileHeader fh = a.nextFileHeader();

while(fh!=null){

if(!fh.isDirectory()){

//1 根据不同的操作系统拿到相应的 destDirName 和 destFileName

String compressFileName = fh.getFileNameString().trim();

String destFileName = "";

String destDirName = "";

//非windows系统

if(File.separator.equals("/")){

destFileName = destDir + compressFileName.replaceAll("\\\\", "/");

destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));

//windows系统

}else{

destFileName = destDir + compressFileName.replaceAll("/", "\\\\");

destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));

}

//2创建文件夹

File dir = new File(destDirName);

if(!dir.exists()||!dir.isDirectory()){

dir.mkdirs();

}

//3解压缩文件

fos = new FileOutputStream(new File(destFileName));

a.extractFile(fh, fos);

fos.close();

fos = null;

}

fh = a.nextFileHeader();

}

a.close();

a = null;

}catch(Exception e){

throw e;

}finally{

if(fos!=null){

try{fos.close();fos=null;}catch(Exception e){e.printStackTrace();}

}

if(a!=null){

try{a.close();a=null;}catch(Exception e){e.printStackTrace();}

}

}

}

/**

* 解压缩

*/

public static void deCompress(String sourceFile,String destDir) throws Exception{

//保证文件夹路径最后是"/"或者"\"

char lastChar = destDir.charAt(destDir.length()-1);

if(lastChar!='/'lastChar!='\\'){

destDir += File.separator;

}

//根据类型,进行相应的解压缩

String type = sourceFile.substring(sourceFile.lastIndexOf(".")+1);

if(type.equals("zip")){

DeCompressUtil.unzip(sourceFile, destDir);

}else if(type.equals("rar")){

DeCompressUtil.unrar(sourceFile, destDir);

}else{

throw new Exception("只支持zip和rar格式的压缩包!");

}

}

}

java 解压rar文件

Java 解压rar文件需要用到apache的commons-compress-1.0.jar,这个类的使用如下:

package cn.myapps.util.pdf;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.util.Enumeration;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;

import org.apache.commons.compress.archivers.zip.ZipFile;

public class UnzipFile {

public static void main(String[] args) {

try {

UnzipFile.unZip(new File("D:/你我他.zip"), "D:/upzip/");

} catch (Exception e) {

e.printStackTrace();

}

}

public static void unZip(File zip, String root) throws Exception {

try {

ZipFile zipFile = new ZipFile(zip, "GBK");

Enumeration e = zipFile.getEntries();

byte ch[] = new byte[256];

while (e.hasMoreElements()) {

ZipArchiveEntry zipEntry = (ZipArchiveEntry) e.nextElement();

String temp = zipEntry.getName();

System.out.println("unziping " + zipEntry.getName());

File zfile = new File(root + temp);

File fpath = new File(zfile.getParentFile().getPath());

if (zipEntry.isDirectory()) {

if (!zfile.exists())

zfile.mkdirs();

} else {

if (!fpath.exists())

fpath.mkdirs();

FileOutputStream fouts = new FileOutputStream(zfile);

InputStream in = zipFile.getInputStream(zipEntry);

int i;

while ((i = in.read(ch)) != -1)

fouts.write(ch, 0, i);

fouts.close();

in.close();

}

}

} catch (Exception e) {

System.err.println("Exception from ZipUtil - unZip() : "

+ e.getMessage());

e.printStackTrace(System.err);

throw e;

}

}

}


文章标题:java解压rar代码 java解压rar的工具类
网址分享:http://cdkjz.cn/article/dohphjs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220