blob: d394411ece7b36c3fa7e4459e21017d1ade06756 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
package com.nis.nmsclient.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;
/**
* 对rar进行解压缩
*
*/
public class RarUtil {
static Logger logger = Logger.getLogger(RarUtil.class);
/**
* 对单个rar压缩包解压
* @param rarFileName 需要解压的rar文件
* @param destDir 解压后的目标目录
* @throws Exception
*/
public static void unRar(String rarFileName, String destDir)
throws Exception {
logger.debug("unRar start……");
Archive archive = null;
FileHeader fh = null;
try {
logger.debug("unRar rarFileName---" + rarFileName);
logger.debug("unRar destDir---" + destDir);
File file = new File(rarFileName);
if(!file.exists()){
// throw new Exception("解压源文件不存在: " + file.getAbsolutePath());
throw new Exception("Unzip source file does not exist: " + file.getAbsolutePath());
}
logger.debug("unRar: get archive……");
archive = new Archive(file);
logger.debug("unRar: archive =" + archive);
if (archive != null) {
logger.debug("unRar: while start");
int index = 0;
while ((fh = archive.nextFileHeader()) != null) {
logger.debug("unRar: " + ++index + " start");
String fileName = fh.getFileNameW().trim();
if (!existZH(fileName)) {
fileName = fh.getFileNameString().trim();
}
String path = (destDir + File.separator + fileName)
.replaceAll("\\\\", "/");
logger.debug("unRar: path---" + path);
if (fh.isDirectory()) {
//logger.debug("unRar: FileHeader is directory");
int end = path.lastIndexOf("/");
if (end != -1) {
File dir = new File(path.substring(0, end));
if (!dir.exists()) {
dir.mkdirs();
}
}
}else{
//logger.debug("unRar: FileHeader is file");
FileOutputStream os = null;
try{
File out = new File(path);
if(!out.getParentFile().exists()){
out.getParentFile().mkdirs();
}
os = new FileOutputStream(out);
archive.extractFile(fh, os);
}catch (Exception e) {
throw e;
}finally{
fh = null;
if (os != null) {
os.close();
os = null;
}
}
}
logger.debug("unRar: " + index + " end");
}
logger.debug("unRar: while end");
}
logger.debug("unRar end……");
} catch (Exception e) {
throw e;
} finally {
fh = null;
try {
if (archive != null) {
archive.close();
archive = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
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;
}
public static void main(String[] args) {
try {
unRar("D:\\temp\\logs.rar", "D:\\temp\\log");
} catch (Exception e) {
e.printStackTrace();
}
}
}
|