解压缩文件的工具类(包括zip,gz类型)

package honda.jp.hm.hds.common.util;

import honda.jp.hm.common.framework.v1_0.log.ApplLogger;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Vector;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

public class HdsFileUtils {

 static Vector<String> tb = new Vector<String>();


 public static Vector<String> StreamToVector(InputStream is,
   ApplLogger logger) throws Exception {
  BufferedReader br = new BufferedReader(new InputStreamReader(is));
  String s;
  while ((s = br.readLine()) != null) {
   tb.add(s);
  }
  return tb;
 }


 public static String getPath(String path, String filename) {

  if (null == filename) {
   return path;
  }
  
  String pathResult = null;
  if (path.endsWith(File.separator)) {
   pathResult = path + filename;
  } else {
   pathResult = path + File.separator + filename;
  }

  return pathResult;
 }
 
 public static String createDir(String id, String errMsgKey,
   String dirPath, ApplLogger logger) throws Exception {

  File sourceDir = null;

  try {
   sourceDir = new File(dirPath);
   if (!sourceDir.exists()) {
    sourceDir.mkdirs();
   }
  } catch (Exception e) {
   logger.error(errMsgKey, new String[] { errMsgKey, id, dirPath }, e);
   throw new Exception(errMsgKey + "_" + id, e.getCause());
  }

  return sourceDir.getPath();

 }
 
 public static void delAllInDir(File dir) throws Exception {
  
  try {
   if (dir.isDirectory()) {
    for (File temp : dir.listFiles()) {
     delAllInDir(temp);
    }
   }
   if (dir.exists()) {
    dir.delete();
   }
  } catch(Exception e) {
   throw e;
  }
 }
 
 public static void zip(String inputFile,
  String zipFilePath ,String zipFileName) throws IOException {
 
  zip(new File(inputFile) ,zipFilePath ,zipFileName);
 }
 
 public static void unZip(String zipFileName,
  String outputDirectory, String fileName) throws IOException {

  ZipFile zipFile = null;
  InputStream in = null;
  FileOutputStream out = null;
  ZipEntry zipEntry = null;

  File fl = new File(outputDirectory);
  if (!fl.exists()) {
   fl.mkdirs();
  }
  try{
   zipFile = new ZipFile(zipFileName);
   Enumeration e = zipFile.getEntries();
   while (e.hasMoreElements()) {
    
    zipEntry = (ZipEntry) e.nextElement();
    File f = new File(outputDirectory + File.separator + fileName);
    f.createNewFile();
    in =zipFile.getInputStream(zipEntry);
    out = new FileOutputStream(f);

    byte[] by = new byte[1024];
    int c;
    while ((c = in.read(by)) != -1) {
     out.write(by, 0, c);
    }
    zipFile.close();
    out.close();
    in.close();
   }
  }catch(FileNotFoundException e){
   if(in != null){
    in.close();
   }
   if(out != null){
    out.close();
   }
   if(zipFile != null){
    zipFile.close();
   }
   throw e;
  }catch(IOException e1){
   if(in != null){
    in.close();
   }
   if(out != null){
    out.close();
   }
   if(zipFile != null){
    zipFile.close();
   }
   throw e1;
  }
 }

 public static String getZipFileName(String zipFileName) throws IOException {

  ZipFile zipFile = null;
  String fileName = "";
  try{
   zipFile = new ZipFile(zipFileName);
   Enumeration e = zipFile.getEntries();
   ZipEntry zipEntry = null;
   while (e.hasMoreElements()) {
    zipEntry = (ZipEntry) e.nextElement();
    fileName = zipEntry.getName();
   }
  }catch(FileNotFoundException e){
   if(zipFile != null){
    zipFile.close();
   }
   throw e;
  }catch(IOException e1){
   if(zipFile != null){
    zipFile.close();
   }
   throw e1;
  }
  return fileName;
 }

 private static void zip(File inputFile ,String zipFilePath,String zipFileName)
  throws FileNotFoundException ,IOException {
  createDirectory(zipFilePath, "");
  FileOutputStream out = null;
  ZipOutputStream zOut = null;
  try{
   out = new FileOutputStream(zipFilePath + File.separator + zipFileName);
   zOut = new ZipOutputStream(out);
   zip(zOut, inputFile, "");
   //out.close();
   zOut.close();  
  }catch(FileNotFoundException e){
   if(zOut != null){
    zOut.close();
   }
   throw e;
  }catch(IOException e1){
   if(zOut != null){
    zOut.close();
   }
   throw e1;
  }
 }

