Solved

untaring tar file and putting files into a specified directory

Posted on 2002-07-05
5
412 Views
Last Modified: 2008-02-01
Hi all,

I am using com.ice.tar to work with a tar file (if you can recommend a better utility class for me that would be good too).  

here is what I want to do:
1 - read a tar file (tardemo.tar)
2 - extract the files to a directory

I am able to do (1) but I have problems retrieving the files.

here is some code:
======= start of code snippet
String fileName = "tardemo.tar";
FileInputStream fis = new FileInputStream(fileName);
TarInputStream tis = new TarInputStream(fis);

TarEntry te = null;
File entryFile = null;

while (null != (te = tis.getNextEntry())){
     if (!te.isDirectory()){
     entryFile = te.getFile();
     }
}
======= end of code snippet

it turns out that the code "te.getFile()" is always returning null.  but when I do te.getName() I a get the name of the file.  And te.getSize() gives me the right size for the each file so I don't think my tar file is corrupt.  I guess, somehow, the file is not being stored in the TarEntry object as a protected variable?

can anyone offer suggestions on how to go about solving this problem?  Or, does someone have any ample code that might shed some light on the subject?

thanks,
- tekel
0
Comment
Question by:tekel
  • 4
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7133076
tekel,

I'd say that getFile() only returns the file if you are creating the tar file, and it will point to the file to be tarred.
In the case of untarring the file would not exist so it is returing null.
0
 
LVL 92

Expert Comment

by:objects
ID: 7133079
To extract the files from the tar, you'll need to either use the TarArchive class, or read files individually using the TarInputStream.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7133083
String fileName = "tardemo.tar";
FileInputStream fis = new FileInputStream(fileName);
TarArchive ta = new TarArchive(fis);
ta.extractContents(directory);

0
 

Author Comment

by:tekel
ID: 7134418
Hi objects,

great job! that's better than what I had hoped to do.

- tekel
0
 
LVL 92

Expert Comment

by:objects
ID: 7134421
Happy to help :-)

http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now