refactor(run download): extract zip extraction func into a separate package

Signed-off-by: Babak K. Shandiz <babakks@github.com>

Co-authored-by: Kynan Ware <bagtoad@github.com>
Co-authored-by: Devraj Mehta <devm33@github.com>
This commit is contained in:
Babak K. Shandiz 2026-01-19 10:28:42 +00:00
parent 559e729f9a
commit 0f32f2ac46
No known key found for this signature in database
GPG key ID: 9472CAEFF56C742E
4 changed files with 10 additions and 5 deletions

Binary file not shown.

View file

@ -1,4 +1,4 @@
package download
package zip
import (
"archive/zip"
@ -17,7 +17,11 @@ const (
execMode os.FileMode = 0755
)
func extractZip(zr *zip.Reader, destDir safepaths.Absolute) error {
// ExtractZip extracts the contents of a zip archive to destDir.
// Files that would result in path traversal are silently skipped.
// Files that would produce any other error cause the extraction to be aborted,
// and the error is returned.
func ExtractZip(zr *zip.Reader, destDir safepaths.Absolute) error {
for _, zf := range zr.File {
fpath, err := destDir.Join(zf.Name)
if err != nil {

View file

@ -1,4 +1,4 @@
package download
package zip
import (
"archive/zip"
@ -19,7 +19,7 @@ func Test_extractZip(t *testing.T) {
require.NoError(t, err)
defer zipFile.Close()
err = extractZip(&zipFile.Reader, extractPath)
err = ExtractZip(&zipFile.Reader, extractPath)
require.NoError(t, err)
_, err = os.Stat(filepath.Join(extractPath.String(), "src", "main.go"))

View file

@ -10,6 +10,7 @@ import (
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/safepaths"
ghzip "github.com/cli/cli/v2/internal/zip"
"github.com/cli/cli/v2/pkg/cmd/run/shared"
)
@ -62,7 +63,7 @@ func downloadArtifact(httpClient *http.Client, url string, destDir safepaths.Abs
if err != nil {
return fmt.Errorf("error extracting zip archive: %w", err)
}
if err := extractZip(zipfile, destDir); err != nil {
if err := ghzip.ExtractZip(zipfile, destDir); err != nil {
return fmt.Errorf("error extracting zip archive: %w", err)
}