github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/construct/archive/zip.go (about) 1 package archive 2 3 import ( 4 "archive/zip" 5 "bytes" 6 "fmt" 7 "io/ioutil" 8 "strings" 9 ) 10 11 type Zip struct{} 12 13 func (z *Zip) Unzip(fileArchive []byte, file string) ([]byte, error) { 14 archiveBuffer := bytes.NewReader(fileArchive) 15 zipReader, err := zip.NewReader(archiveBuffer, int64(len(fileArchive))) 16 if err != nil { 17 return nil, fmt.Errorf("invalid zip archive: %s", err) 18 } 19 20 for _, f := range zipReader.File { 21 if strings.HasSuffix(f.Name, file) { 22 // This scope is currently not testable 23 r, err := f.Open() 24 if err != nil { 25 return nil, fmt.Errorf("could not open %s in zip archive: %s", file, err) 26 } 27 data, err := ioutil.ReadAll(r) 28 if err != nil { 29 return nil, fmt.Errorf("could not read content of %s in zip archive: %s", file, err) 30 } 31 32 return data, nil 33 } 34 } 35 36 return nil, fmt.Errorf("could not find %s in zip archive", file) 37 }