//Create a new Header instance header := &tar.Header{ Name: "file.txt", Mode: 0666, Size: int64(fileSize), } //Print the header information fmt.Printf("Name: %s, Mode: %o, Size: %d", header.Name, header.Mode, header.Size)
//Extract header information from a TAR archive file tarFile, err := os.Open("archive.tar") if err != nil { log.Fatal(err) } tarReader := tar.NewReader(tarFile) for { header, err := tarReader.Next() if err == io.EOF { break } fmt.Println(header.Name) //Prints the file name for each file in the archive }In this code example, we are extracting header information from a TAR archive file using the archive/tar package. We first open the archive file using the os package and create a new tar.Reader instance. We then loop through every file in the archive using the Next() method, which returns the Header struct for each file. Finally, we can access the file name by printing the header.Name field. Thus, we can see that the archive/tar package is a powerful tool for working with TAR archive files in Go, and it provides a wide range of functionalities for managing the information stored in the archive.