MCPcopy Create free account
hub / github.com/cli/cli / extractZip

Function extractZip

pkg/cmd/copilot/copilot.go:378–408  ·  view source on GitHub ↗

extractZip reads a ZIP archive at path and extracts its contents into destDir. It returns an error if the archive cannot be read, or if any file or directory within the archive cannot be created or written.

(path, destDir string)

Source from the content-addressed store, hash-verified

376// It returns an error if the archive cannot be read,
377// or if any file or directory within the archive cannot be created or written.
378func extractZip(path, destDir string) error {
379 zipReader, err := zip.OpenReader(path)
380 if err != nil {
381 return fmt.Errorf("failed to open zip: %w", err)
382 }
383 defer zipReader.Close()
384
385 absPath, err := safepaths.ParseAbsolute(destDir)
386 if err != nil {
387 return err
388 }
389
390 // As of the time of writing, ghzip.ExtractZip will safely skip files that
391 // would result in path traversal. This is an issue for our use-case because
392 // we want to error out before extracting if there's any such file.
393 // To avoid breaking the shared ghzip.ExtractZip code that expects unsafe
394 // paths to be ignored and no error produced, we pre-validate here,
395 // producing an error if any such file is found.
396 for _, f := range zipReader.File {
397 _, err := absPath.Join(f.Name)
398 if err != nil {
399 return err
400 }
401 }
402
403 if err := ghzip.ExtractZip(&zipReader.Reader, absPath); err != nil {
404 return err
405 }
406
407 return nil
408}
409
410// extractTarGz reads a TAR.GZ archive from r and extracts its contents into destDir.
411// It returns an error if the archive cannot be read,

Callers 2

TestExtractZipFunction · 0.85
downloadCopilotFunction · 0.85

Calls 4

ParseAbsoluteFunction · 0.92
JoinMethod · 0.80
ErrorfMethod · 0.65
CloseMethod · 0.65

Tested by 1

TestExtractZipFunction · 0.68