jar文件修改
/**
* 写入文件到jar
* @author 林晚枫
* @date 2021年5月31日14:47:10
* @throws IOException
*/
public static void writeFile(List<JarEntry> lists, String jarFilePath,
JarFile jarFile, JarFile jarFileBackup, String content) throws IOException {
FileOutputStream fos = new FileOutputStream(jarFile.getName());
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(je);
jos.write(streamToByte(jarFileBackup.getInputStream(je)));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
jos.close();
fos.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();
}