日期:2014-05-20 浏览次数:20769 次
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;
public class RarFileUtil {
public static boolean existZH(String str) {
String regEx = "[\\u4e00-\\u9fa5]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.find();
}
public static String getFileName(String fileName) {
if (null == fileName && "".equals(fileName)) {
return "";
}
int index = fileName.lastIndexOf(".");
return fileName.substring(0, index);
}
public static boolean unrarFile(String rarFilePath, String extPlace) {
boolean flag = false;
if (null == rarFilePath
------解决方案--------------------
"".equals(rarFilePath.trim())) {
System.out.println("rar文件路径为空.");
return flag;
} else {
int index = rarFilePath.lastIndexOf(".");
if (index > -1) {
String suffix = rarFilePath.substring(index + 1,
rarFilePath.length());
if (!"rar".equalsIgnoreCase(suffix)) {
System.out.println("压缩文件格式不正确.");
return flag;
}
}
}
if (null == extPlace
------解决方案--------------------
"".equals(extPlace.trim())) {
System.out.println("解压路径为空.");
return flag;
}
Archive archive = null;
OutputStream os = null;
try {
File file = new File(rarFilePath);
String extDir = extPlace + getFileName(file.getName())
+ File.separator;
archive = new Archive(file);
FileHeader fh = archive.nextFileHeader();
String fileName = null;
String path = null;
String dirPath = null;
File dir = null;
while (null != fh) {
fileName = fh.getFileNameW().trim();
if (!existZH(fileName)) {
fileName = fh.getFileNameString();
}
path = (extDir + fileName).replaceAll("\\\\", "/");
int end = path.lastIndexOf("/");