尝试从 Ear 文件编辑 jar 中的属性文件。最好的方法是什么?

发布于 2024-11-03 02:19:15 字数 254 浏览 1 评论 0原文

我正在考虑使用Java中的truezip API来操作ear文件,将

  1. ear提取到tmp目录中,
  2. 然后搜索tmp中的jar,
  3. 如果在jar中找到属性,
  4. 则将其提取到tmp中,
  5. 修改该属性
  6. 然后将其打包回jar中,
  7. 然后将罐子装回耳朵中。

或者有更好的方法使用 shell 脚本吗?

请指教。

谢谢

I am thinking using truezip API in Java to manipulate with ear file by

  1. extract ear into tmp directory,
  2. then search through jars in tmp,
  3. if found properties in jar,
  4. then extract it into tmp,
  5. modify that property
  6. then pack it back into jar,
  7. then pack jar back into ear.

OR is there a better way in using shell script?

Please advise.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦一生花开无言 2024-11-10 02:19:15

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

Using TrueZIP 7, you could use something like this:

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();
    }
}
梦萦几度 2024-11-10 02:19:15

我使用 @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;
    }
}

I used the answer from @Christian Schlichtherle to get me started on what I was trying to accomplish, but the usage of True Zip has changed quite a bit. I thought I'd post what I needed to do here to hopefully help someone.

You need to create a class that extends TApplication. In my case I'm making it abstract so I can reuse the setup code in my implementing logic classes.

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)},
                    }));    
    }

}

Then you just extend the abstract class and implement the "work" method as shown.

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文