Java解压tar.Z文件(使用Apache Commons-compress)
这里使用apache commons compress对.tar.Z格式文件进行解压。
对于一个文件test.tar.Z,我们可以将解压过程理解为:
- 将test.tar.Z解压为test.tar;
- 将test.tar解压为test。
解压.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)的更多相关文章
- 【转】JAVA解压.TAR.Z及.ZIP文件
解压.ZIP文件 package app.qdupr.Method; import java.io.File; import java.io.FileOutputStream; import jav ...
- 解压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] ...
- tar解压.tar.bz2文件失败:tar: Error is not recoverable: exiting now
使用tar解压.tar.bz2文件: tar -jxvf xxxx.tar.bz2 报如下错误: 原因:未安装bzip yum -y install bzip2
- [Linux] 解压tar.gz文件,解压部分文件
遇到数据库无法查找问题原因,只能找日志,查找日志的时候发现老的日志都被压缩了,只能尝试解压了 数据量比较大,只能在生产解压了,再进行查找 文件名为*.tar.gz,自己博客以前记录过解压方法: h ...
- 【java】 java 解压tar.gz读取内容
package com.xwolf.stat.util; import com.alibaba.druid.util.StringUtils; import com.alibaba.fastjson. ...
- Linux使用shell解压tar.Z格式文件
建设当前目录下有一个名为test.tar.Z的文件. 使用如下指令可以将其解压,并将解压后的所有文件放置在当前目录下: zcat test.tar.Z | tar -xvf - 如果想要将解压缩的文件 ...
- Linux(Ubuntu) 下如何解压 .tar.gz 文件
在终端输入以下命令即可解压: tar -zxvf YOUR_FILE_NAME.tar.gz 如果出现“权限不够”的错误提示,在命令前加上 sudo ,即 sudo tar -zxvf YOUR_FI ...
- linux命令(及解压tar.gz文件)
https://wenku.baidu.com/view/f5805017866fb84ae45c8df3.html 1.压缩命令: 命令格式:tar -zcvf 压缩文件名.tar.gz ...
- tar命令--数据解档(三)解压.tar.gz文件报错 gzip:stdin:not in gzip format
毕竟是生产..... 提示以下信息: gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not ...
随机推荐
- 集成百度编辑器 ueditor 后端配置项没有正常加载,上传插件不能正常使用!
项目要用到编辑器,于是集成了ueditor,集成ok,但一直显示 ‘’后端配置项没有正常加载,上传插件不能正常使用!‘’ 各种查: 网上说的无非就是那么集中情况 1. 因为百度官方的问题,php/co ...
- BZOJ 2839: 集合计数 广义容斥
在一个 $N$ 个元素集合中的所有子集中选择若干个,且交集大小为 $k$ 的方案数. 按照之前的套路,令 $f[k]$ 表示钦定交集大小为 $k$,其余随便选的方案数. 令 $g[k]$ 表示交集恰好 ...
- luogu 1144
最短路计数 #include <bits/stdc++.h> using namespace std; , M = 2e6 + ; << ); #define gc getch ...
- centos7mongo集群
1.安装 cat > /etc/yum.repos.d/mongodb.repo << EOF[mongodb-org-3.6]name=MongoDB Repositorybase ...
- 在 Ubuntu 18.04 /centos7上安装 Python 3.7
扩展源安装 sudo apt update sudo apt install software-properties-common sudo add-apt-repository ppa:deadsn ...
- codeforces396A
sol:很显然就是找出所有质因数,然后分别塞进去就行了,怎么塞就是组合数.感觉就是道小学奥数题 #include <bits/stdc++.h> using namespace std; ...
- 【原】Python基础-函数
#不定长参数,这里prams是一个元组集合def print_params(*prams): for e in prams: print(e) print(prams) #输出('xxx', (1, ...
- lucene IndexOptions可以设置DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS DOCS,ES里也可以设置
org.apache.lucene.index Enum Constants Enum Constant and Description DOCS_AND_FREQS Only documents ...
- 详解JDBC对象
1. DriverManager (1) 注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); 真正注册驱动的是驱动包下 jdbc 文件夹 ...
- SQLite3中的日期时间函数使用小结
代码如下: import sqlite3conn = sqlite3.connect('/tmp/sqlite.db')cur = conn.cursor() 接下来干嘛呢?建一张表吧.这里需要注意的 ...