package archive import ( "archive/tar" "bytes" "compress/gzip" "fmt" "github.com/blakesmith/ar" "io" "log" "strings" ) func InspectPackage(r io.Reader) (string, error) { arReader := ar.NewReader(r) for { header, err := arReader.Next() if err == io.EOF { break } if err != nil { return "", fmt.Errorf("error in inspectPackage loop: %s", err) } if strings.Trim(header.Name, "/") == "control.tar.gz" { return InspectPackageControl(arReader) } } return "", nil } func InspectPackageControl(reader io.Reader) (string, error) { gzf, err := gzip.NewReader(reader) if err != nil { return "", fmt.Errorf("error creating gzip reader: %s", err) } tarReader := tar.NewReader(gzf) var controlBuf bytes.Buffer for { header, err := tarReader.Next() if err == io.EOF { break } if err != nil { return "", fmt.Errorf("failed to inspect package: %s", err) } name := header.Name switch header.Typeflag { case tar.TypeDir: continue case tar.TypeReg: if name == "./control" { io.Copy(&controlBuf, tarReader) return controlBuf.String(), nil } default: log.Printf( "Unable to figure out type : %c in file %s\n", header.Typeflag, name, ) } } return "", nil }