1. 程式人生 > >Java實現動態修改Jar包內文件內容

Java實現動態修改Jar包內文件內容

文件夾 finall 寫入 prope 某個文件 prop steam hang 修改


import java.io.*;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;

/**
 * jarPath: jar包所在路徑
 * jarFilePath: jar中想要修改文件所在的路徑
 * regex:正則表達式
 * replacement:替換的字符串
 * 註意:Jar包內的jar包內的文件不適用!
 */
public class JarTool {

    public void change(String jarPath, String jarFilePath, String regex, String replacement) throws IOException {
        File file = new File(jarPath);
        JarFile jarFile = new JarFile(file);// 通過jar包的路徑 創建Jar包實例
        change(jarFile, jarFilePath, regex, replacement);
    }

    public void change(JarFile jarFile, String jarFilePath, String regex, String replacement) throws IOException {
        JarEntry entry = jarFile.getJarEntry(jarFilePath);//通過某個文件在jar包中的位置來獲取這個文件
        //創建該文件輸入流
        InputStream input = jarFile.getInputStream(entry);
        //獲取entries集合lists
        List<JarEntry> lists = new LinkedList<>();
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            lists.add(jarEntry);
        }
        String s = readFile(input, regex, replacement);// 讀取並修改文件內容
        writeFile(lists, jarFilePath, jarFile, s);// 將修改後的內容寫入jar包中的指定文件
        jarFile.close();
    }

    private static String readFile(InputStream input, String regex, String replacement)
            throws IOException {
        InputStreamReader isr = new InputStreamReader(input);
        BufferedReader br = new BufferedReader(isr);
        StringBuilder buf = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            // 此處根據實際需要修改某些行的內容
            buf.append(line);
            buf.append(System.getProperty("line.separator"));
        }
        br.close();
        return buf.toString().replaceAll(regex, replacement);
    }

    private static void writeFile(List<JarEntry> lists, String jarFilePath,
                                 JarFile jarFile, String content) throws IOException {
        FileOutputStream fos = new FileOutputStream(jarFile.getName(), true);
        JarOutputStream jos = new JarOutputStream(fos);
        try {
            for (JarEntry je : lists) {
                if (je.getName().equals(jarFilePath)) {
                    // 將內容寫入文件中
                    jos.putNextEntry(new JarEntry(jarFilePath));
                    jos.write(content.getBytes());
                } else {
                    //表示將該JarEntry寫入jar文件中 也就是創建該文件夾和文件
                    jos.putNextEntry(new JarEntry(je));
                    jos.write(streamToByte(jarFile.getInputStream(je)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 關閉流
            jos.close();
        }
    }

    private static byte[] streamToByte(InputStream inputStream) {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                outSteam.write(buffer, 0, len);
            }
            outSteam.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outSteam.toByteArray();
    }


    public static void main(String[] args) throws IOException {
        JarTool jarTool = new JarTool();
        jarTool.change("D:\\IDEA\\workSpace\\demo.jar"
                , "spring/spring-aop.xml", "expression=\".*\"", "expression=\"%%\"");
    }

}

Java實現動態修改Jar包內文件內容