之前一直在用java-unrar-0.3.jar来解压rar文件包,一直很正常,但是今天来了个中文名字的包,类似于这样的结构:
压缩包.rar,这个压缩包里面还有个文件夹也叫压缩包,在解压这个压缩包的时候出现了乱码,研究了好久,已经解决,现与大家分享下。
原来解压文件的代码:
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Enumeration;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
- import de.innosystec.unrar.Archive;
- import de.innosystec.unrar.exception.RarException;
- import de.innosystec.unrar.rarfile.FileHeader;
- /**
- * 对rar或者zip进行解压缩
- *
- * @author yKF41624
- *
- */
- public class Decompress {
- private static String fileName = "";
- /**
- * 对rar文件解压
- *
- * @param rarFileName
- * @param extPlace
- * @return
- */
- public static boolean unrarFiles(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) {
- fileName = fh.getFileNameString().trim();
- path = (extPlace + fileName).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 + fileName);
- 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;
- }
- }
以上代码解压无中文的压缩包是很正常的,经过调试发现,是在的文件名的时候如果含有中文那么得到的就是乱码,即是代码中的第54行:
- fileName = fh.getFileNameString().trim();
最开始我查了下API,我发现有一个getFileNameW()方法,就尝试着用这个方法得到要解压的文件名,发现如果有中文一下就成功了,于是我把上面的方法替换成了这个,可是没想到如果来的压缩包没有英文,那么就得不到了,于是这就要加个判断。于是我通过正则表达式判断文件名中是否含有中文,如果有中文,调用getFileNameW(),如果没有中文就调用getFileNameString()方法,就解决了。
附上判断字符中是否存在中文的方法:
- public static boolean existZH(String str) {
- String regEx = "[\\u4e00-\\u9fa5]";
- Pattern p = Pattern.compile(regEx);
- Matcher m = p.matcher(str);
- while (m.find()) {
- return true;
- }
- return false;
- }
于是,只需要将原来的代码第54行修改为如下代码即可:
- fileName= fh.getFileNameW().trim();
- if(!existZH(fileName)){
- fileName = fh.getFileNameString().trim();
- }
最后在附上所用jar包。