summaryrefslogtreecommitdiff
path: root/src/com/nis/nmsclient/util/RarUtil.java
diff options
context:
space:
mode:
authorchenjinsong <[email protected]>2018-09-27 16:11:54 +0800
committerchenjinsong <[email protected]>2018-09-27 16:11:54 +0800
commit56d71f261a8bd6031e47e2bf80867049a2aa13da (patch)
treef09257b2143782a333a9eda3395137837d9bdad1 /src/com/nis/nmsclient/util/RarUtil.java
initial commit
Diffstat (limited to 'src/com/nis/nmsclient/util/RarUtil.java')
-rw-r--r--src/com/nis/nmsclient/util/RarUtil.java125
1 files changed, 125 insertions, 0 deletions
diff --git a/src/com/nis/nmsclient/util/RarUtil.java b/src/com/nis/nmsclient/util/RarUtil.java
new file mode 100644
index 0000000..d394411
--- /dev/null
+++ b/src/com/nis/nmsclient/util/RarUtil.java
@@ -0,0 +1,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();
+ }
+ }
+}