Dear All,

I have a tar file which contains .Z files. File Structure is : x.tar -> [1.Z, 2.Z]. I need to read data from this zip files.

So far I have reached a stage where I read entry from tar but am not able to unzip files within the tar. Please can you help. My code is below.


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.compress.archivers.tar.TarArchiveInputStream;

 * Extracts Tar file
 * @author Aditya.YAGNIK
class TarExtractor{
	public void extract() throws IOException{
		String filePath = "file.tar";
		FileInputStream fis = new FileInputStream(new File(filePath));
		BufferedInputStream bis = new BufferedInputStream(fis); 
		try {
			ArchiveInputStream input = new ArchiveStreamFactory().createArchiveInputStream(bis);
			if (input instanceof TarArchiveInputStream){
				System.out.println("It is a tar input stream");
				TarArchiveInputStream tarInput = (TarArchiveInputStream)input; 
				TarArchiveEntry entry = tarInput.getNextTarEntry();
				while (entry != null) {
                    String name = entry.getName();
                    System.out.println("Entry: " + name  );
                    entry = tarInput.getNextTarEntry();
		} catch (Exception e) {

	public static void main(String[] args) throws IOException{
		TarExtractor t = new TarExtractor();

Look into the import.

You can then use the GZIPInputStream/GZIPOutputStream with your stream:

InputStream in = new GZIPInputStream(new FileInputStream(file));

I've also seen the TarInputStream being used before. There are a few tutorials out there:

The class file:


Dev Daily:

Good luck!

This article has been dead for over six months. Start a new discussion instead.