func Unzip()

in e2e-testing/e2e_util.go [86:137]


func Unzip(l *zap.SugaredLogger, archivePath, destinationFolderPath string) {
	openedArchive, err := zip.OpenReader(archivePath)
	ProcessError(l, err)
	defer openedArchive.Close()

	// Permissions setup
	err = os.MkdirAll(destinationFolderPath, 0o755)
	if err != nil {
		l.Errorf("Could not create folders required to unzip, %v", err)
	}

	// Closure required, so that Close() calls do not pile up when unzipping archives with a lot of files
	extractAndWriteFile := func(f *zip.File) error {
		rc, err := f.Open()
		if err != nil {
			return err
		}
		defer func() {
			if err = rc.Close(); err != nil {
				panic(err)
			}
		}()

		path := filepath.Join(destinationFolderPath, f.Name) //nolint:gosec

		// Check for ZipSlip (Directory traversal)
		if !strings.HasPrefix(path, filepath.Clean(destinationFolderPath)+string(os.PathSeparator)) {
			return fmt.Errorf("illegal file path: %s", path)
		}

		if f.FileInfo().IsDir() {
			if err = os.MkdirAll(path, f.Mode()); err != nil {
				l.Errorf("Could not unzip folder : %v", err)
			}
		} else {
			if err = os.MkdirAll(filepath.Dir(path), f.Mode()); err != nil {
				l.Errorf("Could not unzip file : %v", err)
			}
			f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
			ProcessError(l, err)
			defer f.Close()
			_, err = io.Copy(f, rc) //nolint:gosec
			ProcessError(l, err)
		}
		return nil
	}

	for _, f := range openedArchive.File {
		err := extractAndWriteFile(f)
		ProcessError(l, err)
	}
}