 private static void zip(ZipOutputStream zOut, File file,
  String base) throws IOException {
  FileInputStream in =null;
  try{
   if (file.isDirectory()) {
    File[] listFiles = file.listFiles();
    zOut.putNextEntry(new ZipEntry(base + File.separator));

    base = (base.length() == 0 ? "" : base + File.separator);
    for (int i = 0; i < listFiles.length; i++) {
     zip(zOut, listFiles[i], base + listFiles[i].getName());
    }
   } else {
    if (base == "") {
     base = file.getName();
    }
    
    zOut.putNextEntry(new ZipEntry(base));
    in = new FileInputStream(file);
    int len;
    while ((len = in.read()) != -1){
     zOut.write(len);
    }
    in.close();
   }
  }catch(FileNotFoundException e){
   if(in != null){
    in.close();
   }
   throw e;
  }catch(IOException e1){
   if(in != null){
    in.close();
   }
   throw e1;
  }
  
 }
 

 public static void tarGzipFile(String inputFile, String outputDir,
  String outputFileName) throws IOException {
  
  File f1 = new File(outputDir);
  if (!f1.exists()) {
   f1.mkdirs();
  }
  
  File file = new File(outputDir + "temp.tar");
  if (!file.exists()) {
   file.createNewFile();
  }
  FileOutputStream fout = null;
  BufferedOutputStream bufferedOutputStream =  null;
  TarArchiveOutputStream tout = null;
  FileOutputStream gzFile = null;
  GZIPOutputStream gzout = null;
  FileInputStream tarin = null;
  try {
   byte[] buf = new byte[1024];
   fout = new FileOutputStream(file);
      bufferedOutputStream = new BufferedOutputStream(fout);
      tout = new TarArchiveOutputStream(bufferedOutputStream);
   String temp = inputFile.replace("\\", "/").replace("//", "/");
   if(temp.endsWith("/")){
    temp = temp.substring(0,temp.length()-1);
   }
   String[] tempArr = temp.split("/");
   String temp2 = tempArr[tempArr.length-1];
   temp = temp.substring(0, temp.length()-1-temp2.length());
   tarFile(tout,inputFile,temp);
   //bufferedOutputStream.close();
   tout.close();
   gzFile = new FileOutputStream(outputDir + File.separator+ outputFileName);
   gzout = new GZIPOutputStream(gzFile);
   tarin = new FileInputStream(outputDir+ "temp.tar");
   int len;
   while ((len = tarin.read(buf)) != -1) {
    gzout.write(buf, 0, len);
   }
   gzout.close();
   gzFile.close();
   tarin.close();
   fout.close();
   file.delete();
  }catch(FileNotFoundException e){
   if(tout != null){
    tout.close();
   }
   if(gzFile != null){
    gzFile.close();
   }
   if(gzout != null){
    gzout.close();
   }
   if(fout != null){
    fout.close();
   }
   if(tarin != null){
    tarin.close();
   }
   file.delete();
   throw e;
  }catch(IOException e1){
   if(tout != null){
    tout.close();
   }
   if(gzFile != null){
    gzFile.close();
   }
   if(gzout != null){
    gzout.close();
   }
   if(fout != null){
    fout.close();
   }
   if(tarin != null){
    tarin.close();
   }
   file.delete();
   throw e1;
  }
  
 }
 
 private static void tarFile(TarArchiveOutputStream tout ,String folderPath,String outputDir) throws IOException {
  byte[] buf = new byte[2048];
  File srcPath = new File(folderPath);
  FileInputStream fin = null;
  try {
   if(!srcPath.isDirectory()){
    fin = new FileInputStream(folderPath);
    TarArchiveEntry tarEn = new TarArchiveEntry(srcPath);
    String path = srcPath.getParent();
    path = path.replace("\\", "/");
    path = path.substring(outputDir.length()).replace("/", File.separator);
    tarEn.setName(path + File.separator + srcPath.getName());
    tarEn.setSize(srcPath.length());
    tout.putArchiveEntry(tarEn);
    int num;
    while ((num = fin.read(buf)) != -1) {
     tout.write(buf, 0, num);
    }
       tout.closeArchiveEntry();
       fin.close();
   }else{
    int length = srcPath.listFiles().length;
    File[] files = srcPath.listFiles();
    for (int i = 0; i < length; i++) {
     String filename = srcPath.getPath() + File.separator
       + files[i].getName();
     tarFile(tout, filename,outputDir);
    }
   }
  }catch(FileNotFoundException e){
   if(fin != null){
    fin.close();
   }
   throw e;
  }catch(IOException e1){
   if(fin != null){
    fin.close();
   }
   throw e1;
  }
 }
 
