java不解压tar.gz读取包里面的某个文件内容

Posted james-roger

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java不解压tar.gz读取包里面的某个文件内容相关的知识,希望对你有一定的参考价值。

<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-compress</artifactId>
			<version>1.18</version>
		</dependency>

  

package com.example.demo.xml2map;

import java.io.*;

/**
 * @Author: luojie
 * @Date: 2020/5/13 8:19
 */
public class FileUtil {

    public static byte[] getContent(File file) {
        try {
            return getContent(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return new byte[]{};
    }

    public static byte[] getContent(InputStream is) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[1024];
            //byte[] buffer = new byte[16 * 1024];
            while (true) {
                int len = is.read(buffer);
                if (len == -1) {
                    break;
                }
                baos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

}

  

package com.example.demo.xml2map;

import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.zip.GZIPInputStream;

/**
 * @Author: luojie
 * @Date: 2020/5/13 8:21
 */
public class TarGz2Test {

    public static void readTarFile(File tarFile){
        String orginFileName = tarFile.getName();
        try {
            ArchiveInputStream archiveInputStream = null;
            if (StringUtils.endsWithIgnoreCase(tarFile.getName(), ".gz")) {
                archiveInputStream = new ArchiveStreamFactory()
                        .createArchiveInputStream("tar", new GZIPInputStream(new BufferedInputStream(new FileInputStream(tarFile))));
            } else {
                archiveInputStream = new ArchiveStreamFactory()
                        .createArchiveInputStream("tar", new BufferedInputStream(new FileInputStream(tarFile)));
            }
            TarArchiveEntry entry = null;
            while ((entry = (TarArchiveEntry) archiveInputStream.getNextEntry()) != null) {

                if (entry.getSize() > 0) {

                    String fileName = orginFileName.substring(0, orginFileName.indexOf(".tar.gz")) + "-MSS1.xml";
                    if(fileName.equalsIgnoreCase(entry.getName())){
                        System.out.println("fileSize is " + entry.getSize());
                        byte[] bytes = FileUtil.getContent(archiveInputStream);
                        System.out.println(new String(bytes));
                    }

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        readTarFile(new File("E:\\satellite\\GF1_PMS1_E109.2_N38.3_20170803_L1A0002525088.tar.gz"));
//        System.out.println(JSON.toJSONString(data));
    }
}

  

以上是关于java不解压tar.gz读取包里面的某个文件内容的主要内容,如果未能解决你的问题,请参考以下文章

tar 解压某个指定的文件或者文件夹

linux不解压直接查看tar.gz压缩包里的内容

linux不解压直接查看tar.gz压缩包里的内容

tar压缩解压文件

java java 解压tar.gz读取内容

如何只查看tar.gz压缩文件中顶层目录的列表