Solved

untaring tar file and putting files into a specified directory

Posted on 2002-07-05
5
415 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

773 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