java中rar_Java中Rar文件处理

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 unRarFile(String srcRarPath, String dstDirectoryPath, String projectPath) { List htmlList = new ArrayList(); List htmlFile = new ArrayList(); 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 loadFile) { for (File f : loadFile) { f.delete(); } } /** 公用的rar解压处理方法开始 **/ /** * 根据原始rar路径,解压到指定文件夹下. *  * @param srcRarPath *            原始rar路径 * @param dstDirectoryPath *            解压到的文件夹 */ public static List publicUnRarFile(String srcRarPath, String dstDirectoryPath, String[] extensionFilter) { List fileList = new ArrayList(); 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解压处理方法结束 **/ }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值