在程序中更新jar文件

我们知道,用ZIP,jar可以将多个文件一起打包,如classes,images etc. 其实我们可以用J2SE的SDK提供的Jar命令来创建Jar文件,尽管我们可以通过该命令添加一个文件到Jar包中,但我们没有很直接的办法在程序中这么做,也没有办法通过Jar命令在Jar中删除一个或多个文件,

下面我们来研究一下如何更新一个Jar:要更新一个Jar,你必须创建原Jar文件的一个拷贝,在我们更新完拷贝后移除原文件,然后将拷贝重命名为原文件名就可以了。(译者注:我曾经试图找过更直接的办法,但是徒劳。只是找到这篇文章的原文) 对于添加一个或多个文件,删除一个或多个文件,思路应该是一样的。我们将举例更新一个Jar文件。

首先我们应该知道如何列出Jar文件中的Entries:

JarFile jar = new JarFile(name); Enumeration e = jar.entries(); while (e.hasMoreElements()) { System.out.println("\t" + entry.getName()); }

然后我们要知道如何写入Jar文件


while (entries.hasMoreElements()) { JarEntry entry = (JarEntry) entries.nextElement(); InputStream is = jar.getInputStream(entry); newJar.putNextEntry(entry); while ((bytesRead = is.read(buffer)) != -1) { newJar.write(buffer, 0, bytesRead); } }


这里的输出是一个JarOutputStream


File tempJar = File.createTempFile(nameOfJar, null); JarOutputStream newJar = new JarOutputStream( new FileOutputStream(tempJar));


在确定更新成功时,我们不要忘记删除原文件,并重新命名临时文件。


if (success) { File origFile = new File(nameOfJar); origFile.delete(); tempJar.renameTo(origFile); }


下边我们将给出完整的代码:

import java.io.*; import java.util.*; import java.util.zip.*; import java.util.jar.*; public class AddFileToJar { public static void main(String args[]) { if (args.length != 2) { System.err.println( "command: java AddFileToJar example.jar filetoadd.txt"); System.exit(-1); } String nameOfJar = args[0]; String nameToAdd = args[1]; File tempJar = null; try { tempJar = File.createTempFile(nameOfJar, null); } catch (IOException e) { System.err.println("Unable to create intermediate file."); System.exit(-2); } JarFile jar = null; try { jar = new JarFile(nameOfJar); } catch (IOException e) { System.err.println("Unable to access original file."); System.exit(-3); } // Only rename file at end on success boolean success = false; try { JarOutputStream newJar = new JarOutputStream( new FileOutputStream(tempJar)); byte buffer[] = new byte[1024]; int bytesRead; try { FileInputStream fis = new FileInputStream(nameToAdd); // Add back the original files Enumeration entries = jar.entries(); while (entries.hasMoreElements()) { // Prompt for copy JarEntry entry = (JarEntry) entries.nextElement(); String name = entry.getName(); System.out.println ("Copy " + name + " into new jar? (y or n)"); BufferedReader reader = new BufferedReader (new InputStreamReader(System.in)); String line = reader.readLine(); if ("n".equals(line)) { // if user enters n, don't copy file, // move to next name System.out.println("Skipping: " + name); continue; } InputStream is = jar.getInputStream(entry); newJar.putNextEntry(entry); while ((bytesRead = is.read(buffer)) != -1) { newJar.write(buffer, 0, bytesRead); } } // Add new file last try { JarEntry entry = new JarEntry(nameToAdd); newJar.putNextEntry(entry); while ((bytesRead = fis.read(buffer)) != -1) { newJar.write(buffer, 0, bytesRead); } } finally { fis.close(); } success = true; } catch (IOException ex) { System.err.println ("Operation aborted due to : " + ex); } finally { try { newJar.close(); } catch (IOException ignored) { } } } catch (IOException ex) { System.err.println( "Can't access new file"); } finally { try { jar.close(); } catch (IOException ignored) { } if (!success) { tempJar.delete(); } } if (success) { File origFile = new File(nameOfJar); origFile.delete(); tempJar.renameTo(origFile); } } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值