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
How to untar a TAR file using Apache Commons的更多相关文章
- 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 ...
- 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. ...
- apache.commons.compress 压缩,解压
最近在一个前辈的指引下,开始研究apache.commons.都是网上找的,而且不会中文乱码,而且还可以在压缩包里面加一层文件夹 package my.test; import java.io.Buf ...
- 编写更少量的代码:使用apache commons工具类库
Commons-configuration Commons-FileUpload Commons DbUtils Commons BeanUtils Commons CLI Commo ...
- Apache Commons CLI 开发命令行工具示例
概念说明Apache Commons CLI 简介 虽然各种人机交互技术飞速发展,但最传统的命令行模式依然被广泛应用于各个领域:从编译代码到系统管理,命令行因其简洁高效而备受宠爱.各种工具和系统都 提 ...
- apache commons Java包简介
更多信息,请参考:http://commons.apache.org/ 一.Commons BeanUtils说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanU ...
- Apache Commons 简述
Apache Commons 是一个关注于可复用的 Java 组件的 Apache 项目.Apache Commons 由三部分构成: Commons Proper - 一个可复用的 Java 组件库 ...
- Apache Commons 工具集使用简介
Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动.我选了一些比较常用的项目做简单介绍.文中用了很多网上现成的东西,我只是做了一个汇总整理. 一.Comm ...
- Apache Commons介绍(转载)
一.Commons BeanUtils说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装. 二.Commons CLI说明:这 ...
随机推荐
- LeetCode之“数组”:Rotate Array
题目链接 题目要求: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, ...
- 第十八篇 ANDROID的声音管理系统及服务
声音管理系统用来实现声音的输入和输出.声音的控制和路由等功能,包括主和各种音源的音量调节.声音焦点控制,声音外设的检测和状态管理,声音源输入和输出的策略管理.音效的播放.音轨设置和播放.录音设置 ...
- 【nginx】4xx,5xx 保持自定义header
问题 nginx使用中,如果请求返回的状态code类似404或者50x这种,仍然返回自定义的header. 分析和解决 nginx文档中关于 add_header的部分 有这么一句 Adds the ...
- 和菜鸟一起学产品之用户体验设计UED
ps:参考产品经理深入浅出ppt
- Django之admin的使用和源码剖析
admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLE ...
- java并发包分析之———AQS框架
一.什么是同步器 多线程并发的执行,之间通过某种 共享 状态来同步,只有当状态满足 xxxx 条件,才能触发线程执行 xxxx . 这个共同的语义可以称之为同步器.可以认为以上所有的锁机制都可以基 ...
- python中的类机制
一.python中的对象 1.python中对象种类及关系 <type 'type'>:该对象可以成为其他类的类型,python中几乎所有对象都是直接或间接由<type 'type' ...
- nvm使用笔记
1.先发个中文博客的链接:http://www.cnblogs.com/kaiye/p/4937191.html 2.安装node版本的命令问题,版本号前面要加v,安装6.9.1的正确命令是: nvm ...
- Aptana版本回滚的方法
最近Aptana对Django1.7的编译支持有点问题,开发环境必须使用Django1.6版本,今天看了一眼它的官网,版本已经到3.6.1,我的版本还是3.4.2,就checkupdate升级到3.6 ...
- HTML5学习系列之表单与文件
article元素 article元素代表文档.页面或应用程序中独立的.完整的.可以独自被外部引用的内容.它可以是一篇博客或报刊中的文章.一篇论坛帖子.一段用户评论或独立的插件,或者其他任何独立的内容 ...