尝试从ear文件中编辑jar中的属性文件。最好的办法吗?

时间:2011-04-20 13:54:29

标签: java shell jar ear truezip

我正在考虑使用Java中的truezip API来通过

操作ear文件
  1. 将ear提取到tmp目录,
  2. 然后搜索tmp中的jar,
  3. 如果在jar中找到属性,
  4. 然后将其提取到tmp,
  5. 修改该属性
  6. 然后把它装回罐子里,
  7. 然后将罐装回耳中。
  8. OR有更好的方法来使用shell脚本吗?

    请告知。

    由于

2 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分: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)},
                    }));    
    }

}

然后你只需扩展抽象类并实现&#34; work&#34;方法如图所示。

<强> 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;
    }
}