package com.canalyze.web.tool;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
public class Decompression {
/**
* @param args
*/
static final int BUFFER = 2048;
/***
* 解壓.zip檔案
* @param filename
@SuppressWarnings("unchecked")
public static void unzip(String filename) {
try{
// String filename = "D:/1.zip";
int index1=filename.lastIndexOf(".");
String zipName=filename.substring(0,index1);
ZipFile zipFile = new ZipFile(filename);
Enumeration enu = zipFile.entries();
new File(zipName).mkdirs();
while(enu.hasMoreElements()){
ZipEntry zipEntry = (ZipEntry)enu.nextElement();
if(zipEntry.isDirectory()){
new File(zipName+"/"+zipEntry.getName()).mkdirs();
continue;
}
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry));
File file = new File(zipName+"/"+zipEntry.getName());
File parent = file.getParentFile();
if(parent != null && !parent.exists()){
parent.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER);
byte[] array = new byte[BUFFER];
while((bis.read(array, 0, BUFFER))!=-1){
bos.write(array, 0, BUFFER);
}
bos.flush();
bos.close();
bis.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
/***
* 解壓.tar.bz2檔案到指定檔案夾
* @param file
* @param outputDir
* @throws IOException
*/
public static void decompressTarBz2(File file, String outputDir) throws IOException {
try (TarArchiveInputStream tarIn =
new TarArchiveInputStream(
new BZip2CompressorInputStream(
new FileInputStream(file)))) {
createDirectory(outputDir, null);
TarArchiveEntry entry;
while ((entry = tarIn.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
createDirectory(outputDir, entry.getName());
} else {
try (OutputStream out = new FileOutputStream(
new File(outputDir + File.separator + entry.getName()))) {
writeFile(tarIn, out);
}
}
}
}
}
/**
* 寫檔案
* @param in
* @param out
* @throws IOException
*/
public static void writeFile(InputStream in, OutputStream out) throws IOException {
int length;
byte[] b = new byte[1024*100];
while ((length = in.read(b)) != -1) {
out.write(b, 0, length);
}
}
/**
* 建立目錄
* @param outputDir
* @param subDir
*/
public static void createDirectory(String outputDir, String subDir) {
File file = new File(outputDir);
//子目錄不為空
if (!(subDir == null || subDir.trim().equals(""))) {
file = new File(outputDir + File.separator + subDir);
}
if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.mkdirs();
}
}
/***
* 解壓.tar.gz檔案到指定檔案夾
* @param file
* @param outputDir
* @throws IOException
*/
public static void decompressTarGZip(File file, String outputDir) throws IOException {
try (TarArchiveInputStream tarIn =
new TarArchiveInputStream(
new GzipCompressorInputStream(
new FileInputStream(file)))) {
createDirectory(outputDir, null);
TarArchiveEntry entry;
while ((entry = tarIn.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
createDirectory(outputDir, entry.getName());
} else {
try (OutputStream out = new FileOutputStream(
new File(outputDir + File.separator + entry.getName()))) {
writeFile(tarIn, out);
}
}
}
}
}
}