2012-01-12 33 views
5

Javaプログラムのtarファイルのすべてのエントリを表示したいとします。どのように可能ですか? は、zipファイルのために、私は以下のコードを使用することができますjavaでtarファイルのすべてのエントリを表示するには?

ZipFile zf = new ZipFile("ReadZip.zip"); 
Enumeration entries = zf.entries(); 
while (entries.hasMoreElements()) {.....} 

をしかし、私はtarファイルのために確認していません。誰でも助けることができますか?私は使用していますorg.apache.tools.tar.*

答えて

-3

Javaの.jarファイルを読むには、 "jar"ツール...またはUnzipを使用できます。 .Jarファイルは.Zip形式です。

* nix .tarファイルを読むには、 "tar"ツールを使用する必要があります。

Windowsの場合は、7-Zipを試してみることをおすすめします。これは、膨大な数の形式を認識便利なツール... .zipファイル(それゆえもの.jar)とタールの両方を含むです:あなたがプログラム的にそれを行う必要がある場合

http://www.7-zip.org/

、私は、Apache AntのAPIを推測する「タール」行く良い方法です。あなたは

をチェックアウトすることができます

http://www.jajakarta.org/ant/ant-1.6.1/docs/ja/manual/api/org/apache/tools/tar/TarEntry.html

+0

オリジナルポスト明確に「Javaで」と述べ –

2

このAPIは、Java自身使用に非常によく似ています。

オフを開始するには、次の

TarInputStream tis = new TarInputStream(new FileInputStream("myfile.tar")); 
try 
{ 
    TarEntry entry; 
    do 
    { 
     entry = tis.getNextEntry(); 

     //Do something with the entry 
    } 
    while (entry != null); 
} 
finally 
{ 
    tis.close(); 
} 

More examples with different APIs are [here][2]. 
12

Apache Commons Compress (http://commons.apache.org/compress/)は使いやすいです。ここで

はタールのエントリを読んでの例です:

import java.io.FileInputStream; 

import org.apache.commons.compress.archivers.tar.TarArchiveEntry; 
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; 

public class Taread { 
    public static void main(String[] args) { 
     try { 
      TarArchiveInputStream tarInput = new TarArchiveInputStream(new FileInputStream(args[0])); 
      TarArchiveEntry entry; 
      while (null!=(entry=tarInput.getNextTarEntry())) { 
       System.out.println(entry.getName()); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
関連する問題