Java中解压文件名有中文的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行修改为如下代码即可:
Java中解压文件名有中文的rar包出现乱码问题的解决的更多相关文章
- Windows 压缩文件到 Linux中解压文件名乱码
问题 在Windows中将文件夹压缩后,拿到Ubuntu系统中解压,中文文件名乱码 解决 因为两个系统所使用的编码不同,Windows一般使用GBK编码,Ubuntu使用utf8编码,只需要在解压的时 ...
- Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)
本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...
- Java压缩包解压到指定文件
在获得一个以Zip格式压缩的文件之后,需要将其进行解压缩,还原成压缩前的文件.若是使用Java自带的压缩工具包来实现解压缩文件到指定文件夹的功能,因为jdk提供的zip只能按UTF-8格式处理,而Wi ...
- 在Ubuntu系统中解压rar和zip文件的方法
大家在以前的windows系统中会存有很多rar和zip格式的压缩文件,Ubuntu系统默认情况下对这些文件的支持不是很好,如果直接用"归档管理器"打开会提示错误,因此今天跟大家分 ...
- java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)
// java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...
- 【linux】安装rar,并解压被压缩成多个rar的文件
rar 官网:http://www.rarsoft.com/download.htm 选择 RAR for linux (注意你的系统是32位还是64位) 1 安装命令: $ cd /roo ...
- web 项目中a标签传值(中文)到后台的乱码问题
web 项目中a标签传值(中文)到后台的乱码问题 jsp页面中的a标签: .............. <c:forEach items="${sellerList }" v ...
- JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包
package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...
- Ubuntu下解决解压zip文件中文文件名乱码问题
在Ubuntu下解压Windows下压缩的zip文件时,会出现解压出的带中文文件名的文件名乱码,这是因为Ubuntu和Windows默认的编码不同,Ubuntu下默认的编码是UTF-8,而Window ...
随机推荐
- 【BZOJ 4455】 4455: [Zjoi2016]小星星 (容斥原理+树形DP)
4455: [Zjoi2016]小星星 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 426 Solved: 255 Description 小Y是 ...
- 【BZOJ 3566】 3566: [SHOI2014]概率充电器 (概率树形DP)
3566: [SHOI2014]概率充电器 Description 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品——概率充电器:“采用全新纳米级加工技术,实现元件与导线能否通电 ...
- HDU 6141 I am your Father!(最小树形图)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6141 [题目大意] 给出一个有向图,求1点为根的最小树形图使得第n个点的直接父亲编号最小 [题解] ...
- 【离散化】【DFS】Gym - 101617H - Security Badges
题意:给你一张有向图,每条边有个限制范围,只有权值在限制范围内的人能走这条边,问你权值不超过K的人中,有多少人能从S到T. K很大,因此我们只处理边的范围的上下界这O(m)个权值能否到达,以防万一,还 ...
- [CodeForces-178F]Representative Sampling
题目大意: 给你n个字符串,要求从中选出k个字符串,使得字符串两两lcp之和最大. 思路: 动态规划. 首先将所有的字符串排序,求出相邻两个字符串的lcp长度(很显然,对于某一个字符串,和它lcp最长 ...
- nginx部署ssl证书
确保nginx有ssl模块,修改nginx.conf文件 在server中添加 listen 443 ssl; #crt文件路径 证书的公钥 ssl_certificate xxx.crt; #key ...
- JSP -- include指令与include动作的区别
JSP -- include指令与include动作的区别 (1)格式的区别: include指令:<%@include file = "文件名"%> include动 ...
- 密码加密_md5
md5加密 package com.fh.util; import java.security.MessageDigest; public class MD5 { public static Stri ...
- poj 3624 Charm Bracelet 背包DP
Charm Bracelet Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3624 Descripti ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...