Solved

untaring tar file and putting files into a specified directory

Posted on 2002-07-05
5
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

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 …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

752 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