How to untar a TAR file using Apache Commons

本文介绍了一个使用Java实现的Tar.gz文件解压工具类。该工具类包括解压Tar.gz文件到指定目录的功能,并能打印出每个文件的内容。使用的库包括Apache Commons Compress和Apache Commons IO。

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值