2012-07-11 22 views
6

私はApache Commons 1.4.1ライブラリを使用して、".tar.gz"ファイルを圧縮および解凍しています。Apache Commonsを使用してTARファイルをuntarする方法

最後のビットに問題があります。TarArchiveInputStreamFileOutputStreamに変換しています。奇妙なことに

、それはこのラインで破壊だ:C:\ DocumentsとSettings \ Administratorのマイドキュメント\ \ JavaWorkspace \ BackupUtility \ untarし

FileOutputStream fout = new FileOutputStream(destPath); 

destPathは、の正規のパスとファイルです\テスト\ SUBDIR \ testinsub.txt

エラーが発生:

Exception in thread "main" java.io.IOException: The system cannot find the path specified 

何ができるか考えてみませんか?それで、なぜその道を見つけることができないのですか?

私は以下の全体の方法を取り入れています(ほとんどがhereから持ち出されています)。

private void untar(File dest) throws IOException { 
    dest.mkdir(); 
    TarArchiveEntry tarEntry = tarIn.getNextTarEntry(); 
    // tarIn is a TarArchiveInputStream 
    while (tarEntry != null) {// create a file with the same name as the tarEntry 
     File destPath = new File(dest.toString() + System.getProperty("file.separator") + tarEntry.getName()); 
     System.out.println("working: " + destPath.getCanonicalPath()); 
     if (tarEntry.isDirectory()) { 
      destPath.mkdirs(); 
     } else { 
      destPath.createNewFile(); 
      FileOutputStream fout = new FileOutputStream(destPath); 
      tarIn.read(new byte[(int) tarEntry.getSize()]); 
      fout.close(); 
     } 
     tarEntry = tarIn.getNextTarEntry(); 
    } 
    tarIn.close(); 
} 
+0

これを尋ねるのは恥ずかしいですが、私はあなたのコードサンプルを使ってみました。私が作業していた特定の 'gzip'ファイルがあれば動作していました。これは、inputStreamで読み込まれたコンテンツがある場合、 'fout.write(...) 'を呼び出すことなくどのように動作しますか? [answer @ user1894600 suggests](http://stackoverflow.com/a/14211580/320399)では、彼は明示的に 'write(...)'を呼び出して、メモリに読み込まれたバイト配列を提供しなければなりません。 – blong

答えて

5

一般的なポイントのカップルは、なぜあなたはあなたが親ファイルを作成し、与えたいFileの名前を定義することができperfectly usable constructorがあるFileコンストラクタ、とブードゥー教をしますか?

第2に、ウィンドウ内のパスで空白がどのように処理されるかはあまりよく分かりません。あなたの問題の原因かもしれません。私は上記のコンストラクタを使用して試してみて、それが違いを作るかどうかを確認:File destPath = new File(dest, tarEntry.getName());File destが適切なファイルであり、かつ存在し、あなたによってアクセス可能であることを仮定し

第三に、あなたはFileオブジェクトに何かをする前に確認する必要があります。それが存在する場合、それがアクセス可能である場合。それは最終的にあなたが問題を特定するのに役立ちます。

+0

お返事ありがとうございます。私はモジュールを書き直すことにしました。私はFileオブジェクトで遊んではいけないというアドバイスを受けたので、答えを正しいものとしてマークします(原則に基づいて) – Redandwhite

+0

助けてくれてうれしいです。最後にすべてうまくいくことを願っています。幸運:) – posdef

+0

私は.tarファイルを.tar.gzで解凍するのと同じコードを使用しています。そして、私はこの行から、新しいファイル(dest、tarEntry.getName())にファイル名ではなくファイルの内容を取得しています。 .tarの内部でファイル名を取得するにはどうすればよいですか? –

13

あなたのプログラムは、Javaヒープ領域エラーがあります。 はので、私はほとんど変化が必要だと思う。 をここにコードがある...

public static void uncompressTarGZ(File tarFile, File dest) throws IOException { 
    dest.mkdir(); 
    TarArchiveInputStream tarIn = null; 

    tarIn = new TarArchiveInputStream(
       new GzipCompressorInputStream(
        new BufferedInputStream(
         new FileInputStream(
          tarFile 
         ) 
        ) 
       ) 
      ); 

    TarArchiveEntry tarEntry = tarIn.getNextTarEntry(); 
    // tarIn is a TarArchiveInputStream 
    while (tarEntry != null) {// create a file with the same name as the tarEntry 
     File destPath = new File(dest, tarEntry.getName()); 
     System.out.println("working: " + destPath.getCanonicalPath()); 
     if (tarEntry.isDirectory()) { 
      destPath.mkdirs(); 
     } else { 
      destPath.createNewFile(); 
      //byte [] btoRead = new byte[(int)tarEntry.getSize()]; 
      byte [] btoRead = new byte[1024]; 
      //FileInputStream fin 
      // = new FileInputStream(destPath.getCanonicalPath()); 
      BufferedOutputStream bout = 
       new BufferedOutputStream(new FileOutputStream(destPath)); 
      int len = 0; 

      while((len = tarIn.read(btoRead)) != -1) 
      { 
       bout.write(btoRead,0,len); 
      } 

      bout.close(); 
      btoRead = null; 

     } 
     tarEntry = tarIn.getNextTarEntry(); 
    } 
    tarIn.close(); 
} 

good l uck

+0

これは、 '' byte [] []と宣言されたときにバイト配列が大きすぎる可能性があるため、ヒープスペースエラーが発生します。btoRead = new byte [(int)tarEntry.getSize() ]; '? – blong

+1

優れた応答。しかし、次の 'deskPath.createNewFile();'は、親ディレクトリ 'if(!destPath.getParentFile()を作成するように変更する必要があります。exists()){ destPath.getParentFile()。mkdirs(); } destPath.createNewFile(); ' –

関連する問題