Skip to content

Commit 69e310f

Browse files
committed
Extract file check to shouldExtractPathInZip() method
1 parent d007be2 commit 69e310f

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

file.go

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ func downloadGithubZipFile(gitHubCommit GitHubCommit, gitHubToken string, instan
6161
return zipFilePath, nil
6262
}
6363

64+
func shouldExtractPathInZip(pathPrefix string, zipPath *zip.File) bool {
65+
//
66+
// We need to return true (i.e extract file) based on the following conditions:
67+
//
68+
// The current archive item is a directory.
69+
// Archive item's path name will always be appended with a "/", so we use
70+
// this fact to ensure we are working with a full directory name.
71+
// Extract the file if (pathPrefix + "/") is a prefix in path name
72+
//
73+
// The current archive item is a file.
74+
// There are two things possible here:
75+
// 1 User specified a filename that is an exact match for the current archive file,
76+
// we need to extract this file.
77+
// 2 The current archive filename is not a exact match to the user supplied filename.
78+
// Check if (pathPrefix + "/") is a prefix in f.Name, if yes, we extract this file.
79+
80+
zipPathIsFile := !zipPath.FileInfo().IsDir()
81+
return (zipPathIsFile && zipPath.Name == pathPrefix) || strings.Index(zipPath.Name, pathPrefix + "/") == 0
82+
}
83+
6484
// Decompress the file at zipFileAbsPath and move only those files under filesToExtractFromZipPath to localPath
6585
func extractFiles(zipFilePath, filesToExtractFromZipPath, localPath string) error {
6686

@@ -86,37 +106,8 @@ func extractFiles(zipFilePath, filesToExtractFromZipPath, localPath string) erro
86106
// printing some of their contents.
87107
for _, f := range r.File {
88108

89-
//
90-
// Skip the current archive item being processed based on
91-
// rules described in comments below.
92-
//
93-
if f.FileInfo().IsDir() {
94-
// The current archive item is a directory.
95-
// Archive item's f.Name will always be appended with a "/", so we use
96-
// that fact to ensure we are working with a full directory name. Skip
97-
// to next item if (pathPrefix + "/") is not a prefix in f.Name
98-
if strings.Index(f.Name, pathPrefix + "/") != 0 {
99-
continue
100-
}
101-
} else {
102-
// The current archive item is a file.
103-
// There are three things possible here:
104-
// 1 User specified a filename using --source-path option and if we hit
105-
// that file in archive, we need to process this file, so do not skip.
106-
// 2 We do additional checks if we did not hit the exact file specified by user:
107-
// 2a User specified a directory (or wants to download full repo);
108-
// the (pathPrefix + "/") is a prefix in f.Name, we want to process this
109-
// file, so not skipping.
110-
// 2b User specified either file or directory.
111-
// (pathPrefix + "/") is not a prefix in the current item's f.Name, we
112-
// have hit a file that is not within the folder that the user specified,
113-
// so we skip it.
114-
if f.Name != pathPrefix {
115-
if strings.Index(f.Name, pathPrefix + "/") != 0 {
116-
continue
117-
}
118-
}
119-
}
109+
// check if current archive file needs to be extracted
110+
if shouldExtractPathInZip(pathPrefix, f) {
120111

121112
if f.FileInfo().IsDir() {
122113
// Create a directory
@@ -143,6 +134,7 @@ func extractFiles(zipFilePath, filesToExtractFromZipPath, localPath string) erro
143134
return fmt.Errorf("Failed to write file: %s", err)
144135
}
145136
}
137+
}
146138
}
147139

148140
return nil

0 commit comments

Comments
 (0)