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行:

  1. fileName = fh.getFileNameString().trim();

最开始我查了下API,我发现有一个getFileNameW()方法,就尝试着用这个方法得到要解压的文件名,发现如果有中文一下就成功了,于是 我把上面的方法替换成了这个,可是没想到如果来的压缩包没有英文,那么就得不到了,于是这就要加个判断。于是我通过正则表达式判断文件名中是否含有中文, 如果有中文,调用getFileNameW(),如果没有中文就调用getFileNameString()方法,就解决了。

附上判断字符中是否存在中文的方法:

  1. public static boolean existZH(String str) {
  2. String regEx = "[\\u4e00-\\u9fa5]";
  3. Pattern p = Pattern.compile(regEx);
  4. Matcher m = p.matcher(str);
  5. while (m.find()) {
  6. return true;
  7. }
  8. return false;
  9. }

于是,只需要将原来的代码第54行修改为如下代码即可:

Java中解压文件名有中文的rar包出现乱码问题的解决的更多相关文章

  1. Windows 压缩文件到 Linux中解压文件名乱码

    问题 在Windows中将文件夹压缩后,拿到Ubuntu系统中解压,中文文件名乱码 解决 因为两个系统所使用的编码不同,Windows一般使用GBK编码,Ubuntu使用utf8编码,只需要在解压的时 ...

  2. Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)

    本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...

  3. Java压缩包解压到指定文件

    在获得一个以Zip格式压缩的文件之后,需要将其进行解压缩,还原成压缩前的文件.若是使用Java自带的压缩工具包来实现解压缩文件到指定文件夹的功能,因为jdk提供的zip只能按UTF-8格式处理,而Wi ...

  4. 在Ubuntu系统中解压rar和zip文件的方法

    大家在以前的windows系统中会存有很多rar和zip格式的压缩文件,Ubuntu系统默认情况下对这些文件的支持不是很好,如果直接用"归档管理器"打开会提示错误,因此今天跟大家分 ...

  5. java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)

    // java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...

  6. 【linux】安装rar,并解压被压缩成多个rar的文件

    rar  官网:http://www.rarsoft.com/download.htm 选择  RAR for  linux   (注意你的系统是32位还是64位) 1 安装命令: $ cd /roo ...

  7. web 项目中a标签传值(中文)到后台的乱码问题

    web 项目中a标签传值(中文)到后台的乱码问题 jsp页面中的a标签: .............. <c:forEach items="${sellerList }" v ...

  8. JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包

    package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...

  9. Ubuntu下解决解压zip文件中文文件名乱码问题

    在Ubuntu下解压Windows下压缩的zip文件时,会出现解压出的带中文文件名的文件名乱码,这是因为Ubuntu和Windows默认的编码不同,Ubuntu下默认的编码是UTF-8,而Window ...

随机推荐

  1. java8新特性——简介

    java8问世已经有好长时间了,但是之前项目中都没有使用到,所以一直都只是了解一些,近期刚刚换了家新公司,在开发中需要使用到java8来开发,所以也是马上赶来学习一下java8得新特性. 一.新特性 ...

  2. [UOJ61]怎样更有力气

    这个题还是挺有意思的... 一个小结论是:在一个$n$点$m$边的图中,如果度数最小的点度数为$d$,那么$d^2=O(m)$,因为$d\leq\frac{2m}n$,所以$d^2\leq dn\le ...

  3. 【tarjan+拓扑】BZOJ3887-[Usaco2015 Jan]Grass Cownoisseur

    [题目大意] 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1) [思路] 首先 ...

  4. Codeforces Round #299 (Div. 2) A. Tavas and Nafas 水题

    A. Tavas and Nafas Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/535/pr ...

  5. jpush-react-native 推送功能(Android 篇)

    github原地址 极光推送官方支持的 React Native 插件 安装 npm install jpush-react-native --savenpm install jcore-react- ...

  6. 使用Win2D在UWP程序中2D绘图(二)

    绘制API 首先还是看一下前文的的示例: args.DrawingSession.DrawEllipse(155, 115, 80, 30, Colors.Black, 3);    args.Dra ...

  7. Inrush limiter also provides short-circuit protection

    For containing large amounts of bulk capacitance, controlling inrush currents poses problems. The si ...

  8. Spring自动装配Beans

    在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义“autowire”属性. <bean id="custom ...

  9. Spring JavaConfig实例

    从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中. 但是,仍然允许使用经典的XML方式来定义bean ...

  10. [ring3反作弊篇] 基于EBP遍历调用栈及模块名

    http://blog.csdn.net/wangningyu/article/details/44569803