使用java错误提取tar.gz

时间:2013-04-10 07:27:07

标签: java extract tar

我正在尝试使用java提取存档.tar.gz,我收到目录错误,我似乎并不理解。请帮忙。我从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();
    }
}

一旦我运行这段代码,这就是我的输出;

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

当我手动创建目标目录C:/ AskulInstaller / Extracted Files

我收到此错误输出;

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)

我是否有办法放置我的目录,以便提取发生或什么是我的错误?

1 个答案:

答案 0 :(得分:1)

如果tar文件包含文件foo/bar.txt的条目但不包含foo/的先前目录条目,那么您的代码将尝试在不具有该文件的目录中创建文件存在。尝试添加

destFile.getParentFile().mkdirs();

在创建FileOutputStream之前。

或者,如果你不介意你的代码依赖于Ant作为库,那么你可以将整个解包过程委托给Ant任务而不是手工完成。像这样(没有经过全面测试):

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();