Skip to content

Commit

Permalink
rethink zip extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm authored and cmaglie committed Jan 11, 2018
1 parent e3a3096 commit 9205729
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion sketch_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s *SketchLoader) Run(ctx *types.Context) error {

logger := ctx.GetLogger()

if !utils.SliceContains(allSketchFilePaths, sketchLocation) {
if !utils.SliceContains(allSketchFilePaths, sketchLocation) && !ctx.SketchZipped {
return i18n.ErrorfWithLogger(logger, constants.MSG_CANT_FIND_SKETCH_IN_PATH, sketchLocation, filepath.Dir(sketchLocation))
}

Expand Down
27 changes: 10 additions & 17 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,19 +499,15 @@ func ExtractZip(filePath string, location string) (string, error) {

var dirList []string

for _, f := range r.File {
dirList = append(dirList, f.Name)
}

basedir := findBaseDir(dirList)

for _, f := range r.File {
fullname := filepath.Join(location, strings.Replace(f.Name, "", "", -1))
if f.FileInfo().IsDir() {
dirList = append(dirList, fullname)
os.MkdirAll(fullname, 0755)
} else {
_, err := os.Stat(filepath.Dir(fullname))
if err != nil {
dirList = append(dirList, filepath.Dir(fullname))
os.MkdirAll(filepath.Dir(fullname), 0755)
}
perms := f.FileInfo().Mode().Perm()
Expand All @@ -537,26 +533,23 @@ func ExtractZip(filePath string, location string) (string, error) {
}
}
}
basedir := filepath.Base(findBaseDir(dirList))
return filepath.Join(location, basedir), nil
}

func findBaseDir(dirList []string) string {
baseDir := ""
minLen := 256
// https://github.com/backdrop-ops/contrib/issues/55#issuecomment-73814500
dontdiff := []string{"pax_global_header"}
for index := range dirList {
if SliceContains(dontdiff, dirList[index]) {
for _, dir := range dirList {
if SliceContains(dontdiff, dir) {
continue
}
candidateBaseDir := dirList[index]
for i := index; i < len(dirList); i++ {
if !strings.Contains(dirList[i], candidateBaseDir) {
return baseDir
}
}
// avoid setting the candidate if it is the last file
if dirList[len(dirList)-1] != candidateBaseDir {
baseDir = candidateBaseDir
//get the shortest string
if len(dir) < minLen {
baseDir = dir
minLen = len(dir)
}
}
return baseDir
Expand Down

0 comments on commit 9205729

Please sign in to comment.