一、文件格式介绍
最近从Linux系统中copy了一个tar。z压缩文件,用于解压测试。
文件格式为*.tar压缩包下,还有*.z压缩包
如:压缩包paid_orderlist_20170920.tar,该tar包下有一个paid_orderlist_20170920.z压缩包,z包下有一个数据文件paid_orderlist_20170920.dat
即如果获取paid_orderlist_20170920.dat文件,需要分别解压tar包和z包。
二、文件解压(java实现)
1.前言
目前网络上解压*.tar文件以及其他压缩格式文件的java例子很多,大多都使用的是
commons-compress-1.x.jar或者javatar-2.x.jar工具进行解压操作。但真正实现解压tar.z文件的例子确不多见。
tar.z格式作为linxu中常见压缩文件格式,java实现解压操作可能会在项目中经常遇到。在参照网上例子及个人实践总结后,成功实现了对该文件的解压操作。
2.jar包准备
所需jar包:commons-compress-1.x.jar(网上介绍说需1.7版本及以上,反正亲测1.4版本不行,下载了1.7版本测试成功)
推荐maven下载:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.7</version>
</dependency>
3.java代码实现
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 java.io.*;
import java.util.*;
/**
* java解压tar.z文件
*/
public class TarZUtil{
public static void main(String[] args) {
File tarFile= new File("E:\\TARZ\\paid_orderlist_20170920.tar");
List<String> zFileNames = unTarFile(tarFile);
for (String zFileName:zFileNames ){
File zFile = new File("E:\\TARZ\\"+zFileName);
unZFile(zFile,"E:\\TARZ\\");
}
}
/**
* 解压*.z文件 * @param file z包文件
* @param outPath z包下解压后文件存放路径
* @return 解压后文件
*/
public static File unZFile(File file,String outPath) {
int buffersize = 2048;
FileOutputStream out = null;
ZCompressorInputStream zIn = null;
try {
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
// 若获取z文件名,最好使用lastIndexOf,不要使用indexOf,比如paid_orderlist_20170920.test.z
String name = file.getName() .substring(0, file.getName().lastIndexOf("."));
File outFile = new File(outPath+File.separator + name);
out = new FileOutputStream(outFile);
zIn = new ZCompressorInputStream(in);
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = zIn.read(buffer))) {
out.write(buffer, 0, n);
}
return outFile;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
finally {
try {
out.close();
zIn.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* .TAR文件解压
* @param file
*/
public static List<String> unTarFile(File file) {
int buffersize = 2048;
String basePath = file.getParent() + File.separator;
TarArchiveInputStream is = null;
// 存储tar包下所有z文件名
List<String> zFileNames = new ArrayList<String>();
try {
is = new TarArchiveInputStream(new FileInputStream(file));
while (true) {
TarArchiveEntry entry = is.getNextTarEntry();
if (entry == null) {
break;
}
zFileNames.add(entry.getName());
if (entry.isDirectory()) {
// 一般不会执行
new File(basePath + entry.getName()).mkdirs();
} else {
FileOutputStream os = null;
try {
File f = new File(basePath + entry.getName());
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
if (!f.exists()) {
f.createNewFile();
}
os = new FileOutputStream(f);
byte[] bs = new byte[buffersize];
int len = -1;
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
os.close();
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
is.close();
// 解压后删除tar包
// file.delete();
}
catch (IOException e) {
e.printStackTrace();
}
}
// 返回tar包下所有文件名
return zFileNames;
}
}
参照:http://www.cnblogs.com/w821759016/p/6210818.html