public static void modify(ByteSource input,ByteSink output,Map<String,Function<byte[],byte[]>> toModify,Predicate<String> toOmit) throws IOException {
  try (ZipInputStream zipInput=new ZipInputStream(input.openBufferedStream());ZipOutputStream zipOutput=new ZipOutputStream(output.openBufferedStream())){
    while (true) {
      ZipEntry entry=zipInput.getNextEntry();
      if (entry == null) {
        break;
      }
      Function<byte[],byte[]> replacement=toModify.get(entry.getName());
      if (replacement != null) {
        byte[] clean=ByteStreams.toByteArray(zipInput);
        byte[] modified=replacement.apply(clean);
        try (InputStream replacementStream=new ByteArrayInputStream(modified)){
          ZipEntry newEntry=new ZipEntry(entry.getName());
          newEntry.setComment(entry.getComment());
          newEntry.setExtra(entry.getExtra());
          newEntry.setMethod(entry.getMethod());
          newEntry.setTime(entry.getTime());
          zipOutput.putNextEntry(newEntry);
          copy(replacementStream,zipOutput);
        }
       }
 else       if (!toOmit.test(entry.getName())) {
        ZipEntry newEntry=new ZipEntry(entry);
        newEntry.setCompressedSize(-1);
        zipOutput.putNextEntry(newEntry);
        copy(zipInput,zipOutput);
      }
      zipInput.closeEntry();
      zipOutput.closeEntry();
    }
  }
 }

public static void modify ( ByteSource input , ByteSink output , Map < String , Function < byte [ ] , byte [ ] > > toModify , Predicate < String > toOmit ) throws IOException { try ( ZipInputStream zipInput = new ZipInputStream ( input . openBufferedStream ( ) ) ; ZipOutputStream zipOutput = new ZipOutputStream ( output . openBufferedStream ( ) ) ) { while ( true ) { ZipEntry entry = zipInput . getNextEntry ( ) ; if ( entry == null ) { break ; } Function < byte [ ] , byte [ ] > replacement = toModify . get ( entry . getName ( ) ) ; if ( replacement != null ) { byte [ ] clean = ByteStreams . toByteArray ( zipInput ) ; byte [ ] modified = replacement . apply ( clean ) ; try ( InputStream replacementStream = new ByteArrayInputStream ( modified ) ) { ZipEntry newEntry = new ZipEntry ( entry . getName ( ) ) ; newEntry . setComment ( entry . getComment ( ) ) ; newEntry . setExtra ( entry . getExtra ( ) ) ; newEntry . setMethod ( entry . getMethod ( ) ) ; newEntry . setTime ( entry . getTime ( ) ) ; zipOutput . putNextEntry ( newEntry ) ; copy ( replacementStream , zipOutput ) ; } } else if ( ! toOmit . test ( entry . getName ( ) ) ) { ZipEntry newEntry = new ZipEntry ( entry ) ; newEntry . setCompressedSize ( - 1 ) ; zipOutput . putNextEntry ( newEntry ) ; copy ( zipInput , zipOutput ) ; } zipInput . closeEntry ( ) ; zipOutput . closeEntry ( ) ; } } }
modifies only the specified entries in a zip file .
