// java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)
新建工具类:
package com.mobile.utils;
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* @Description: UnzipUtil 工具类
* @Param:
* @return:
* @Author: mufeng
* @Date: 2018/8/20
*/
public class UnzipUtil {
//解压.zip文件
public static void unZip(String sourceFile, String outputDir) throws IOException {
ZipFile zipFile = null;
File file = new File(sourceFile);
try {
Charset CP866 = Charset.forName("CP866"); //specifying alternative (non UTF-8) charset
zipFile = new ZipFile(file, CP866);
createDirectory(outputDir,null);//创建输出目录
Enumeration> enums = zipFile.entries();
while(enums.hasMoreElements()){
ZipEntry entry = (ZipEntry) enums.nextElement();
System.out.println("解压." + entry.getName());
if(entry.isDirectory()){//是目录
createDirectory(outputDir,entry.getName());//创建空目录
}else{//是文件
File tmpFile = new File(outputDir + "/" + entry.getName());
createDirectory(tmpFile.getParent() + "/",null);//创建输出目录
InputStream in = null;
OutputStream out = null;
try{
in = zipFile.getInputStream(entry);;
out = new FileOutputStream(tmpFile);
int length = 0;
byte[] b = new byte[2048];
while((length = in.read(b)) != -1){
out.write(b, 0, length);
}
}catch(IOException ex){
throw ex;
}finally{
if(in!=null)
in.close();
if(out!=null)
out.close();
}
}
}
} catch (IOException e) {
throw new IOException("解压缩文件出现异常",e);
} finally{
try{
if(zipFile != null){
zipFile.close();
}
}catch(IOException ex){
throw new IOException("关闭zipFile出现异常",ex);
}
}
}
/**
* 构建目录
* @param outputDir
* @param subDir
*/
public static void createDirectory(String out