@@ -499,19 +499,15 @@ func ExtractZip(filePath string, location string) (string, error) {
499
499
500
500
var dirList []string
501
501
502
- for _ , f := range r .File {
503
- dirList = append (dirList , f .Name )
504
- }
505
-
506
- basedir := findBaseDir (dirList )
507
-
508
502
for _ , f := range r .File {
509
503
fullname := filepath .Join (location , strings .Replace (f .Name , "" , "" , - 1 ))
510
504
if f .FileInfo ().IsDir () {
505
+ dirList = append (dirList , fullname )
511
506
os .MkdirAll (fullname , 0755 )
512
507
} else {
513
508
_ , err := os .Stat (filepath .Dir (fullname ))
514
509
if err != nil {
510
+ dirList = append (dirList , filepath .Dir (fullname ))
515
511
os .MkdirAll (filepath .Dir (fullname ), 0755 )
516
512
}
517
513
perms := f .FileInfo ().Mode ().Perm ()
@@ -537,26 +533,23 @@ func ExtractZip(filePath string, location string) (string, error) {
537
533
}
538
534
}
539
535
}
536
+ basedir := filepath .Base (findBaseDir (dirList ))
540
537
return filepath .Join (location , basedir ), nil
541
538
}
542
539
543
540
func findBaseDir (dirList []string ) string {
544
541
baseDir := ""
542
+ minLen := 256
545
543
// https://github.com/backdrop-ops/contrib/issues/55#issuecomment-73814500
546
544
dontdiff := []string {"pax_global_header" }
547
- for index := range dirList {
548
- if SliceContains (dontdiff , dirList [ index ] ) {
545
+ for _ , dir := range dirList {
546
+ if SliceContains (dontdiff , dir ) {
549
547
continue
550
548
}
551
- candidateBaseDir := dirList [index ]
552
- for i := index ; i < len (dirList ); i ++ {
553
- if ! strings .Contains (dirList [i ], candidateBaseDir ) {
554
- return baseDir
555
- }
556
- }
557
- // avoid setting the candidate if it is the last file
558
- if dirList [len (dirList )- 1 ] != candidateBaseDir {
559
- baseDir = candidateBaseDir
549
+ //get the shortest string
550
+ if len (dir ) < minLen {
551
+ baseDir = dir
552
+ minLen = len (dir )
560
553
}
561
554
}
562
555
return baseDir
0 commit comments