jQuery in Action, 3rd edition
The moose likes I/O and Streams and the fly likes Untar a tar.gz file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Untar a tar.gz file" Watch "Untar a tar.gz file" New topic

Untar a tar.gz file

Pradeep KumarTR

Joined: Dec 08, 2008
Posts: 1
I used the code given at the end to untar a tar.gz file. It works fine for few files. But for some files it gives FileNotFoundException. Can someone please suggest what is wrong with the code given below?

Thanks in advance.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;

public class untarFiles

public static void main(String args[]) {

try {

untar("c:/Files/AnyFile.tar.gz",new File("c:/Files/"));

catch(Exception e) {



private static void untar(String tarFileName, File dest) throws IOException {

//assuming the file you pass in is not a dir


//create tar input stream from a .tar.gz file

TarInputStream tin = new TarInputStream( new GZIPInputStream
( new FileInputStream(new File(tarFileName))));

//get the first entry in the archive

TarEntry tarEntry = tin.getNextEntry();

while (tarEntry != null){//create a file with the same name as the tarEntry

File destPath = new File(dest.toString() + File.separatorChar + tarEntry.getName());



} else {

FileOutputStream fout = new FileOutputStream(destPath);


tarEntry = tin.getNextEntry();
Joe Ess

Joined: Oct 29, 2001
Posts: 9154

Welcome to the JavaRanch.
If your code works for some files and doesn't work with others, I'd say the problem is with the files. What is the difference between those that work and those that don't?

[How To Ask Questions On JavaRanch]
daniele danielo

Joined: Nov 04, 2009
Posts: 1
You were right! Following this advice and trying with mkdirs() made it!

I agree. Here's the link: http://aspose.com/file-tools
subject: Untar a tar.gz file
It's not a secret anymore!