原来也是对这些压缩包的操作不熟悉的,后来搞了datax,其中ftpreader插件,对其功能做了一些扩展,就涉及到压缩包的相关操作了
首先导入ftp和压缩包的相关依赖
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.9</version>
</dependency>
tar.z、tar.gz、tgz、gz这些都是压缩包
需要用到解压流去解析,解压之后到一个新路径,返回一个解压文件的集合
public static Set<String> localFiles = null;
public Set<String> unGzip(String fileName, String unZipPath) {
Set<String> set = new HashSet<>();
try {
InputStream inputStream = ftpHelper.getInputStream(fileName);
TarArchiveInputStream fin = new TarArchiveInputStream(new GzipCompressorInputStream(inputStream));
System.out.println("********开始执行gzip");
TarArchiveEntry entry;
// 将 tar 文件解压到 extractPath 目录下
while ((entry = fin.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
//目标文件位置
File curfile = new File(unZipPath + entry.getName());
File parent = curfile.getParentFile();
System.out.println("***文件保存的路径" + curfile.getAbsolutePath());
if (!parent.exists()) {
parent.mkdirs();
}
// 将文件写出到解压的目录
IOUtils.copy(fin, new FileOutputStream(curfile));
set.add(curfile.getAbsolutePath());
}
} catch (IOException e) {
e.printStackTrace();
}
if (localFiles == null) {
localFiles = set;
} else {
localFiles.addAll(set);
}
return set;
}
tar包的解压就简单了,
// 解压tar包
public Set<String> unTar(String fileName, String unZipPath) {
Set<String> set = new HashSet<>();
try {
InputStream inputStream = ftpHelper.getInputStream(fileName);
TarArchiveInputStream fin = new TarArchiveInputStream(inputStream);
System.out.println("********开始执行tar");
// File extraceFolder = new File(unZipPath);
TarArchiveEntry entry;
// 将 tar 文件解压到 extractPath 目录下
while ((entry = fin.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
System.out.println("*********创建目录开始" + unZipPath);
boolean flag = ftpHelper.isDirExistCreate(unZipPath);
System.out.println("***********解压目录创建结束" + flag);
System.out.println("***文件名称" + entry.getName());
File curfile = new File(unZipPath + entry.getName());
File parent = curfile.getParentFile();
System.out.println("***文件保存的路径" + curfile.getAbsolutePath());
if (!parent.exists()) {
parent.mkdirs();
}
set.add(curfile.getAbsolutePath());
// 将文件写出到解压的目录
IOUtils.copy(fin, new FileOutputStream(curfile));
}
//fin.close();
} catch (IOException e) {
e.printStackTrace();
}
if (localFiles == null) {
localFiles = set;
} else {
localFiles.addAll(set);
}
return set;
}