func extractTarball()

in projects/aws/image-builder/builder/manifests.go [122:171]


func extractTarball(tarball, path string) error {
	tarFile, err := os.Open(tarball)
	if err != nil {
		return err
	}
	defer tarFile.Close()

	// Create a new directory to extract the tarball into.
	if err = os.RemoveAll(path); err != nil {
		return err
	}

	err = os.MkdirAll(path, 0o755)
	if err != nil {
		return err
	}

	// Create a tar reader for the tarball.
	tarReader := tar.NewReader(tarFile)

	// Iterate over the tarball's entries.
	for {
		// Get the next header from the tarball.
		header, err := tarReader.Next()
		if err == io.EOF {
			// We've reached the end of the tarball.
			break
		}
		if err != nil {
			return err
		}

		// Create a new file for the tar entry.
		outFile, err := os.Create(filepath.Join(path, header.Name))
		if err != nil {
			return err
		}
		defer outFile.Close()

		// Copy the tar entry's contents to the new file.
		_, err = io.Copy(outFile, tarReader)
		if err != nil {
			return err
		}

		outFile.Chmod(os.FileMode(header.Mode))
	}

	return nil
}