Add -e argument to soong_zip to allow setting an explicit filename
soong_zip normally takes the name of the input source file as the
name of the file in the zip, which is ideal for zipping up
directories but not for constructing arbitrary zip files. Add a
-e argument that explicitly sets the path in the zip file for
the next -f argument.
Bug: 254867347
Test: zip_test.go
Change-Id: If9d62c1a0064a485aebddf6d2a661f63f3e60b0f
diff --git a/zip/zip.go b/zip/zip.go
index 6f1a8ad..5e1a104 100644
--- a/zip/zip.go
+++ b/zip/zip.go
@@ -80,6 +80,7 @@
type FileArg struct {
PathPrefixInZip, SourcePrefixToStrip string
+ ExplicitPathInZip string
SourceFiles []string
JunkPaths bool
GlobDir string
@@ -124,6 +125,10 @@
arg := b.state
arg.SourceFiles = []string{name}
b.fileArgs = append(b.fileArgs, arg)
+
+ if b.state.ExplicitPathInZip != "" {
+ b.state.ExplicitPathInZip = ""
+ }
return b
}
@@ -189,6 +194,12 @@
return b
}
+// ExplicitPathInZip sets the path in the zip file for the next File call.
+func (b *FileArgsBuilder) ExplicitPathInZip(s string) *FileArgsBuilder {
+ b.state.ExplicitPathInZip = s
+ return b
+}
+
func (b *FileArgsBuilder) Error() error {
if b == nil {
return nil
@@ -425,7 +436,9 @@
var dest string
- if fa.JunkPaths {
+ if fa.ExplicitPathInZip != "" {
+ dest = fa.ExplicitPathInZip
+ } else if fa.JunkPaths {
dest = filepath.Base(src)
} else {
var err error