• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Untar a tar.gz file

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used the code given at the end to untar a tar.gz file. It works fine for few files. But for some files it gives FileNotFoundException. Can someone please suggest what is wrong with the code given below?

Thanks in advance.



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;

public class untarFiles
{

public static void main(String args[]) {

try {

untar("c:/Files/AnyFile.tar.gz",new File("c:/Files/"));
}

catch(Exception e) {

e.printStackTrace();

System.out.println(e.getMessage());
}

}
private static void untar(String tarFileName, File dest) throws IOException {

//assuming the file you pass in is not a dir

dest.mkdir();

//create tar input stream from a .tar.gz file

TarInputStream tin = new TarInputStream( new GZIPInputStream
( new FileInputStream(new File(tarFileName))));

//get the first entry in the archive

TarEntry tarEntry = tin.getNextEntry();

while (tarEntry != null){//create a file with the same name as the tarEntry

File destPath = new File(dest.toString() + File.separatorChar + tarEntry.getName());

if(tarEntry.isDirectory()){

destPath.mkdir();

} else {

FileOutputStream fout = new FileOutputStream(destPath);

tin.copyEntryContents(fout);

fout.close();
}
tarEntry = tin.getNextEntry();
}
tin.close();
}
}
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the JavaRanch.
If your code works for some files and doesn't work with others, I'd say the problem is with the files. What is the difference between those that work and those that don't?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You were right! Following this advice and trying with mkdirs() made it!

Thanks!
 
permaculture is giving a gift to your future self. After reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic