3

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

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

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

请指教。

谢谢

4

2 回答 2

2

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();
    }
}
于 2011-05-01T17:11:41.290 回答
0

我使用来自@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)},
                    }));    
    }

}

然后您只需扩展抽象类并实现“工作”方法,如图所示。

更改属性.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;
    }
}
于 2013-09-11T17:09:28.930 回答