Javaでtruezip APIを使用してearファイルを操作することを考えています
- ear を tmp ディレクトリに抽出し、
- 次に、tmp で jar を検索し、
- jar にプロパティが見つかった場合、
- 次に、それをtmpに抽出し、
- そのプロパティを変更する
- それを瓶に詰め直して、
- 次に、瓶を耳に戻します。
または、シェルスクリプトを使用するより良い方法はありますか?
お知らせ下さい。
ありがとう
TrueZIP 7 を使用すると、次のようなものを使用できます。
public static void main(String args[]) throws IOException {
// Remember to add the following dependencies to the class path:
// Compile time artifactId(s): truezip-file
// Run time artifactId(s): truezip-kernel, truezip-driver-file, truezip-driver-zip
TFile.setDefaultArchiveDetector(new TDefaultArchiveDetector("ear|jar|war"));
search(new TFile(args[0])); // e.g. "my.ear"
TFile.umount(); // commit changes
}
private void search(TFile entry) throws IOException {
if (entry.isDirectory()) {
for (TFile member : dir.listFiles())
search(member);
} else if (entry.isFile()) {
if (entry.getName().endsWith(".properties");
update(entry);
} // else is special file or non-existent
}
private void update(TFile file) throws IOException {
Properties properties = new Properties();
InputStream in = new TFileInputStream(file);
try {
properties.load(in);
} finally {
in.close();
}
// [your updates here]
OutputStream out = new TFileOutputStream(file);
try {
properties.store(out, "updated");
} finally {
out.close();
}
}
@Christian Schlichtherle からの回答を使用して、達成しようとしていたことを開始しましたが、True Zip の使用法はかなり変わりました。うまくいけば誰かを助けるために、ここに必要なことを投稿したいと思いました。
TApplication を拡張するクラスを作成する必要があります。私の場合は、実装するロジック クラスでセットアップ コードを再利用できるように抽象化しています。
アプリケーション.java :
import de.schlichtherle.truezip.file.TApplication;
import de.schlichtherle.truezip.file.TArchiveDetector;
import de.schlichtherle.truezip.file.TConfig;
import de.schlichtherle.truezip.fs.archive.zip.JarDriver;
import de.schlichtherle.truezip.fs.archive.zip.ZipDriver;
import de.schlichtherle.truezip.socket.sl.IOPoolLocator;
/**
* An abstract class which configures the TrueZIP Path module.
*/
abstract class Application<E extends Exception> extends TApplication<E> {
/**
* Runs the setup phase.
* <p>
* This method is {@link #run run} only once at the start of the life
* cycle.
*/
@Override
protected void setup() {
TConfig.get().setArchiveDetector(
new TArchiveDetector(
TArchiveDetector.NULL,
new Object[][] {
{ "zip", new ZipDriver(IOPoolLocator.SINGLETON)},
{ "ear|jar|war", new JarDriver(IOPoolLocator.SINGLETON)},
}));
}
}
次に、抽象クラスを拡張し、示されているように「work」メソッドを実装します。
ChangeProperty.java :
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.ServiceConfigurationError;
import de.schlichtherle.truezip.file.TFile;
import de.schlichtherle.truezip.file.TFileInputStream;
import de.schlichtherle.truezip.file.TFileOutputStream;
public class ChangeProperty extends Application<IOException> {
public static void main(String args[]) throws IOException {
try {
System.exit(new ChangeProperty().run(args));
} catch (ServiceConfigurationError e) {
// Ignore this error because what we wanted to accomplish has been done.
}
}
private void search(TFile entry) throws IOException {
System.out.println("Scanning: " + entry);
if (entry.isDirectory()) {
for (TFile member : entry.listFiles())
search(member);
} else if (entry.isFile()) {
if (entry.getName().endsWith(".properties")) {
update(entry);
}
}
}
private void update(TFile file) throws IOException {
System.out.println("Updating: " + file);
Properties properties = new Properties();
InputStream in = new TFileInputStream(file);
try {
properties.load(in);
} finally {
in.close();
}
// [your updates here]
// For example: properties.setProperty(key, newValue);
OutputStream out = new TFileOutputStream(file);
try {
properties.store(out, "updated by loggerlevelchanger");
} finally {
out.close();
}
}
@Override
protected int work(String[] args) throws IOException {
search(new TFile(args[0]));
return 0;
}
}