Add support for detecting nested java archives (#77)

* with sb build app

* test nested jar support

* pin jdk version during parse test (but dont compare version)
This commit is contained in:
Alex Goodman
2020-07-15 07:42:35 -04:00
committed by GitHub
parent e8d11eec69
commit 66a16a67fd
22 changed files with 1285 additions and 620 deletions
+58
View File
@@ -0,0 +1,58 @@
package file
import (
"archive/zip"
"fmt"
"os"
"sort"
"github.com/anchore/imgbom/internal"
"github.com/anchore/imgbom/internal/log"
)
type ZipFileManifest map[string]os.FileInfo
func newZipManifest() ZipFileManifest {
return make(ZipFileManifest)
}
func (z ZipFileManifest) Add(entry string, info os.FileInfo) {
z[entry] = info
}
func (z ZipFileManifest) GlobMatch(patterns ...string) []string {
uniqueMatches := internal.NewStringSet()
for _, pattern := range patterns {
for entry := range z {
if GlobMatch(pattern, entry) {
uniqueMatches.Add(entry)
}
}
}
results := uniqueMatches.ToSlice()
sort.Strings(results)
return results
}
func NewZipFileManifest(archivePath string) (ZipFileManifest, error) {
zipReader, err := zip.OpenReader(archivePath)
manifest := newZipManifest()
if err != nil {
return manifest, fmt.Errorf("unable to open zip archive (%s): %w", archivePath, err)
}
defer func() {
err = zipReader.Close()
if err != nil {
log.Errorf("unable to close zip archive (%s): %w", archivePath, err)
}
}()
for _, file := range zipReader.Reader.File {
manifest.Add(file.Name, file.FileInfo())
}
return manifest, nil
}