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进程管理(第二版) --计划任务
计划任务 一.一次性计划任务 月11日) at 5:30pm at 17:30 [today] #today可省略 at now + 3 hours at now + 180 minutes at 1 ...
- linux开发调试工具---GDB的使用
编译 gcc -g xxx.c -o xxxgdb gdb xxxgdb 进入gdb调试界面 查看代码 list ==> l 设置断点 可以以函数为断点 / 行数 break ...
- ZeroC Ice IceGrid Node和IceGrid
IceGrid Node介绍 绝大多数分布式系统都有一个共同特点,即分布在各个主机上的节点进程并不是完全独立的,而是彼此之间有相互联系和通信的.集群对集群中的节点有一些控制指令,如部署.启停或者调整某 ...
- PS 滤镜算法原理——拼贴
%%%% Tile %%%%% 实现拼贴效果 %%%%% 将原图像进行分块,然后让图像块在 %%%%% 新图像范围内进行随机移动,确定移动后的边界 %%%%% 将移动后的图像块填入新图像内 clc; ...
- 【5】-阿里面试题android网络图片加载优化
题目: 遇到网络不好,卡顿的时候如何对网络的加载图片进行优化? 思路: 从加载的图片的本身和手机的存储两方面考虑 解决办法: 1.找现有图片格式的替换者 在众多的图片格式中,选择了Google的Web ...
- 杭电ACM 1002题
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(S ...
- async & await 异步编程的一点巧方法
await 关键字不会创建新的线程,而是由Task任务或是FCL中的xxxAsync等方法创建的线程,而且这里创建的线程都是基于线程池创建的工作线程,属于后台线程. await关键字会阻塞/暂停调用它 ...
- OOP的基本原则
OOP的基本原则 点击打开链接
- JVM组成
java内存组成介绍:堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动时 ...
- 2013-8:SDCE大会笔记
百度移动云三大开发框架:Clouda,SiteApp,AppBuilder MBaaS解决高性能Server很难的问题 百度开放云的区域运营服务于创业者 Pinterest架构变迁: 互联网就是把线下 ...