Solved

untaring tar file and putting files into a specified directory

Posted on 2002-07-05
5
409 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
Comment Utility
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
Comment Utility
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
Comment Utility
String fileName = "tardemo.tar";
FileInputStream fis = new FileInputStream(fileName);
TarArchive ta = new TarArchive(fis);
ta.extractContents(directory);

0
 

Author Comment

by:tekel
Comment Utility
Hi objects,

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

- tekel
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Happy to help :-)

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SPLUNK REST  API call to Splunk to create and index? 2 66
Fibonacci challenge 11 82
countX 22 68
json example 39 110
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

771 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

10 Experts available now in Live!

Get 1:1 Help Now