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 root, err := safepaths.OpenRoot(destDir)
386 if err != nil {
387 return err
388 }
389 defer root.Close()
390
391 // As of the time of writing, ghzip.ExtractZip will safely skip files that
392 // would result in path traversal. This is an issue for our use-case because
393 // we want to error out before extracting if there's any such file.
394 // To avoid breaking the shared ghzip.ExtractZip code that expects unsafe
395 // paths to be ignored and no error produced, we pre-validate here,
396 // producing an error if any such file is found.
397 for _, file := range zipReader.File {
398 if err := root.Validate(file.Name); err != nil {
399 return err
400 }
401 }
402
403 if err := ghzip.ExtractZip(&zipReader.Reader, root); 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

OpenRootFunction · 0.92
ErrorfMethod · 0.65
CloseMethod · 0.65
ValidateMethod · 0.45

Tested by 1

TestExtractZipFunction · 0.68