 public static void gZipFile(String inputFile, String outputDir,
  String outputFileName) throws FileNotFoundException, IOException {
      
  String outFileName = outputDir + File.separator + outputFileName;
  File f1 = new File(outputDir);
  if (!f1.exists()){
   f1.mkdirs();
  }
  
  GZIPOutputStream out = null;
  BufferedInputStream bufferedIn = null;
  try {
   out = new GZIPOutputStream(new FileOutputStream(outFileName));
   bufferedIn = new BufferedInputStream(new FileInputStream(inputFile));
   
   byte[] buf = new byte[2048];
   int len;
   while((len = bufferedIn.read(buf)) > 0) {
       out.write(buf, 0, len);
   }
   bufferedIn.close();
   out.finish();
   out.close();
  }catch(FileNotFoundException e){
   if(bufferedIn != null){
    bufferedIn.close();
   }
   if(out != null){
    out.close();
   }
   throw e;
  }catch(IOException e1){
   if(bufferedIn != null){
    bufferedIn.close();
   }
   if(out != null){
    out.close();
   }
   throw e1;
  }
    }


 public static void unGzipFile(String inputFile, String outputDir,
  String outputName) throws FileNotFoundException, IOException {
  
  OutputStream out =null;
  BufferedInputStream in =null;
  try{
   in = new BufferedInputStream (new GZIPInputStream(new FileInputStream(inputFile)));
   out = new FileOutputStream(outputDir + File.separator + outputName);
   File f1 = new File(outputDir);
   if (f1.exists() != true) {
    f1.mkdirs();
   }

   int length = 0;
   byte[] b = new byte[2048];
   while ((length = in.read(b)) != -1) {
    out.write(b, 0, length);
   }
  
   in.close();
   out.close();
  }catch(FileNotFoundException e){
   if(in != null){
    in.close();
   }
   if(out != null){
    out.close();
   }
   throw e;
  }catch(IOException e1){
   if(in != null){
    in.close();
   }
   if(out != null){
    out.close();
   }
   throw e1;
  }
 }  
      
       public static void unTarGzipFile(String inputFile,
  String outputDir, String outFileName) throws FileNotFoundException, IOException {
  
  File file = new File(inputFile);
  if (outputDir == null) {
   outputDir = file.getParent();
  }
  TarArchiveInputStream tarIn = null;
  TarArchiveEntry entry = null;
  OutputStream out = null;
  try{
   tarIn = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(file)));
   while ((entry = tarIn.getNextTarEntry()) != null) {
    if (entry.isDirectory()) {
     createDirectory(outputDir, entry.getName());
    } else {
     File tmpFile = new File(outputDir + File.separator + entry.getName());
     if (outFileName !=null && !"".equals(outFileName)) {
      tmpFile = new File(outputDir + File.separator + outFileName);
     }
     createDirectory(tmpFile.getParent() + File.separator, null);
     out = new FileOutputStream(tmpFile);
     int length = 0;
     byte[] b = new byte[2048];
     while ((length = tarIn.read(b)) != -1) {
      out.write(b, 0, length);
     }
     if (out != null) {
      out.close();
     }
    }
   }
   if (tarIn != null) {
    tarIn.close();
   }
  }catch(FileNotFoundException e){
   if(tarIn != null){
    tarIn.close();
   }
   if(out != null){
    out.close();
   }
   throw e;
  }catch(IOException e1){
   if(tarIn != null){
    tarIn.close();
   }
   if(out != null){
    out.close();
   }
   throw e1;
  }
  
 }
 

 private static void createDirectory(String outputDir, String subDir) {
  
  File file = new File(outputDir);
  if (subDir != null && !subDir.trim().isEmpty()) {
   file = new File(outputDir + File.separator + subDir);
  }

  if (!file.exists()) {
   file.mkdirs();
  }
 }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值