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说明:这 ...
 
随机推荐
- Linux文件与目录的默认权限与隐藏权限 - umask, chattr, lsattr, SUID, SGID, SBIT, file
			
文件默认权限:umask [root@www ~]# umask 0022 <==与一般权限有关的是后面三个数字! [root@www ~]# umask -S u=rwx,g=rx,o=rx ...
 - 速度之王 — LZ4压缩算法(二)
			
LZ4 (Extremely Fast Compression algorithm) 项目:http://code.google.com/p/lz4/ 作者:Yann Collet 本文作者:zhan ...
 - Unity PUN插件多人在线同步角色坐标旋转角度和动作
			
用PUN插件的话,就在OnJoinedRoom()回调函数里,表示加入房间,可以实例化角色,GameObject go=PhotonNetwork.Instantiate(prefabPlayer.n ...
 - Hbase问题
			
Q: .meta.和root表是否要分裂? A: meta表和root表不会分裂,代码中有所判断. Q: 如果不分裂,那么都只有1个region? A: ... (查看代码后)A: meta和root ...
 - 机器学习算法与Python实践之(五)k均值聚类(k-means)
			
机器学习算法与Python实践这个系列主要是参考<机器学习实战>这本书.因为自己想学习Python,然后也想对一些机器学习算法加深下了解,所以就想通过Python来实现几个比较常用的机器学 ...
 - ruby中printf "%x"%-4为何会打印开头..
			
先看一下ruby中printf "%x" % -4的返回结果: irb(main):134:0> printf "%x\n" % -4 ..fc 前面的. ...
 - Java IO学习--(一)概述
			
在这一小节,我会试着给出Java IO(java.io)包下所有类的概述.更具体地说,我会根据类的用途对类进行分组.这个分组将会使你在未来的工作中,进行类的用途判定时,或者是为某个特定用途选择类时变得 ...
 - IT轮子系列(二)——mvc API 说明文档的自动生成——Swagger的使用(一)
			
这篇文章主要介绍如何使用Swashbuckle插件在VS 2013中自动生成MVC API项目的说明文档.为了更好说明的swagger生成,我们从新建一个空API项目开始. 第一步.新建mvc api ...
 - JVM内存详解-阅读笔记
 - javap
			
本词条缺少概述.信息栏.名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! javap是jdk自带的一个工具,可以反编译,也可以查看java编译器生成的字节码,是分析代码的一个好工具. j ...