導航:首頁 > 編程語言 > javaunrar

javaunrar

發布時間:2024-11-03 20:16:55

1. java unrar 中crcerror的問題

請問您是抄要使用unrar這個軟體,通過java來調用並解壓文件是么?
您做的是windows平台的開發吧。請問具體的rar文件的文件類型是什麼呢?很有可能您是解壓了不同格式的rar文件比如是不是有可能winrar與unrar的壓縮演算法不同,從而導致unrar解壓出現錯誤呢?您可以嘗試解壓一個用unrar壓縮的內容為dll 或者 exe的文件試試

2. 在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格式的壓縮包!");
}
}
}

閱讀全文

與javaunrar相關的資料

熱點內容
iphone6s單手模式 瀏覽:79
vivo怎麼找刪除的app軟體 瀏覽:852
360裝機大師怎麼用教程 瀏覽:168
高一編程語言是什麼 瀏覽:421
phpword插入圖片 瀏覽:261
數控編程s300什麼意思 瀏覽:871
linuxab壓力測試 瀏覽:818
編程語言為什麼是c 瀏覽:797
悅me只能網關密碼錯誤 瀏覽:844
三星交集工具 瀏覽:939
資料庫中怎麼復製表結構 瀏覽:417
戴爾win10平板裝系統嗎 瀏覽:816
編程的變數名有哪些 瀏覽:124
360版本海島奇兵下載 瀏覽:370
常州ug數控編程培訓哪個學校好 瀏覽:802
資料庫的不等於怎麼寫 瀏覽:664
qq關閉送禮物動畫 瀏覽:128
京東健康碼在哪個文件夾里 瀏覽:891
數據線黑了怎麼消除 瀏覽:883
iphone6快捷鎖屏 瀏覽:55

友情鏈接