• 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

  • 相关阅读:
    Linux的文件权限与目录配置
    'scanf': This function or variable may be unsafe
    无法解析的外部符号 _main
    无法解析的外部符号 _WinMain@16
    Linux 基础命令和帮助命令
    Linux是什么与如何学习
    主机规划与磁盘分区
    三、git学习之——管理修改、撤销修改、删除文件
    git学习——git命令之创建版本库和版本退回
    git学习——git下载安装
  • 原文地址:https://www.cnblogs.com/softidea/p/6756341.html
Copyright © 2020-2023  润新知