这里使用apache commons compress对.tar.Z格式文件进行解压。

对于一个文件test.tar.Z,我们可以将解压过程理解为:

  1. 将test.tar.Z解压为test.tar;
  2. 将test.tar解压为test。

根据apache commons compress的示例:

解压.Z文件示例:

InputStream fin = Files.newInputStream(Paths.get("archive.tar.Z"));
BufferedInputStream in = new BufferedInputStream(fin);
OutputStream out = Files.newOutputStream(Paths.get("archive.tar"));
ZCompressorInputStream zIn = new ZCompressorInputStream(in);
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = zIn.read(buffer))) {
out.write(buffer, 0, n);
}
out.close();
zIn.close();

解压tar文件示例:

Adding an entry to a tar archive:

TarArchiveEntry entry = new TarArchiveEntry(name);
entry.setSize(size);
tarOutput.putArchiveEntry(entry);
tarOutput.write(contentOfEntry);
tarOutput.closeArchiveEntry();

Reading entries from an tar archive:

TarArchiveEntry entry = tarInput.getNextTarEntry();
byte[] content = new byte[entry.getSize()];
LOOP UNTIL entry.getSize() HAS BEEN READ {
tarInput.read(content, offset, content.length - offset);
}

apache commons compress的maven地址如下:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.16.1</version>
</dependency>

测试程序:

package test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List; import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.z.ZCompressorInputStream;
import org.apache.commons.compress.utils.IOUtils; public class TestUncompressTarZ {
public static void compressTest() throws IOException {
// 将tar.Z解压为tar
System.out.println("uncompress tar.Z to tar...");
InputStream fin = Files.newInputStream(Paths.get("D:\\test.tar.Z"));
BufferedInputStream in = new BufferedInputStream(fin);
OutputStream out = Files.newOutputStream(Paths.get("D:\\test.tar"));
ZCompressorInputStream zIn = new ZCompressorInputStream(in);
byte[] buffer = new byte[1024];
int n = 0;
while (-1 != (n = zIn.read(buffer))) {
out.write(buffer, 0, n);
}
out.close();
zIn.close();
System.out.println("uncompress tar.Z to tar finished!");
// 将tar解压为文件夹
System.out.println("uncompress tar to directory file...");
List<String> fileNames = new ArrayList<String>();
InputStream inputStream = new FileInputStream(new File("D:\\test.tar"));
TarArchiveInputStream tarIn = new TarArchiveInputStream(inputStream, 1024);
TarArchiveEntry entry = null;
while ((entry = tarIn.getNextTarEntry()) != null) {
System.out.println("... " + entry.getName() + " , " + entry.isDirectory());
fileNames.add(entry.getName());
if (entry.isDirectory()) {//是目录
File tmpDir = new File("D:\\test\\" + entry.getName());
if (tmpDir.exists() == false) {
tmpDir.mkdirs();
}
} else {//是文件
File tmpFile = new File("D:\\test\\" + entry.getName());
File tmpDir = tmpFile.getParentFile();
System.out.println("parent: " + tmpDir.getAbsolutePath());
if (tmpDir.exists() == false) {
tmpDir.mkdirs();
}
OutputStream outputStream = new FileOutputStream(tmpFile);
int length = 0;
byte[] b = new byte[1024];
while ((length = tarIn.read(b)) != -1) {
outputStream.write(b, 0, length);
}
}
}
IOUtils.closeQuietly(tarIn);
System.out.println("uncompress tar to directory file finished!");
System.out.println("all finished!");
} public static void main(String[] args) throws IOException {
compressTest();
}
}

在这个测试程序中,首先将test.tar.Z解压为test.tar,然后将test.tar解压为一个名为test的文件。

