1. 读取Jar包里的MANIFEST.MF文件(读取EAR包里的文件相同的操作),大概步骤如下:
1)用Jar包的绝对路径作为构参,New一个JarFile类
2)用Jar包里的一个文件文件的相对路径作为参数,调用JarFile类的一个getJarEntry的方法,得到一个JarEntry类
3)再用这个Entry类作为参数,调用JarFile的getInputStream文法,得到这个文件的InputStream
4)接下来对InputSteam的操作与普通读取文件的方法相同
StringBuffer sb = new StringBuffer(); try { JarFile testJarFile = new JarFile("c:/test/common.jar"); JarEntry jarEntry = testJarFile.getJarEntry("META-INF/MANIFEST.MF"); InputStream jarEntryInputStream = testJarFile.getInputStream(jarEntry); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(jarEntryInputStream)); String readLine = null; while ((readLine = bufferedReader.readLine()) != null) { sb.append(readLine); sb.append("\r\n"); } System.out.println(sb.toString()); } catch (IOException e) { e.printStackTrace(); }
2. 输出ear包里的jar到其它目录,与从Jar包读取相似,操作步骤如下
1)用Ear包的绝对路径作为构参,New一个JarFile类
2)用目标Jar包的相对路径作为参数,调用JarFile的getJarEntry方法,得到一个JarEntry类
3)用这个JarEntry作为参数,调用JarFile的getInputStream方法,取得这个Jar包文件的InputStream
4)接下来对InputStream的操作与普通写文件操作相同
try { JarFile testEarFile = new JarFile("C:/test/test.ear"); JarEntry jarEntry = testEarFile.getJarEntry("target.jar"); InputStream jarEntryInputStream = testEarFile.getInputStream(jarEntry); FileOutputStream fileOutputStream = new FileOutputStream("c:/test/target.jar", true); int b = 0; while ((b = jarEntryInputStream.read()) != -1) { fileOutputStream.write(b); } fileOutputStream.flush(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); }
3. 替换Ear包里的某些Jar包, 相对之前几个实例,这个会相对复杂一些,步骤如下:
1)创建一个目标的Ear包File类,然后再得到一个JarOutputStream,接下来会往这里添加需要的Jar包
2)读取源Ear包,然后JarFile类的entries()方法遍历源Ear包的所有文件
3)判断要替换的Jar文件,跳过它,其它的文件马上写入目标的Ear包里
4)关闭源Ear包
5)建立要替换进来的Jar包的JarEntry,用FileInputStream类读取这个Jar包,然后马上写进目标Ear包里
6)Flush JarOutputStream
7)依次关闭其它仍未关闭的资源
try { //先创建目标的ear包,再New一个JarOutputStream,以备输出目标Ear包之用 File targetEarFile = new File("c:/test/target.ear"); targetEarFile.createNewFile(); FileOutputStream targetEarFileOutputStream = new FileOutputStream(targetEarFile); JarOutputStream newJarOutputStream = new JarOutputStream(targetEarFileOutputStream); byte buffer[] = new byte[100]; int bytesRead; //循环读取源Ear包里除开要替换的Jar包外的其它文件,并将这些文件写到目标Ear包里去 JarFile sourceEarFile = new JarFile("c:/test/sourceEar.ear"); Enumeration sourceEarJarEnties = sourceEarFile.entries(); while (sourceEarJarEnties.hasMoreElements()) { JarEntry jarEntry = (JarEntry) sourceEarJarEnties.nextElement(); //要替换Jar包,跳过它 if (jarEntry.toString().equals("sourceJar.jar")) { continue; } //其它文件马上写到目标Ear包里去 InputStream jarInputStream = sourceEarFile.getInputStream(jarEntry); newJarOutputStream.putNextEntry(jarEntry); while ((bytesRead = jarInputStream.read(buffer)) != -1) { newJarOutputStream.write(buffer, 0, bytesRead); } newJarOutputStream.flush(); jarInputStream.close(); } //到这里源Ear包己经没有用了,close掉 sourceEarFile.close(); JarEntry targetJarEntry = new JarEntry("targetJar.jar"); newJarOutputStream.putNextEntry(targetJarEntry); FileInputStream targetJarFileInputStream = new FileInputStream("c:/test/targetJar.jar"); while ((bytesRead = targetJarFileInputStream.read(buffer)) != -1) { newJarOutputStream.write(buffer, 0, bytesRead); } newJarOutputStream.flush(); targetJarFileInputStream.close(); newJarOutputStream.close(); targetEarFileOutputStream.close(); } catch (Exception ex) { ex.printStackTrace(); }