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的更多相关文章

  1. 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 ar ...

  2. docker pull报错failed to register layer: Error processing tar file(exit status 1): open permission denied

    近来在一个云主机上操作docker pull,报错如下: failed to register layer: Error processing ): open /etc/init.d/hwclock. ...

  3. apache.commons.compress 压缩,解压

    最近在一个前辈的指引下,开始研究apache.commons.都是网上找的,而且不会中文乱码,而且还可以在压缩包里面加一层文件夹 package my.test; import java.io.Buf ...

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

    Commons-configuration   Commons-FileUpload   Commons DbUtils   Commons BeanUtils  Commons CLI  Commo ...

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

    概念说明Apache Commons CLI 简介 虽然各种人机交互技术飞速发展,但最传统的命令行模式依然被广泛应用于各个领域:从编译代码到系统管理,命令行因其简洁高效而备受宠爱.各种工具和系统都 提 ...

  6. apache commons Java包简介

    更多信息,请参考:http://commons.apache.org/ 一.Commons BeanUtils说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanU ...

  7. Apache Commons 简述

    Apache Commons 是一个关注于可复用的 Java 组件的 Apache 项目.Apache Commons 由三部分构成: Commons Proper - 一个可复用的 Java 组件库 ...

  8. Apache Commons 工具集使用简介

    Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.我选了一些比较常用的项目做简单介绍.文中用了很多网上现成的东西,我只是做了一个汇总整理. 一.Comm ...

  9. Apache Commons介绍(转载)

    一.Commons BeanUtils说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装. 二.Commons CLI说明:这 ...

随机推荐

  1. Android绘图机制(一)——自定义View的基础属性和方法

    Android绘图机制(一)--自定义View的基础属性和方法 自定义View看起来,确实看起来高深莫测,很多Android开发都不是特别在行这一块,这里面的逻辑以及一些绘画都是有一点难的,说一下我目 ...

  2. Markdown语法及编辑器

    宗旨 Markdown 的目标是实现「易读易写」. 可读性,无论如何,都是最重要的.一份使用 Markdown 格式撰写的文件应该可以直接以纯文本发布,并且看起来不会像是由许多标签或是格式指令所构成. ...

  3. 关于减少BUG的思考

    开发之前,就要先设计,理清好思路:如果需求都不清楚,软件肯定有缺陷: 和客户.测试的沟通 如果开发出来的东西本身就有BUG,交给测试,会浪费很多人的时间. 开发做完一个功能后,要自己做一遍测试 自己的 ...

  4. oracle索引建立和删除

    1.多列建立索引 SQL> create index dex_index2 on dex(sex,name); Index created. SQL> select object_name ...

  5. 用python开发调试器——起始篇

    首先,你得准备一套python开发环境,正常情况下,一般是在windows下开发的,因为win系统应用广泛,再则就是要有个IDE,这里我选择我熟悉的Eclipse.环境搭建,网上都有,比如:http: ...

  6. Jbpm工作流(一)

    了解一下什么是Jbpm及特点. jBPM,全称是Java Business Process Management,是一种基于J2EE的轻量级工作流管理系统.jBPM是公开源代码项目,它使用要遵循 Ap ...

  7. Servlet 学习总结

    Servlet资料整理[很全很强大] 分类: J2EE2009-10-23 00:51 671人阅读 评论(0) 收藏 举报 servletsessionstring服务器initialization ...

  8. Pod install 之后 no such module

    官方文档在pod install之后的操作是: open App.xcworkspace 使用pod以后,项目的旧打开方式就不行了,必须到项目目录里面,打开“项目名.xcworkspace”这种方式来 ...

  9. Ocelot中文文档-转换Claims

    Ocelot允许用户访问claims并把它们转换到头部,请求字符串参数和其他claims中.这仅在用户通过身份验证后才可用. 用户通过身份验证之后,我们运行claims转换中间件.这个中间件允许在授权 ...

  10. eBay账号token授权

    1.注册开发者账号(https://go.developer.ebay.com/) hufangyong   hu6253859. 2.注册沙箱测试账号(http://sandbox.ebay.com ...