码迷,mamicode.com
首页 > Web开发 > 详细

How to untar a TAR file using Apache Commons

时间:2017-04-24 13:17:52      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:apache   ==   cal   files   complete   char   examples   example   har   

 

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

 

How to untar a TAR file using Apache Commons

标签:apache   ==   cal   files   complete   char   examples   example   har   

原文地址:http://www.cnblogs.com/softidea/p/6756341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!