歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java解壓帶密碼的RAR壓縮文件

Java解壓帶密碼的RAR壓縮文件

日期:2017/3/1 9:37:44   编辑:Linux編程

RAR壓縮算法是不公開的,所以這方面的開源項目不多

幸好有一個叫unrar的開源項目支持RAR的解壓,但不能壓縮RAR文件

不過,直接使用unrar卻不能支持帶密碼的RAR文件解壓,經過多方查找,終於在Google Code上面找到一個支持密碼的unrar版本,下載地址:http://code.google.com/p/java-unrar/

該項目依賴Jar包:

commons-logging.jar 比較常用,可以到Apache官網下載

gnu-crypto.jar 可以在http://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);
}
}
}

相關資源下載地址:

------------------------------------------分割線------------------------------------------

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2014年資料/11月/6日/Java解壓帶密碼的RAR壓縮文件

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

------------------------------------------分割線------------------------------------------

Copyright © Linux教程網 All Rights Reserved