Java解压tar.Z文件(使用Apache Commons-compress)

Posted <・)))><<

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java解压tar.Z文件(使用Apache Commons-compress)相关的知识,希望对你有一定的参考价值。

这里使用apache commons compress对.tar.Z格式文件进行解压。
对于一个文件test.tar.Z,我们可以将解压过程理解为:

  1. 将test.tar.Z解压为test.tar;
  2. 将test.tar解压为test。

根据apache commons compress的示例:

解压.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 调用linux命令 解压 tar.Z 包的路径问题

Linux使用shell解压tar.Z格式文件

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

Linux解压.tar .tgz .tar.gz .tar.Z等文件

Linux解压.tar .tgz .tar.gz .tar.Z等文件(转)

Linux 文件的压缩与解压