進(jìn)程沖突吧?
創(chuàng)新互聯(lián)建站主要從事成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)巴東,十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18982081108
兩個(gè)命令一起執(zhí)行的應(yīng)該。。兩個(gè)rar進(jìn)程應(yīng)該可以同事啊~
樓主試試這個(gè)代碼~~
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格式壓縮包
* 對(duì)應(yīng)的是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工具默認(rèn)壓縮編碼為UTF-8編碼,
而winRAR軟件壓縮是用的windows默認(rèn)的GBK或者GB2312編碼
所以解壓縮時(shí)要制定編碼格式
*/
e.setEncoding("gbk");
e.execute();
}catch(Exception e){
throw e;
}
}
/**
* 解壓rar格式壓縮包。
* 對(duì)應(yīng)的是java-unrar-0.3.jar,但是java-unrar-0.3.jar又會(huì)用到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 根據(jù)不同的操作系統(tǒng)拿到相應(yīng)的 destDirName 和 destFileName
String compressFileName = fh.getFileNameString().trim();
String destFileName = "";
String destDirName = "";
//非windows系統(tǒng)
if(File.separator.equals("/")){
destFileName = destDir + compressFileName.replaceAll("\\\\", "/");
destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));
//windows系統(tǒng)
}else{
destFileName = destDir + compressFileName.replaceAll("/", "\\\\");
destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));
}
//2創(chuàng)建文件夾
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;
}
//根據(jù)類型,進(jìn)行相應(yīng)的解壓縮
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文件需要用到apache的commons-compress-1.0.jar,這個(gè)類的使用如下:
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;
}
}
}