How to untar a TAR file using Apache Commons

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.Charset;

public class IOHelper {

    public static final Logger LOGGER = LoggerFactory.getLogger(IOHelper.class);

    public static void uncompressTarGZ(File tarFile, File dest) throws IOException {
        boolean mkdirs = dest.mkdirs();
        if (!mkdirs) {
            LOGGER.warn("Unable to create directory ‘{}‘", dest.getAbsolutePath());
            return;
        }

        BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(tarFile));
        GzipCompressorInputStream gcis = new GzipCompressorInputStream(inputStream);
        try (TarArchiveInputStream tais = new TarArchiveInputStream(gcis)) {
            TarArchiveEntry entry;
            while ((entry = tais.getNextTarEntry()) != null) {// create a file with the same name as the entry
                File desFile = new File(dest, entry.getName());
                if (entry.isDirectory()) {
                    boolean mkDirs = desFile.mkdirs();
                    if (!mkDirs) {
                        LOGGER.warn("Unable to create directory ‘{}‘", desFile.getAbsolutePath());
                    }
                } else {
                    boolean createNewFile = desFile.createNewFile();
                    if (!createNewFile) {
                        LOGGER.warn("Unable to create file ‘{}‘", desFile.getCanonicalPath());
                        continue;
                    }
                    try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desFile));) {
//                        IOUtils.copy(tais, bos);
                        byte[] btoRead = new byte[1024];
                        int len;
                        while ((len = tais.read(btoRead)) != -1) {
                            bos.write(btoRead, 0, len);
                        }
                    }
                }
            }
            LOGGER.info("Untar completed successfully!");
        }
    }

    public static void printTarGzFile(File tarFile) throws IOException {
        BufferedInputStream bin = new BufferedInputStream(FileUtils.openInputStream(tarFile));
        CompressorInputStream cis = new GzipCompressorInputStream(bin);

        try (TarArchiveInputStream tais = new TarArchiveInputStream(cis)) {
            TarArchiveEntry entry;
            while ((entry = tais.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    LOGGER.warn("dir:{}", entry.getName());
                } else {
                    int size = (int) entry.getSize();
                    byte[] content = new byte[size];
                    int readCount = tais.read(content, 0, size);
                    LOGGER.info("fileName:{}", entry.getName());
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content, 0, readCount);
                    LineIterator iterator = IOUtils.lineIterator(byteArrayInputStream, Charset.forName("utf-8"));
                    try {
                        while (iterator.hasNext()) {
                            LOGGER.info("line:{}", iterator.nextLine());
                        }
                    } finally {
                        LineIterator.closeQuietly(iterator);
                    }
                }
            }
            LOGGER.info("===============finish===============");
        }
    }
}

https://commons.apache.org/proper/commons-compress/examples.html

http://stackoverflow.com/questions/7128171/how-to-compress-decompress-tar-gz-files-in-java

https://commons.apache.org/proper/commons-io/description.html

时间: 2024-08-27 08:43:17

How to untar a TAR file using Apache Commons的相关文章

How to append files to a .tar archive using Apache Commons Compress?(转)

I created a copy of the tar archive and copied to entire content to it. Then I delete the old tar archive. public void appendFileInTarArchive(String tarPath, String tarFileName, String file2WriteName, String file2WriteContent) throws AuthenticationEx

编写更少量的代码:使用apache commons工具类库

Commons-configuration   Commons-FileUpload   Commons DbUtils   Commons BeanUtils  Commons CLI  Commons Codec   Commons Collections Commons DBCP    Commons HttpClient  Commons IO  Commons JXPath   Commons Lang   Commons Math   Commons Net   Commons Va

Apache Commons

官方链接走起 http://commons.apache.org/ Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.我选了一些比较常用的项目做简单介绍.文中用了很多网上现成的东西,我只是做了一个汇总整理. Commons BeanUtils http://jakarta.apache.org/commons/beanutils/index.html 说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils

Apache Commons 简述

Apache Commons 是一个关注于可复用的 Java 组件的 Apache 项目.Apache Commons 由三部分构成: Commons Proper - 一个可复用的 Java 组件库. Commons Sandbox - Java 组件开发的工作区. Commons Dormant - 当前非活动状态的组件库. Commons Proper 致力于创建和维护可服用的 Java 组件.这些 Java 组件都是尽可能小地依赖其他的库使得组件能够方便地应用.而且这些组件的开发者尽力地

Apache Commons 工具集使用简介

Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.我选了一些比较常用的项目做简单介绍.文中用了很多网上现成的东西,我只是做了一个汇总整理. 一.Commons BeanUtils http://jakarta.apache.org/commons/beanutils/index.html 说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装. 使用示例:功能有很多,网站上有详细

(转)Apache Commons工具集简介

Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.我选了一些比较常用的项目做简单介绍.文中用了很多网上现成的东西,我只是做了一个汇总整理. 一.Commons BeanUtils http://jakarta.apache.org/commons/beanutils/index.html 说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装. 使用示例:功能有很多,网站上有详细

apache commons Java包简介

更多信息,请参考:http://commons.apache.org/ 一.Commons BeanUtils说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装. 二.Commons CLI说明:这是一个处理命令的工具.比如main方法输入的string[]需要解析.你可以预先定义好参数的规则,然后就可以调用CLI来解析. 三.Commons Codec说明:这个工具是用来编码和解码的,包括Base64,URL,Sound

Apache Commons CLI 开发命令行工具示例

概念说明Apache Commons CLI 简介 虽然各种人机交互技术飞速发展,但最传统的命令行模式依然被广泛应用于各个领域:从编译代码到系统管理,命令行因其简洁高效而备受宠爱.各种工具和系统都 提供了详尽的使用手册,有些还提供示例说明如何二次开发.然而关于如何开发一个易用.强壮的命令行工具的文章却很少.本文将结合 Apache Commons CLI,通过一个完整的例子展示如何准备.开发.测试一个命令行工具.希望本文对有相关需求的读者能有所帮助.      Apache Commons CL

Apache Commons介绍(转载)

一.Commons BeanUtils说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装. 二.Commons CLI说明:这是一个处理命令的工具.比如main方法输入的string[]需要解析.你可以预先定义好参数的规则,然后就可以调用CLI来解析. 三.Commons Codec说明:这个工具是用来编码和解码的,包括Base64,URL,Soundx等等.用这个工具的人应该很清楚这些,我就不多介绍了. 四.Common