java解压tar包

本文介绍了如何使用Java的Apache Ant库来解压tar文件。通过创建`TarInputStream`从文件输入流中读取tar文件,并逐个处理`TarEntry`,将文件解压到指定的输出目录。代码示例展示了读取tar文件、创建输出目录、处理文件及目录的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    <dependency>
    <groupId>org.apache.ant</groupId>
    <artifactId>ant</artifactId>
    <version>1.9.2</version>
</dependency>

 

package com.xzc;

import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;

import java.io.*;

public class test {

    public static void main(String[] args) {
        File file = new File("D:\\test\\exes\\tar\\tarfile.tar");
        String outputDir = file.getParent() + "/" + file.getName().replace(".tar", "");
        unTar(file,outputDir);

    }

    public static void unTar(File file , String outputDir){
        TarInputStream tarIns = null;
        InputStream ins = null;
        OutputStream out = null;
        try {
            ins = new FileInputStream(file);
            tarIns = new TarInputStream(ins);
            createDirectory(outputDir, null);//创建输出目录
            TarEntry entry = null;//每一个文件对应一个Entry
            while ((entry = tarIns.getNextEntry()) != null && !"".equals(entry.getName())){
                if(entry.isDirectory()){//目录
                    createDirectory(outputDir, entry.getName());//创建空目录

                }else{//文件
                    File tmpFile = new File(outputDir + "/" + entry.getName());
                    createDirectory(tmpFile.getParent() + "/", null);//创建输出目录

                    out = new FileOutputStream(tmpFile);
                    int len = 0;
                    byte[] bytes = new byte[1024];
                    while((len = tarIns.read(bytes)) != -1){
                        out.write(bytes, 0, len);
                    }

                }

            }
        } catch (FileNotFoundException e) {
            //log.error("文件不存在");
            //throw new BizException("文件不存在");
            e.printStackTrace();
        } catch (IOException e) {
            //log.error("解压文件失败");
            //throw new BizException("解压文件失败");
            e.printStackTrace();
        } finally {
            try {
                out.close();
                ins.close();
                tarIns.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public static void createDirectory(String outputDir, String subDir){
        File file = new File(outputDir);
        if(subDir != null && !subDir.trim().equals("")){//如果子目录不为空则创建子目录
            file = new File(outputDir + "/" + subDir);
        }
        if(!file.exists()){
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdirs();
            }
            file.mkdirs();
        }
    }
}
 

 

https://blog.youkuaiyun.com/luchaofang/article/details/88698038

https://www.cnblogs.com/haoyul/p/9662757.html

https://my.oschina.net/u/3197158/blog/911130

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值