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的文件。

原文地址:https://www.cnblogs.com/zifeiy/p/9017982.html