package decompress;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;
public class DeCompressUtil {
/**
* 解壓zip格式壓縮包
* 對應的是ant.jar
*/
private static void unzip(String sourceZip,String destDir) throws Exception{
try{
Project p = new Project();
Expand e = new Expand();
e.setProject(p);
e.setSrc(new File(sourceZip));
e.setOverwrite(false);
e.setDest(new File(destDir));
/*
ant下的zip工具默認壓縮編碼為UTF-8編碼,
而winRAR軟件壓縮是用的windows默認的GBK或者GB2312編碼
所以解壓縮時要制定編碼格式
*/
e.setEncoding("gbk");
e.execute();
}catch(Exception e){
throw e;
}
}
/**
* 解壓rar格式壓縮包。
* 對應的是java-unrar-0.3.jar,但是java-unrar-0.3.jar又會用到commons-logging-1.1.1.jar
*/
private static void unrar(String sourceRar,String destDir) throws Exception{
Archive a = null;
FileOutputStream fos = null;
try{
a = new Archive(new File(sourceRar));
FileHeader fh = a.nextFileHeader();
while(fh!=null){
if(!fh.isDirectory()){
//1 根據不同的操作系統拿到相應的 destDirName 和 destFileName
String compressFileName = fh.getFileNameString().trim();
String destFileName = "";
String destDirName = "";
//非windows系統
if(File.separator.equals("/")){
destFileName = destDir + compressFileName.replaceAll("\\\\", "/");
destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));
//windows系統
}else{
destFileName = destDir + compressFileName.replaceAll("/", "\\\\");
destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));
}
//2創建文件夾
File dir = new File(destDirName);
if(!dir.exists()||!dir.isDirectory()){
dir.mkdirs();
}
//3解壓縮文件
fos = new FileOutputStream(new File(destFileName));
a.extractFile(fh, fos);
fos.close();
fos = null;
}
fh = a.nextFileHeader();
}
a.close();
a = null;
}catch(Exception e){
throw e;
}finally{
if(fos!=null){
try{fos.close();fos=null;}catch(Exception e){e.printStackTrace();}
}
if(a!=null){
try{a.close();a=null;}catch(Exception e){e.printStackTrace();}
}
}
}
/**
* 解壓縮
*/
public static void deCompress(String sourceFile,String destDir) throws Exception{
//保證文件夾路徑最后是"/"或者"\"
char lastChar = destDir.charAt(destDir.length()-1);
if(lastChar!='/'&&lastChar!='\\'){
destDir += File.separator;
}
//根據類型,進行相應的解壓縮
String type = sourceFile.substring(sourceFile.lastIndexOf(".")+1);
if(type.equals("zip")){
DeCompressUtil.unzip(sourceFile, destDir);
}else if(type.equals("rar")){
DeCompressUtil.unrar(sourceFile, destDir);
}else{
throw new Exception("只支持zip和rar格式的壓縮包!");
}
}
}
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.reyo.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);
}
}
}
