This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Untar a tar.gz file" Watch "Untar a tar.gz file" New topic
Author

Untar a tar.gz file

Pradeep KumarTR
Greenhorn

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) {

e.printStackTrace();

System.out.println(e.getMessage());
}

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

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

dest.mkdir();

//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());

if(tarEntry.isDirectory()){

destPath.mkdir();

} else {

FileOutputStream fout = new FileOutputStream(destPath);

tin.copyEntryContents(fout);

fout.close();
}
tarEntry = tin.getNextEntry();
}
tin.close();
}
}
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8839
    
    7

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?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
daniele danielo
Greenhorn

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

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Untar a tar.gz file
 
Similar Threads
Creating a folder in C drive root
Including property file in the jar
How to make a Tar in java
Problem in creating a zip file java
Append in File with JDK 1.3