Add -stripDir and -zipToNotStrip option in merge_zips.

In some cases, we need delete META-INF dir to void mis-using some
classes files in the Java runtime.

For now, when we delete META-INF, we need keep Manifest file in the jar
since merge_zips doesn't have ability to add a default one.

Bug: b/65455145
Test: m clean && m -j

Change-Id: Iea44b1a98d348cd8df1fa7374907733b1add6935
diff --git a/cmd/merge_zips/merge_zips.go b/cmd/merge_zips/merge_zips.go
index 3c1cb9a..9fd5ddd 100644
--- a/cmd/merge_zips/merge_zips.go
+++ b/cmd/merge_zips/merge_zips.go
@@ -19,6 +19,7 @@
 	"fmt"
 	"log"
 	"os"
+	"path/filepath"
 	"sort"
 	"strings"
 
@@ -26,26 +27,40 @@
 	"android/soong/third_party/zip"
 )
 
-type strip struct{}
+type stripDir struct{}
 
-func (s *strip) String() string {
+func (s *stripDir) String() string {
 	return `""`
 }
 
-func (s *strip) Set(path_prefix string) error {
-	strippings = append(strippings, path_prefix)
+func (s *stripDir) Set(dir string) error {
+	stripDirs = append(stripDirs, filepath.Clean(dir))
+
+	return nil
+}
+
+type zipToNotStrip struct{}
+
+func (s *zipToNotStrip) String() string {
+	return `""`
+}
+
+func (s *zipToNotStrip) Set(zip_path string) error {
+	zipsToNotStrip[zip_path] = true
 
 	return nil
 }
 
 var (
-	sortEntries = flag.Bool("s", false, "sort entries (defaults to the order from the input zip files)")
-	emulateJar  = flag.Bool("j", false, "sort zip entries using jar ordering (META-INF first)")
-	strippings  []string
+	sortEntries    = flag.Bool("s", false, "sort entries (defaults to the order from the input zip files)")
+	emulateJar     = flag.Bool("j", false, "sort zip entries using jar ordering (META-INF first)")
+	stripDirs      []string
+	zipsToNotStrip = make(map[string]bool)
 )
 
 func init() {
-	flag.Var(&strip{}, "strip", "the prefix of file path to be excluded from the output zip")
+	flag.Var(&stripDir{}, "stripDir", "the prefix of file path to be excluded from the output zip")
+	flag.Var(&zipToNotStrip{}, "zipToNotStrip", "the input zip file which is not applicable for stripping")
 }
 
 func main() {
@@ -132,11 +147,20 @@
 	orderedMappings := []fileMapping{}
 
 	for _, namedReader := range readers {
+		_, skipStripThisZip := zipsToNotStrip[namedReader.path]
 	FileLoop:
 		for _, file := range namedReader.reader.File {
-			for _, path_prefix := range strippings {
-				if strings.HasPrefix(file.Name, path_prefix) {
-					continue FileLoop
+			if !skipStripThisZip {
+				for _, dir := range stripDirs {
+					if strings.HasPrefix(file.Name, dir+"/") {
+						if emulateJar {
+							if file.Name != jar.MetaDir && file.Name != jar.ManifestFile {
+								continue FileLoop
+							}
+						} else {
+							continue FileLoop
+						}
+					}
 				}
 			}
 			// check for other files or directories destined for the same path