package com.ylink.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;
/**
* 解压zip包或者rar包工具类
*
*/
public class DeCompressUtil {
private static final int BUFFEREDSIZE = 1024;
/**
* 解压zip的内容到指定的目录下,可以处理其文件夹下包含子文件夹的情况
* @param zipFilename
* 要解压的zip包文件
* @param destDir
* 解压后存放的目录
* @throws Exception
*/
public synchronized static void unzip(String zipFilename, String destDir) throws Exception {
try {
File outFile = new File(destDir);
if (!outFile.exists()) {
outFile.mkdirs();
}
File zipF = new File(zipFilename);
if ((!zipF.exists()) && (zipF.length() <= 0 )) {
throw new Exception( "要解压的文件不存在!" );
}
ZipFile zipFile = new ZipFile(zipFilename,"GB2312");//处理中文乱码问题
Enumeration<?> en = zipFile.getEntries();
ZipEntry zipEntry = null;
while (en.hasMoreElements()) {
zipEntry = (ZipEntry) en.nextElement();
if (zipEntry.isDirectory()) {
// mkdir directory
String dirName = zipEntry.getName();
dirName = dirName.substring(0, dirName.length() - 1);
File f = new File(outFile.getPath() + File.separator + dirName);
f.mkdirs();
} else {
//unzip file
String strFilePath = outFile.getPath() + File.separator + zipEntry.getName();
File f = new File(strFilePath);
//begin/
//判断文件不存在的话,就创建该文件所在文件夹的目录
if (!f.exists()) {
String[] arrFolderName = zipEntry.getName().split("/");
String strRealFolder = "";
for (int i = 0; i < (arrFolderName.length - 1); i++) {
strRealFolder += arrFolderName[i] + File.separator;
}
strRealFolder = outFile.getPath() + File.separator + strRealFolder;
File tempDir = new File(strRealFolder);
//此处使用.mkdirs()方法,而不能用.mkdir()
tempDir.mkdirs();
}
//end///
f.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
FileOutputStream out = new FileOutputStream(f);
int c;
byte[] by = new byte[BUFFEREDSIZE];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
//out.flush();
out.close();
in.close();
}
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 解压zip格式压缩包
* 对应的是ant.jar
*/
@SuppressWarnings("unused")
private static void unzip2(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 = "";
if(fh.isUnicode()){//如果存在中文名,需要使用getFileNameW()方法,解决中文乱码问题
compressFileName = fh.getFileNameW().trim();
}else{
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).toLowerCase();
if("zip".equals(type)){
DeCompressUtil.unzip(sourceFile, destDir);
//DeCompressUtil.unzip2(sourceFile, destDir);
}else if("rar".equals(type)){
DeCompressUtil.unrar(sourceFile, destDir);
}else{
throw new Exception("只支持zip和rar格式的压缩包!");
}
}
public static void main(String[] args) throws Exception {
try {
//DeCompressUtil.deCompress("E:/软件安装包/jetty-6.1.14.zip", "E:/软件安装包/jetty");
DeCompressUtil.deCompress("E:/软件安装包/项目/ydsys.rar", "E:/软件安装包/项目");
} catch (IOException e) {
e.printStackTrace();
}
}
}
最近要搞一个java解压zip或者rar的功能,网上搜索了一下,在此记录,相关jar到网上下载。