java代码实现tar包文件解压

使用commons-compress-1.9.jar进行解压 ,通过重写UnTar方法把源路径与目标路径转为文件,通过TarArchiveInputStream读入目标文件流,然后解压到目的地址。

或者是引用pom

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-compress</artifactId>
   <version>1.18</version>
</dependency>

TarArchiveInputStream使用实例地址如下:

https://www.programcreek.com/java-api-examples/?class=org.apache.commons.compress.archivers.tar.TarArchiveInputStream&method=getNextTarEntry

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
public class UnTar {
    private File sourceFile;
    private File targetFile;
    private TarArchiveInputStream  TarArchiveInputStream;
    private static final int BUFFER = 4096; 
     /** 
     * 文件 解归档 
     *  
     * @param srcPath 
     *            源文件路径 
     * @param destPath 
     *            目标文件路径 
     * @throws Exception 
     */  
          public UnTar(String sourceFilePath, String targetFilePath)
                    throws FileNotFoundException {
             sourceFile = new File(sourceFilePath);
                if (!sourceFile.exists()) {
                    throw new FileNotFoundException("file can not be found:"
                            + sourceFilePath);
                }
                if (sourceFile.isDirectory()) {
                    throw new IllegalArgumentException("file can not be directory:"
                            + sourceFilePath);
                }
                targetFile = new File(targetFilePath);
                if (targetFile.isFile()) {
                    throw new IllegalArgumentException("file should be directory:"
                            + targetFilePath);
                }
            }
          public void  unTar() throws IOException{
                try {
                    TarArchiveInputStream = new TarArchiveInputStream(  
                        new FileInputStream(sourceFile)); 
              //buildDerectory(targetFile);
              TarArchiveEntry entry = null;  
                while ((entry = TarArchiveInputStream.getNextTarEntry()) != null) {  
                     String dir = targetFile.getPath() + File.separator + entry.getName();  
                      File dirFile = new File(dir);  
                      // 文件检查  
                        fileProber(dirFile);  
                        if (entry.isDirectory()) {  
                            dirFile.mkdirs();  
                        } else {  
                            dearchiveFile(dirFile, TarArchiveInputStream);  
                        }
                }
                } finally {
                    if (TarArchiveInputStream != null) {
                        TarArchiveInputStream.close();
                    }
                }
          }
          /** 
             * 文件解归档 
             *  
             * @param destFile 
             *            目标文件 
             * @param tais 
             *            TarArchiveInputStream 
             * @throws Exception 
             */  
            private static void dearchiveFile(File destFile, TarArchiveInputStream tais)  
                    throws IOException {  
                BufferedOutputStream bos = new BufferedOutputStream(  
                        new FileOutputStream(destFile));  
                int count;  
                byte data[] = new byte[BUFFER];  
                while ((count = tais.read(data, 0, BUFFER)) != -1) {  
                    bos.write(data, 0, count);  
                }  
                bos.close();  
            }  
         private static void fileProber(File dirFile) {  
            File parentFile = dirFile.getParentFile();  
            if (!parentFile.exists()) {  
                  // 递归寻找上级目录 
                fileProber(parentFile);  
                parentFile.mkdir();  
            }  
        }  
    public static void main(String[] args)  {
        // TODO Auto-generated method stub
        UnTar untar;
         try {
                untar = new UnTar("C:/Users/Desktop/test.tar","C:/Users/sgg/Desktop/test");
                untar.unTar();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}
————————————————
版权声明:本文为CSDN博主「上掊」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_28825857/article/details/86616566

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值