从ear文件中编辑jar的属性文件,最佳方法是什么?

3
我考虑使用Java中的Truezip API来操作ear文件,步骤如下:
  1. 将ear文件解压到临时目录中
  2. 在临时目录中搜索jar文件
  3. 如果在jar文件中找到属性
  4. 则将其解压到临时目录中
  5. 修改该属性
  6. 将jar文件重新打包
  7. 将打包后的jar文件放回ear文件中
或者是否有更好的方法可以使用shell脚本实现?
请提供建议。
谢谢。

Shell脚本是实现一组标准步骤的非常好的方式,就像您上面描述的那样。问题在于,其中一些步骤需要中高级技能来实现。祝你好运。 - shellter
有关使用Java执行这些任务的建议吗? - Tom
将Java作为标签添加到您的问题中?祝你好运! - shellter
2个回答

2
使用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();
    }
}

不错的提示,我得把True Zip加入我的工具清单。 - Paul Gregoire

0

我参考了@Christian Schlichtherle的答案,开始了解我想要实现的内容,但True Zip的使用方式已经有了很大的变化。我想在这里发布我需要做的事情,希望能帮助到别人。

你需要创建一个继承TApplication的类。在我的情况下,我将其设置为抽象类,以便在我的实现逻辑类中重用设置代码。

Application.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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接