java 解压tar,tar.z、tar.gz、tgz、gz等压缩包

原来也是对这些压缩包的操作不熟悉的,后来搞了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;
        }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值