0

我正在尝试使用 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)

有没有办法我应该放置我的目录以便提取发生或者我的错误到底是什么?

4

1 回答 1

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();
于 2013-04-10T07:33:50.953 回答