How to untar a TAR file using Apache Commons

简介: import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.

 

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

 

相关文章
|
Apache
java.lang.NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream
java.lang.NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream
550 0
|
Java Apache Maven
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决方法
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决方法
1336 0
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决方法
|
5月前
|
Java 数据库连接 Apache
深入理解Apache Commons Pool2池化技术
深入理解Apache Commons Pool2池化技术
|
6月前
|
算法 Java Apache
Apache Commons
Apache Commons是一个开源项目,提供了一系列的工具和库,用于简化Java开发中的常见任务。
65 1
|
移动开发 前端开发 Java
Spring MVC-09循序渐进之文件上传(基于Apache Commons FileUpload)
Spring MVC-09循序渐进之文件上传(基于Apache Commons FileUpload)
102 0
|
Java 应用服务中间件 Apache
nested exception is java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim
nested exception is java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim
360 0
|
Java Apache
java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory
java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory
104 0
|
存储 Java Apache
java积累——apache commons fileupload 实现文件上传
java积累——apache commons fileupload 实现文件上传
365 0
|
编解码 算法 Apache
Apache Commons Codec:各种加密了解一下
Apache Commons Codec 简介 Apache Commons Codec: Apache Commons Codec (TM) software provides implementations of common encoders and decoders such as Base64, Hex, Phonetic and URLs. Apache Commons Codec(TM)软件提供常见编码器和解码器的实现,如Base64,Hex,Phonetic和URL。
2910 0
|
Apache
Apache Commons DbUtils工具使用
Apache Commons DbUtils工具使用
150 0
Apache Commons DbUtils工具使用

推荐镜像

更多