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说明:这 ...
随机推荐
- java数据类型易错点简单总结,欢迎大神前辈补充!谢谢
数据类型那这边看似简单,花了我很长时间也就是才练到几成"功力"吧.还希望路过的大神在下面补充,菜鸟的我深受感谢! 首先看两个思考题 思考题1:请问下面这个有没有问题 double ...
- AngularJS进阶(六)AngularJS+BootStrap实现弹出对话框
AngularJS+BootStrap实现弹出对话框 参考资料: http://angular-ui.github.io/bootstrap/#/modal https://www.zybuluo.c ...
- ActiveMQ系列之一:ActiveMQ简介
ActiveMQ是什么 ActiveMQ是Apache推出的,一款开源的,完全支持JMS1.1和J2EE 1.4规范的JMS Provider实现的消息中间件 (Message Oriented ...
- LeetCode之旅(19)-Power of Two
题目 Given an integer, write a function to determine if it is a power of two. Credits: Special thanks ...
- LeetCode(29)-Plus One
题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...
- css选择器语法速查
通用选择器 *{} 类似于通配符,匹配文档中所有元素类型: 类型选择器 h1,h2,p{} 匹配以逗号隔开元素列表中的所有元素 类选择器 .glass{} or p.glass{} id选择器 #id ...
- Spring Boot 2.0系列文章(七):SpringApplication 深入探索
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...
- 全面解读Java NIO工作原理(4)
全面解读Java NIO工作原理(4) 2011-12-14 10:31 Rollen Holt Rollen Holt的博客 我要评论(0) 字号:T | T JDK 1.4 中引入的新输入输出 ( ...
- Hyper Text Transfer Protocol(超文本传输协议)
HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...
- vue-cli目录结构