Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As others mentioned, it's not possible to append content to an existing zip (or war). However, it's possible to create a new zip on the fly without temporarily writing extracted content to disk. It's hard to guess how much faster this will be, but it's the fastest you can get (at least as far as I know) with standard Java. As mentioned by Carlos Tasada, SevenZipJBindings might squeeze out you some extra seconds, but porting this approach to SevenZipJBindings will still be faster than using temporary files with the same library.</p> <p>Here's some code that writes the contents of an existing zip (war.zip) and appends an extra file (answer.txt) to a new zip (append.zip). All it takes is Java 5 or later, no extra libraries needed.</p> <pre><code>import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; public class Main { // 4MB buffer private static final byte[] BUFFER = new byte[4096 * 1024]; /** * copy input to output stream - available in several StreamUtils or Streams classes */ public static void copy(InputStream input, OutputStream output) throws IOException { int bytesRead; while ((bytesRead = input.read(BUFFER))!= -1) { output.write(BUFFER, 0, bytesRead); } } public static void main(String[] args) throws Exception { // read war.zip and write to append.zip ZipFile war = new ZipFile("war.zip"); ZipOutputStream append = new ZipOutputStream(new FileOutputStream("append.zip")); // first, copy contents from existing war Enumeration&lt;? extends ZipEntry&gt; entries = war.entries(); while (entries.hasMoreElements()) { ZipEntry e = entries.nextElement(); System.out.println("copy: " + e.getName()); append.putNextEntry(e); if (!e.isDirectory()) { copy(war.getInputStream(e), append); } append.closeEntry(); } // now append some extra content ZipEntry e = new ZipEntry("answer.txt"); System.out.println("append: " + e.getName()); append.putNextEntry(e); append.write("42\n".getBytes()); append.closeEntry(); // close war.close(); append.close(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload