我有一个WAR文件,我需要添加两个文件.目前,我这样做:
File war = new File(DIRECTORY, "server.war"); JarOutputStream zos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(war))); //Add file 1 File file = new File(DIRECTORY, "file1.jar"); InputStream is = new BufferedInputStream(new FileInputStream(file)); ZipEntry e = new ZipEntry("file1.jar"); zos.putNextEntry(e); byte[] buf = new byte[1024]; int len; while ((len = is.read(buf, 0, buf.length)) != -1) { zos.write(buf, 0, len); } is.close(); zos.closeEntry(); //repeat for file 2 zos.close();
结果是前面的内容被破坏了:WAR只有我刚刚添加的2个文件.是否有某种追加模式,我没有使用或什么?
是的,FileOutputStream构造函数有一个额外的布尔参数,它允许你强制它附加到文件而不是覆盖它.将您的代码更改为
JarOutputStream zos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(war, True)));
它应该按照你想要的方式工作.