Java中Rar文件处理

来源:互联网 发布:淘宝店铺不能开通花呗 编辑:程序博客网 时间:2024/05/22 08:22
package com.cms21.mobilecrm.utils;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;


import com.cms21.mobilecrm.common.context.ApplicationProperties;


import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;


public class RarUtils {


private static final String zipFilterExtension = AppPropertyUtils
.getProperty(ApplicationProperties.UNZIP_FILE_FILTER);


/** 针对于邮件模板的rar解压处理方法开始 **/


/**
* 根据原始rar路径,解压到指定文件夹下.

* @param srcRarPath
*            原始rar路径
* @param dstDirectoryPath
*            解压到的文件夹
*/
public static List<String> unRarFile(String srcRarPath,
String dstDirectoryPath, String projectPath) {
List<String> htmlList = new ArrayList<String>();
List<File> htmlFile = new ArrayList<File>();
if (!srcRarPath.toLowerCase().endsWith(".rar")) {
System.out.println("非rar文件!");
return null;
}
File dstDiretory = new File(dstDirectoryPath);
if (!dstDiretory.exists()) { // 目标目录不存在时,创建该文件夹
dstDiretory.mkdirs();
}
File file = new File(srcRarPath);
Archive a = null;
try {
a = new Archive(file);
if (a != null) {
a.getMainHeader().print(); // 打印文件信息.
FileHeader fh = a.nextFileHeader();
while (fh != null) {
String fileName = fh.getFileNameW();
if (!existZH(fileName)) {
fileName = fh.getFileNameString();
}
if (fh.isDirectory()) { // 文件夹
if (!isSuffix(fileName)) { // 判断是否是html文件
fh = a.nextFileHeader();
continue;
}
File fol = new File(dstDirectoryPath + File.separator
+ fileName);
fol.mkdirs();
} else { // 文件
if (!isSuffix(fileName)) {
fh = a.nextFileHeader();
continue;
}
File out = new File(dstDirectoryPath + File.separator
+ fileName);
try {// 之所以这么写try,是因为万一这里面有了异常,不影响继续解压.
if (!out.exists()) {
if (!out.getParentFile().exists()) {// 相对路径可能多级,可能需要创建父目录.
if (!isSuffix(fileName)) {
fh = a.nextFileHeader();
continue;
}
out.getParentFile().mkdirs();
}
out.createNewFile();
}
FileOutputStream os = new FileOutputStream(out);
a.extractFile(fh, os);
os.close();


if (isHTML(fileName)) {
htmlList.add(dealHtml(out, projectPath));
htmlFile.add(out);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
fh = a.nextFileHeader();
}
a.close();
}
} catch (Exception e) {
file.delete();
file.getParentFile().delete();
e.printStackTrace();
} finally {
deleteHtmlFile(htmlFile);
}
return htmlList;
}


/** 针对于邮件模板的rar解压处理方法结束 **/


public static void main(String[] args) {
// String srcRarPath = "E://Java//Java.rar";
// String dstDirectoryPath = "E://Java";
// unRarFile(srcRarPath, dstDirectoryPath);
// System.out.println("解压结束");
}


// 对html文件进行重写,目的在于将里边的img中属性为src的进行修改
public static String dealHtml(File out, String projectPath) {
String lastPath = out.getParent().substring(projectPath.length() + 8);
String url = (AppPropertyUtils
.getProperty(ApplicationProperties.IMG_PATH_DOMAIN)
+ AppPropertyUtils.getProperty("img_path") + lastPath).replace(
"\\", "/");
// 得到服务器域名,并和解压文件目录叠加
String htmlOld = "";
try {
htmlOld = FileUtils.readFileToString(out, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
String htmlNew = HtmlUtils.replaceImg(htmlOld, url);
return htmlNew;
}


// 判断解压出来的文件是否为图片
public static boolean isPics(String filename) {
boolean flag = false;
String FileName = filename.toLowerCase();
if (FileName.endsWith(".jpg") || FileName.endsWith(".gif")
|| FileName.endsWith(".bmp") || FileName.endsWith(".png"))
flag = true;


return flag;
}


// 判断解压出来的文件是否为HTML或htm
public static boolean isHTML(String filename) {
return FilenameUtils.isExtension(filename.toLowerCase(), new String[] {
"html", "htm" });
}


// 判断解压出来的文件是否为配置文件规定的后缀
public static boolean isSuffix(String filename) {
String fileExtension[] = zipFilterExtension.split(",");
return FilenameUtils.isExtension(filename.toLowerCase(), fileExtension);
}


// 处理解压后文件为中文是出现的乱码
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;
}


// 删除解压后,并已经处理过的html或htm文件
public static void deleteHtmlFile(List<File> loadFile) {
for (File f : loadFile) {
f.delete();
}
}


/** 公用的rar解压处理方法开始 **/


/**
* 根据原始rar路径,解压到指定文件夹下.

* @param srcRarPath
*            原始rar路径
* @param dstDirectoryPath
*            解压到的文件夹
*/
public static List<File> publicUnRarFile(String srcRarPath,
String dstDirectoryPath, String[] extensionFilter) {
List<File> fileList = new ArrayList<File>();
if (!srcRarPath.toLowerCase().endsWith(".rar")) {
System.out.println("非rar文件!");
}
File dstDiretory = new File(dstDirectoryPath);
if (!dstDiretory.exists()) { // 目标目录不存在时,创建该文件夹
dstDiretory.mkdirs();
}
File file = new File(srcRarPath);
Archive a = null;
try {
a = new Archive(file);
if (a != null) {
// a.getMainHeader().print(); // 打印文件信息.
FileHeader fh = a.nextFileHeader();
while (fh != null) {
String fileName = fh.getFileNameW();
if (!existZH(fileName)) {
fileName = fh.getFileNameString();
}
if (fh.isDirectory()) { // 文件夹
if (!isFileExtension(fileName, extensionFilter)) {
fh = a.nextFileHeader();
continue;
}
File fol = new File(dstDirectoryPath + File.separator
+ fileName);
fol.mkdirs();
} else { // 文件
if (!isFileExtension(fileName, extensionFilter)) {
fh = a.nextFileHeader();
continue;
}
File out = new File(dstDirectoryPath + File.separator
+ fileName);
try {// 之所以这么写try,是因为万一这里面有了异常,不影响继续解压.
if (!out.exists()) {
if (!out.getParentFile().exists()) {// 相对路径可能多级,可能需要创建父目录.
if (!isFileExtension(fileName, extensionFilter)) {
fh = a.nextFileHeader();
continue;
}
out.getParentFile().mkdirs();
}
out.createNewFile();
}
FileOutputStream os = new FileOutputStream(out);
a.extractFile(fh, os);
os.close();


fileList.add(out);
} catch (Exception ex) {
ex.printStackTrace();
}
}
fh = a.nextFileHeader();
}
a.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return fileList;
}


// 判断解压出来的文件是否是特定的后缀
public static boolean isFileExtension(String filename,
String[] extensionFilter) {
return FilenameUtils.isExtension(filename.toLowerCase(),
extensionFilter);
}


/** 公用的rar解压处理方法结束 **/
}
原创粉丝点击