Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Java 解開帶密碼的 RAR 壓縮檔

RAR 的壓縮演算法並未公開,因此相關的開源實作屈指可數。好消息是有一個名為 unrar 的專案能解 RAR,只是它只能解、不能壓。更麻煩的是,原版 unrar 遇到有密碼的 RAR 一樣無解。

翻找多時,我在 Google Code 上找到支援密碼的 unrar 分支:https://code.google.com/p/java-unrar/

它需要兩個相依套件:

commons-logging.jar:常用套件,可至 Apache 官網取得

gnu-crypto.jar:可至 https://www.gnu.org/software/gnu-crypto/ 下載

解壓工具類

package com.ninemax.demo.rar;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;

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

/**
 * RAR 格式壓縮文件解壓工具類
 * 不支持 RAR 格式壓縮
 * 支持中文,支持 RAR 壓縮文件密碼
 * 依賴 jar 包
 * commons-io.jar
 * commons-logging.jar
 * java-unrar-decryption-supported.jar
 * gnu-crypto.jar
 *
 * @author ninemax
 */
public class RarDecompressionUtil {
 
 public static final String SEPARATOR = File.separator;
 
 // =============================== RAR Format ================================
 /**
  * 解壓指定 RAR 文件到當前文件夾
  * @param srcRar 指定解壓
  * @param password 壓縮文件時設定的密碼
  * @throws IOException
  */
 public static void unrar(String srcRar, String password) throws IOException {
  unrar(srcRar, null, password);
 }
 
 /**
  * 解壓指定的 RAR 壓縮文件到指定的目錄中
  * @param srcRar 指定的 RAR 壓縮文件
  * @param destPath 指定解壓到的目錄
  * @param password 壓縮文件時設定的密碼
  * @throws IOException
  */
 public static void unrar(String srcRar, String destPath, String password) throws IOException {
  File srcFile = new File(srcRar);
  if (!srcFile.exists()) {
   return;
  }
  if (null == destPath || destPath.length() == 0) {
   unrar(srcFile, srcFile.getParent(), password);
   return;
  }
  unrar(srcFile,destPath, password);
 }
 
 /**
  * 解壓指定 RAR 文件到當前文件夾
  * @param srcRarFile 解壓文件
  * @param password 壓縮文件時設定的密碼
  * @throws IOException
  */
 public static void unrar(File srcRarFile, String password) throws IOException {
  if (null == srcRarFile || !srcRarFile.exists()) {
   throw new IOException("指定文件不存在.");
  }
  unrar(srcRarFile, srcRarFile.getParent(),password);
 }
 
 /**
  * 解壓指定 RAR 文件到指定的路徑
  * @param srcRarFile 需要解壓 RAR 文件
  * @param destPath 指定解壓路徑
  * @param password 壓縮文件時設定的密碼
  * @throws IOException
  */
 public static void unrar(File srcRarFile, String destPath, String password) throws IOException {
  if (null == srcRarFile || !srcRarFile.exists()) {
   throw new IOException("指定壓縮文件不存在.");
  }
  if (!destPath.endsWith(SEPARATOR)) {
   destPath += SEPARATOR;
  }
  Archive archive = null;
  OutputStream unOut = null;
  try {
   archive = new Archive(srcRarFile, password, false);
   FileHeader fileHeader = archive.nextFileHeader();
   while(null != fileHeader) {
    if (!fileHeader.isDirectory()) {
     // 1 根據不同的操作系統拿到相應的 destDirName 和 destFileName
     String destFileName = "";
     String destDirName = "";
     if (SEPARATOR.equals("/")) {  // 非 windows 系統
      destFileName = (destPath + fileHeader.getFileNameW()).replaceAll("\\\\", "/");
      destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));
     } else {  // windows 系統
      destFileName = (destPath + fileHeader.getFileNameW()).replaceAll("/", "\\\\");
      destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));
     }
     // 2 創建文件夾
     File dir = new File(destDirName);
     if (!dir.exists() || !dir.isDirectory()) {
      dir.mkdirs();
     }
     // 抽取壓縮文件
     unOut = new FileOutputStream(new File(destFileName));
     archive.extractFile(fileHeader, unOut);
     unOut.flush();
     unOut.close();
    }
    fileHeader = archive.nextFileHeader();
   }
   archive.close();
  } catch (RarException e) {
   e.printStackTrace();
  } finally {
   IOUtils.closeQuietly(unOut);
  }
 }
}

實作上有兩個細節值得注意:一是用 getFileNameW() 取檔名,才能拿到 Unicode 形式的中文;二是路徑分隔符在 Windows 與類 Unix 系統上不同,必須正規表達式替換,否則會建立出奇怪的文件夾。

Copyright © Linux教程網 All Rights Reserved