简体   繁体   中英

Extracting tar.gz using java error

I am trying to extract an archive .tar.gz using java and I am getting Directory error that I do not seem to understand. Please help. I got this sample code from https://forums.oracle.com/forums/thread.jspa?threadID=2065236

package untargz;
import java.io.*;
import com.ice.tar.*;
import javax.activation.*;
import java.util.zip.GZIPInputStream;
/**
 *
 * @author stanleymungai
 */
public class Untargz {
public static InputStream getInputStream(String tarFileName) throws Exception{

      if(tarFileName.substring(tarFileName.lastIndexOf(".") + 1, tarFileName.lastIndexOf(".") + 3).equalsIgnoreCase("gz")){
         System.out.println("Creating an GZIPInputStream for the file");
         return new GZIPInputStream(new FileInputStream(new File(tarFileName)));

      }else{
         System.out.println("Creating an InputStream for the file");
         return new FileInputStream(new File(tarFileName));
      }
   }

    private static void untar(InputStream in, String untarDir) throws IOException {

      System.out.println("Reading TarInputStream... ");
      TarInputStream tin = new TarInputStream(in);
      TarEntry tarEntry = tin.getNextEntry();
      if(new File(untarDir).exists()){
          while (tarEntry != null){
             File destPath = new File(untarDir + File.separatorChar + tarEntry.getName());
             System.out.println("Processing " + destPath.getAbsoluteFile());
             if(!tarEntry.isDirectory()){
                FileOutputStream fout = new FileOutputStream(destPath);
                tin.copyEntryContents(fout);
                fout.close();
             }else{
                destPath.mkdir();
             }
             tarEntry = tin.getNextEntry();
          }
          tin.close();
      }else{
         System.out.println("That destination directory doesn't exist! " + untarDir);
      }     
    }   
    private void run(){ 
        try {           
            String strSourceFile = "C:/AskulInstaller/pid.tar.gz";
            String strDest = "C:/AskulInstaller/Extracted Files";
            InputStream in = getInputStream(strSourceFile);
            untar(in, strDest);     

        }catch(Exception e) {

            e.printStackTrace();        
            System.out.println(e.getMessage());
        }
    }   
    public static void main(String[] args) {
       new Untargz().run();
    }
}

Once I run this piece of code, this is My Output;

Creating an GZIPInputStream for the file
Reading TarInputStream... 
That destination directory doesn't exist! C:/AskulInstaller/Extracted Files
BUILD SUCCESSFUL (total time: 0 seconds)

When I Manually Create the destination Directory C:/AskulInstaller/Extracted Files

I get this Error Output;

Creating an GZIPInputStream for the file
Reading TarInputStream... 
Processing C:\AskulInstaller\Extracted Files\AskulInstaller\pid\Askul Logs\DbLayer_AskulMain_10_Apr_2013_07_44.log
java.io.FileNotFoundException: C:\AskulInstaller\Extracted Files\AskulInstaller\pid\Askul Logs\DbLayer_AskulMain_10_Apr_2013_07_44.log (The system cannot find the path specified)
C:\AskulInstaller\Extracted Files\AskulInstaller\pid\Askul Logs\DbLayer_AskulMain_10_Apr_2013_07_44.log (The system cannot find the path specified)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:165)
    at untargz.Untargz.untar(Untargz.java:37)
    at untargz.Untargz.run(Untargz.java:55)
    at untargz.Untargz.main(Untargz.java:64)

Is there a way I am supposed to place My directories so that the extraction Happens or what exactly is My Mistake?

If the tar file contains an entry for a file foo/bar.txt but doesn't contain a previous directory entry for foo/ then your code will be trying to create a file in a directory that doesn't exist. Try adding

destFile.getParentFile().mkdirs();

just before you create the FileOutputStream.

Alternatively, if you don't mind your code depending on Ant as a library then you can delegate the whole unpacking process to an Ant task rather than doing it by hand. Something like this (not fully tested):

Project p = new Project();
Untar ut = new Untar();
ut.setProject(p);
ut.setSrc(tarFile);
if(tarFile.getName().endsWith(".gz")) {
  ut.setCompression((UntarCompressionMethod)EnumeratedAttribute.getInstance(UntarCompressionMethod.class, "gzip"));
}
ut.setDest(destDir);

ut.perform();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM