Java解压带密码的Rar压缩文件
2017-03-11 11:36
525 查看
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/下载
下面是一个简单的解压示例:
[java]
view plain
copy
print?
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://code.google.com/p/java-unrar/downloads/list
或者
http://download.csdn.net/detail/zhangyihui1986/4415967
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/下载
下面是一个简单的解压示例:
[java]
view plain
copy
print?
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);
}
}
}
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://code.google.com/p/java-unrar/downloads/list
或者
http://download.csdn.net/detail/zhangyihui1986/4415967
相关文章推荐
- Java解压带密码的Rar压缩文件
- Java方法进行解压/生成有密码保护的压缩文件(zip格式)
- java.util.zip包无法正常解压rar软件压缩的rar文件
- java解压zip或rar压缩文件(有源码和jar包以及对中文乱码的处理方法)
- Java解压和压缩带密码的zip文件
- java解压RAR压缩文件
- java zip和rar压缩文件的解压
- java解压zip或者rar压缩文件
- Linux实现对rar压缩文件的解压
- C#解压RAR压缩文件(--转载--测试通过)
- [JAVA100例]040、压缩和解压文件(java100例)
- Java文件解压与压缩
- linux下解压rar压缩文件
- C#解压RAR压缩文件
- Asp.net调用RAR压缩 解压文件
- C#解压RAR压缩文件
- 利用java的ZipOutputStream类压缩文件,解压文件或文件夹中的文件
- java解压缩zip文件,java创建zip文件,java压缩文件,java解压文件,用到ant.jar解决汉字乱码
- C#解压RAR压缩文件(--转载--测试通过)
- java压缩与解压文件