Java解压tar.Z文件(使用Apache Commons-compress)的更多相关文章

  1. 【转】JAVA解压.TAR.Z及.ZIP文件

     解压.ZIP文件 package app.qdupr.Method; import java.io.File; import java.io.FileOutputStream; import jav ...

  2. 解压tar.gz文件报错gzip: stdin: not in gzip format解决方法

    解压tar.gz文件报错gzip: stdin: not in gzip format解决方法 在解压tar.gz文件的时候报错 1 2 3 4 5 [Sun@localhost Downloads] ...

  3. tar解压.tar.bz2文件失败:tar: Error is not recoverable: exiting now

    使用tar解压.tar.bz2文件: tar -jxvf xxxx.tar.bz2 报如下错误: 原因:未安装bzip yum -y install bzip2

  4. [Linux] 解压tar.gz文件,解压部分文件

    遇到数据库无法查找问题原因,只能找日志,查找日志的时候发现老的日志都被压缩了,只能尝试解压了   数据量比较大,只能在生产解压了,再进行查找 文件名为*.tar.gz,自己博客以前记录过解压方法: h ...

  5. 【java】 java 解压tar.gz读取内容

    package com.xwolf.stat.util; import com.alibaba.druid.util.StringUtils; import com.alibaba.fastjson. ...

  6. Linux使用shell解压tar.Z格式文件

    建设当前目录下有一个名为test.tar.Z的文件. 使用如下指令可以将其解压,并将解压后的所有文件放置在当前目录下: zcat test.tar.Z | tar -xvf - 如果想要将解压缩的文件 ...

  7. Linux(Ubuntu) 下如何解压 .tar.gz 文件

    在终端输入以下命令即可解压: tar -zxvf YOUR_FILE_NAME.tar.gz 如果出现“权限不够”的错误提示,在命令前加上 sudo ,即 sudo tar -zxvf YOUR_FI ...

  8. linux命令(及解压tar.gz文件)

    https://wenku.baidu.com/view/f5805017866fb84ae45c8df3.html 1.压缩命令: 命令格式:tar  -zcvf   压缩文件名.tar.gz   ...

  9. tar命令--数据解档(三)解压.tar.gz文件报错 gzip:stdin:not in gzip format

    毕竟是生产..... 提示以下信息:  gzip: stdin: not in gzip format  tar: Child returned status 1  tar: Error is not ...

随机推荐

  1. Git----查看提交日志

    Git log 只包括当前分支的commit. 截图示例: Git reflog 显示整个本地仓储的commit(所有branch,包括已撤销的commit) 截图示例: Git reflog --r ...

  2. CSS float详解

    前言:在我们写CSS样式的时候,float,position,display,overflow这几个关键字用得比较多. 弄清楚他们之间的原理,我们可以更高效的写出我们想要的布局. 作者:Ry-yuan ...

  3. Codeforces Round #413 (Div1 + Div. 2) C. Fountains(树状数组维护最大值)

    题目链接:https://codeforces.com/problemset/problem/799/C 题意:有 c 块硬币和 d 块钻石,每种喷泉消耗硬币或钻石中的一种,每个喷泉有一个美丽值,问建 ...

  4. javascript权威指南第12章DOM2 DOM3 示例代码

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> Example XHTML ...

  5. 原生JS实现九宫格拼图

    实现这个案例,需要考虑到鼠标的拖拽效果(onmousedown/onmousemove/mouseup) 拖拽分解: 按下鼠标---->移动鼠标----->松开鼠标 1.给目标元素添加on ...

  6. Flume实时监控目录sink到hdfs,再用sparkStreaming监控hdfs的这个目录,对数据进行计算

    目标:Flume实时监控目录sink到hdfs,再用sparkStreaming监控hdfs的这个目录,对数据进行计算 1.flume的配置,配置spoolDirSource_hdfsSink.pro ...

  7. React组件属性/方法/库属性

    1. propTypes 用于进行props的类型检查:来自于prop-types库. // V15.5之后 import PropTypes from 'prop-types'; 该方法适用于函数组 ...

  8. 008_Python3 列表

           序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表 ...

  9. AS400遇到的一些问题和解决办法

    1.没有权限进入distribution directory wrklnk 'QDLS\'    >User not enrolled in system distribution direct ...

  10. 重载new和delete运算符

    内存管理运算符 new.new[].delete 和 delete[] 也可以进行重载,其重载形式既可以是类的成员函数,也可以是全局函数.一般情况下,内建的内存管理运算符就够用了,只有在需要自己管理内 ...