Link to home
Start Free TrialLog in
Avatar of tekel
tekel

asked on

untaring tar file and putting files into a specified directory

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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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.
To extract the files from the tar, you'll need to either use the TarArchive class, or read files individually using the TarInputStream.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tekel
tekel

ASKER

Hi objects,

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

- tekel