java unrar 乱码_JAVA使用 java-unrar-0.3.jar 解压rar,并且解决中文乱码

有个项目需要解压rar,上网搜索一下大家都在用  java-unrar-0.3.jar ,于是写了一个util方法解压rar

public static void unrar(String srcPath,String unrarPath,boolean incRarDir) throws RarException, IOException{

unrar(new File(srcPath), unrarPath, incRarDir);

}

public static void unrar(File srcFile,String unrarPath,boolean incRarDir) throws RarException, IOException{

if(StringUtil.isEmptyString(unrarPath)){

unrarPath = srcFile.getParentFile().getPath();

}

if(incRarDir){

File dir = new File(unrarPath+"/"+FileUtil.getFileNameWithoutType(srcFile.getName()));

dir.mkdir();

unrarPath = dir.getPath();

}

System.out.println("unrar file to :"+unrarPath);

FileOutputStream fileOut;

File file;

Archive rarfile = new Archive(srcFile);

FileHeader entry = rarfile.nextFileHeader();

while(entry!=null){

String entrypath = entry.getFileNameString().trim();

entrypath=entrypath.replaceAll("\\\\", "/");

file = new File(unrarPath+"/"+entrypath);

System.out.println("unrar entry file :"+file.getPath());

if(entry.isDirectory()){

file.mkdirs();

}else{

File parent = file.getParentFile();

if(parent!=null && !parent.exists()){

parent.mkdirs();

}

fileOut = new FileOutputStream(file);

rarfile.extractFile(entry, fileOut);

fileOut.close();

}

entry = rarfile.nextFileHeader();

}

rarfile.close();

}

经过验证,解压包中如果没有中文名是正常的,但是一旦带有中文名就出现乱码了,看了一下FileHeader类的方法提示,发现有个isUnicode的方法,估计是用来判断文件名是不是Unicode字符的,而且还有一个方法获得压缩包里的文件名的 getFileNameW,经过反复试验,应该这样写才能解决乱码问题

String entrypath = "";

if(entry.isUnicode()){//Unicode文件名使用getFileNameW

entrypath = entry.getFileNameW().trim();

}else{

entrypath = entry.getFileNameString().trim();

}

乱码问题解决

最后附上正确的使用源码和 java-unrar-0.3.jar

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import de.innosystec.unrar.Archive;

import de.innosystec.unrar.exception.RarException;

import de.innosystec.unrar.rarfile.FileHeader;

public class RARUtil {

public static void unrar(String srcPath,String unrarPath,boolean incRarDir) throws RarException, IOException{

unrar(new File(srcPath), unrarPath, incRarDir);

}

public static void unrar(File srcFile,String unrarPath,boolean incRarDir) throws RarException, IOException{

if(StringUtil.isEmptyString(unrarPath)){

unrarPath = srcFile.getParentFile().getPath();

}

if(incRarDir){

File dir = new File(unrarPath+"/"+FileUtil.getFileNameWithoutType(srcFile.getName()));

dir.mkdir();

unrarPath = dir.getPath();

}

System.out.println("unrar file to :"+unrarPath);

FileOutputStream fileOut;

File file;

Archive rarfile = new Archive(srcFile);

FileHeader entry = rarfile.nextFileHeader();

while(entry!=null){

String entrypath = "";

if(entry.isUnicode()){

entrypath = entry.getFileNameW().trim();

}else{

entrypath = entry.getFileNameString().trim();

}

entrypath=entrypath.replaceAll("\\\\", "/");

file = new File(unrarPath+"/"+entrypath);

System.out.println("unrar entry file :"+file.getPath());

if(entry.isDirectory()){

file.mkdirs();

}else{

File parent = file.getParentFile();

if(parent!=null && !parent.exists()){

parent.mkdirs();

}

fileOut = new FileOutputStream(file);

rarfile.extractFile(entry, fileOut);

fileOut.close();

}

entry = rarfile.nextFileHeader();

}

rarfile.close();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值