@@ -477,19 +477,15 @@ func ExtractZip(filePath string, location string) (string, error) {
477477
478478 var dirList []string
479479
480- for _ , f := range r .File {
481- dirList = append (dirList , f .Name )
482- }
483-
484- basedir := findBaseDir (dirList )
485-
486480 for _ , f := range r .File {
487481 fullname := filepath .Join (location , strings .Replace (f .Name , "" , "" , - 1 ))
488482 if f .FileInfo ().IsDir () {
483+ dirList = append (dirList , fullname )
489484 os .MkdirAll (fullname , 0755 )
490485 } else {
491486 _ , err := os .Stat (filepath .Dir (fullname ))
492487 if err != nil {
488+ dirList = append (dirList , filepath .Dir (fullname ))
493489 os .MkdirAll (filepath .Dir (fullname ), 0755 )
494490 }
495491 perms := f .FileInfo ().Mode ().Perm ()
@@ -515,26 +511,23 @@ func ExtractZip(filePath string, location string) (string, error) {
515511 }
516512 }
517513 }
514+ basedir := filepath .Base (findBaseDir (dirList ))
518515 return filepath .Join (location , basedir ), nil
519516}
520517
521518func findBaseDir (dirList []string ) string {
522519 baseDir := ""
520+ minLen := 256
523521 // https://github.com/backdrop-ops/contrib/issues/55#issuecomment-73814500
524522 dontdiff := []string {"pax_global_header" }
525- for index := range dirList {
526- if SliceContains (dontdiff , dirList [ index ] ) {
523+ for _ , dir := range dirList {
524+ if SliceContains (dontdiff , dir ) {
527525 continue
528526 }
529- candidateBaseDir := dirList [index ]
530- for i := index ; i < len (dirList ); i ++ {
531- if ! strings .Contains (dirList [i ], candidateBaseDir ) {
532- return baseDir
533- }
534- }
535- // avoid setting the candidate if it is the last file
536- if dirList [len (dirList )- 1 ] != candidateBaseDir {
537- baseDir = candidateBaseDir
527+ //get the shortest string
528+ if len (dir ) < minLen {
529+ baseDir = dir
530+ minLen = len (dir )
538531 }
539532 }
540533 return baseDir
0 commit comments