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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi objects,
great job! that's better than what I had hoped to do.
- tekel
great job! that's better than what I had hoped to do.
- tekel
Happy to help :-)
http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
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.