@@ -477,19 +477,15 @@ func ExtractZip(filePath string, location string) (string, error) {
477
477
478
478
var dirList []string
479
479
480
- for _ , f := range r .File {
481
- dirList = append (dirList , f .Name )
482
- }
483
-
484
- basedir := findBaseDir (dirList )
485
-
486
480
for _ , f := range r .File {
487
481
fullname := filepath .Join (location , strings .Replace (f .Name , "" , "" , - 1 ))
488
482
if f .FileInfo ().IsDir () {
483
+ dirList = append (dirList , fullname )
489
484
os .MkdirAll (fullname , 0755 )
490
485
} else {
491
486
_ , err := os .Stat (filepath .Dir (fullname ))
492
487
if err != nil {
488
+ dirList = append (dirList , filepath .Dir (fullname ))
493
489
os .MkdirAll (filepath .Dir (fullname ), 0755 )
494
490
}
495
491
perms := f .FileInfo ().Mode ().Perm ()
@@ -515,26 +511,23 @@ func ExtractZip(filePath string, location string) (string, error) {
515
511
}
516
512
}
517
513
}
514
+ basedir := filepath .Base (findBaseDir (dirList ))
518
515
return filepath .Join (location , basedir ), nil
519
516
}
520
517
521
518
func findBaseDir (dirList []string ) string {
522
519
baseDir := ""
520
+ minLen := 256
523
521
// https://github.com/backdrop-ops/contrib/issues/55#issuecomment-73814500
524
522
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 ) {
527
525
continue
528
526
}
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 )
538
531
}
539
532
}
540
533
return baseDir
0 commit comments