Java 解压.tar.gz文件

1.先将文件解压为.tar文件
/**
     * @Description:解压.gz文件
     * @Author: LiTing
     * @Date: 6:25 PM 2019/11/3
     * @return:
     * @throws:
     */
    private static void unCompressArchiveGz(String archive) throws IOException {

        File file = new File(archive);

        BufferedInputStream bis = null;

        BufferedOutputStream bos = null;

        GzipCompressorInputStream gcis = null;

        String finalName = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));

            String fileName = file.getName().substring(0, file.getName().lastIndexOf("."));

            finalName = file.getParent() + File.separator + fileName;

            bos = new BufferedOutputStream(new FileOutputStream(finalName));
            try {
                gcis = new GzipCompressorInputStream(bis);
                byte[] buffer = new byte[1024];
                int read = -1;
                while ((read = gcis.read(buffer)) != -1) {
                    bos.write(buffer, 0, read);
                }
            } catch (Exception e) {
                throw e;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally { 
            if (bos != null) {
                bos.close();
            }
            if (gcis != null) {
                gcis.close();
            }
            if (bis != null) {
                bis.close();
            }
        }

        unCompressTar(finalName);
    }

2.解压.tar文件,返回解压后的文件路径
 /**
     * @Description: 解压.tar 文件
     * @Author: LiTing
     * @Date: 7:03 PM 2019/11/3
     * @return:
     * @throws:/Users/liting/Downloads/0102019071610.npd.tar.gz
     */
    private static String unCompressTar(String finalName) {
        File file = new File(finalName);
        String basePath = file.getParent() + File.separator;
        TarArchiveInputStream tais = null;
        FileOutputStream os = null;
        TarArchiveEntry entry = null;
        String finPath = null;
        try {
            tais = new TarArchiveInputStream(new FileInputStream(file));
            while ((entry = tais.getNextTarEntry()) != null) {
                // 压缩文件下存在文件夹
                if (entry.isDirectory()) {
                    // 一般不会执行
                    new File(basePath + entry.getName()).mkdirs();
                } else {
                    finPath = basePath + entry.getName();
                    File f = new File(finPath);
                    // 父文件夹不存在,创建文件夹
                    if (!f.getParentFile().exists()) {
                        f.getParentFile().mkdirs();
                    }
                    // 文件不存在,创建文件
                    if (!f.exists()) {
                        f.createNewFile();
                    }
                    os = new FileOutputStream(f);
                    byte[] bs = new byte[2048];
                    int len = -1;
                    while ((len = tais.read(bs)) != -1) {
                        os.write(bs, 0, len);
                    }
                    os.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (tais != null) {
                    tais.close();
                }
                // 解压后删除tar包
                file.delete();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return finPath;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值