天道不一定酬所有勤
但是,天道只酬勤
Hollis出品的全套Java面试宝典不来了解一下吗?

Java文件解压之TGZ解压

Hollis出品的全套Java面试宝典不来了解一下吗?
package com.alibaba.intl.batch.dependency;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 解压工具类
 * @author hollis
 *
 */
public class PackDecompressor {
    public static int BUFFER_SIZE = 2048;

    public static void main(String[] args) throws Exception {
            unTarGZ("/home/hollis/Downloads/a.tgz", "/home/hollis/Downloads/a");
    }

    public static void unTarGZ(String file,String destDir) throws Exception{ 
       File tarFile = new File(file);
       unTarGZ(tarFile, destDir);
   }
    public static void unTarGZ(File tarFile,String destDir) throws Exception{ 
       if(StringUtils.isBlank(destDir)) {
           destDir = tarFile.getParent();
       }
       destDir = destDir.endsWith(File.separator) ? destDir : destDir + File.separator;
       unTar(new GzipCompressorInputStream(new FileInputStream(tarFile)), destDir);
   }  
    private static void unTar(InputStream inputStream, String destDir) throws Exception {

       TarArchiveInputStream tarIn = new TarArchiveInputStream(inputStream, BUFFER_SIZE);
       TarArchiveEntry entry = null;
       try {
           while ((entry = tarIn.getNextTarEntry()) != null) {

               if (entry.isDirectory()) {//是目录  
                   createDirectory(destDir, entry.getName());//创建空目录  
               } else {//是文件  
                   File tmpFile = new File(destDir + File.separator + entry.getName());
                   createDirectory(tmpFile.getParent() + File.separator, null);//创建输出目录  
                   OutputStream out = null;
                   try { 
                       out = new FileOutputStream(tmpFile);
                       int length = 0;
                       byte[] b = new byte[2048];
                       while ((length = tarIn.read(b)) != -1) {
                           out.write(b, 0, length);
                       }
                   } finally {
                       IOUtils.closeQuietly(out);
                   }
               }
           }
       } catch (Exception e) {
           e.printStackTrace();
           throw e;
       } finally {
           IOUtils.closeQuietly(tarIn);
       }
   }

   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()){  
           file.mkdirs();  
       }  
   }  
}
赞(3)
如未加特殊说明,此网站文章均为原创,转载必须注明出处。HollisChuang's Blog » Java文件解压之TGZ解压
Hollis出品的全套Java面试宝典不来了解一下吗?

评论 抢沙发

HollisChuang's Blog

联系我关于我