基本的に、junitテストから特定のフォルダに解凍するjarファイルがあります。Javaでjarを解凍する最も簡単な方法
これを行う最も簡単な方法は何ですか? 必要な場合は、無料の第三者図書館を使用します。
基本的に、junitテストから特定のフォルダに解凍するjarファイルがあります。Javaでjarを解凍する最も簡単な方法
これを行う最も簡単な方法は何ですか? 必要な場合は、無料の第三者図書館を使用します。
java.util.jar.JarFile
を使用して、ファイル内のエントリを反復し、それぞれをInputStream
で抽出し、外部ファイルに書き出すことができます。 Apache Commons IOはこれを少し不器用にするユーティリティを提供します。
Jarは基本的にZIPアルゴリズムを使用して圧縮されているため、winzipまたはwinrarを使用して抽出することができます。
プログラマチックな方法をお探しの場合は、最初の答えがより正確です。コマンドラインタイプjar xf foo.jar
またはunzip foo.jar
。
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();
}
}
Antのunzip taskを使用することから
OPのjunitテストから実行する場合は動作しません。 – Chadwick