java代码实现对jar包中的配置文件替换

package com.sf.module.gui.configcopy.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;

public class JarFileReplaceUtil {
	
	/**
	 * 
	 * @param jarPath
	 *            jar包所在绝对路径
	 * @param sourcePath
	 *            confPath配置文件绝对路径
	 * @param destPath
	 *            配置文件jar包 位置config/sysconfig.properties
	 * @throws IOException
	 */
	public static void replaceFile(String jarPath, String sourcePath, String destPath) throws IOException {
		String jarName = jarPath.substring(jarPath.lastIndexOf(File.separator), jarPath.lastIndexOf("."));
		File file = new File(jarPath);
		File destFile = new File(jarPath.substring(0, jarPath.lastIndexOf(File.separator)) + jarName + "_cp.jar");
		file.renameTo(destFile);// 将jar文件名重命名为jarName_cp.jar

		JarFile jarFile = null;
		InputStream in = null;
		JarOutputStream out = null;
		try {
			jarFile = new JarFile(destFile);
			out = new JarOutputStream(new FileOutputStream(file));
			Enumeration<JarEntry> enumeration = jarFile.entries();
			while (enumeration.hasMoreElements()) {
				JarEntry jarEntry = enumeration.nextElement();
				InputStream in_ = null;
				try {
					String jarEntryName = jarEntry.getName();
					System.out.println(jarEntryName);
					if (destPath.equals(jarEntryName)) {
						continue;
					}
					in_ = jarFile.getInputStream(jarEntry);
					out.putNextEntry(jarEntry);
					ConfigCopyUtil.copyFile(in_, out);
				} finally {
					if (in_ != null) {
						try {
							in_.close();
						} catch (IOException e) {
						}
					}
				}
			}
			JarEntry jarEntry = new JarEntry(destPath);
			out.putNextEntry(jarEntry);
			in = new FileInputStream(new File(sourcePath));
			ConfigCopyUtil.copyFile(in, out);

		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
				}
			}
			if (jarFile != null) {
				try {
					jarFile.close();
				} catch (IOException e) {
				}
			}
		}

		destFile.delete();
	}

	public static void main(String[] args) {
		String jarPath = "E:\\com.sf.app.ebar.main_2016.1.13.jar";
		String sourcePath = "E:/gui/uat/main.sysconfig.properties";
		String destPath = "config/sysconfig.properties";
		try {
			JarFileReplaceUtil.replaceFile(jarPath, sourcePath, destPath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
public static void copyFile(InputStream in, OutputStream out) throws IOException {
		int length = 2097152;
		byte[] buffer = new byte[length];
		int len = 0;
		while ((len = in.read(buffer)) > -1) {
			out.write(buffer, 0, len);
		}
}

猜你喜欢

转载自wddpwzzhao123.iteye.com/blog/2271441