2009-08-19 16 views
3

基本的に、junitテストから特定のフォルダに解凍するjarファイルがあります。Javaでjarを解凍する最も簡単な方法

これを行う最も簡単な方法は何ですか? 必要な場合は、無料の第三者図書館を使用します。

答えて

6

java.util.jar.JarFileを使用して、ファイル内のエントリを反復し、それぞれをInputStreamで抽出し、外部ファイルに書き出すことができます。 Apache Commons IOはこれを少し不器用にするユーティリティを提供します。

2

Jarは基本的にZIPアルゴリズムを使用して圧縮されているため、winzipまたはwinrarを使用して抽出することができます。

プログラマチックな方法をお探しの場合は、最初の答えがより正確です。コマンドラインタイプjar xf foo.jarまたはunzip foo.jar

+1

OPのjunitテストから実行する場合は動作しません。 – Chadwick

1

4
ZipInputStream in = null; 
OutputStream out = null; 

try { 
    // Open the jar file 
    String inFilename = "infile.jar"; 
    in = new ZipInputStream(new FileInputStream(inFilename)); 

    // Get the first entry 
    ZipEntry entry = in.getNextEntry(); 

    // Open the output file 
    String outFilename = "o"; 
    out = new FileOutputStream(outFilename); 

    // Transfer bytes from the ZIP file to the output file 
    byte[] buf = new byte[1024]; 
    int len; 
    while ((len = in.read(buf)) > 0) { 
     out.write(buf, 0, len); 
    } 
} catch (IOException e) { 
    // Manage exception 
} finally { 
    // Close the streams 
    if (out != null) { 
     out.close(); 
    } 

    if (in != null) { 
     in.close(); 
    } 
} 
関連する問題