I have been able to loop through the files in a tar file, but I am stuck on how to read the contents of those files as string. I would like to know how to print the contents of the files as a string?
This is my code below
package main
import (
"archive/tar"
"fmt"
"io"
"log"
"os"
"bytes"
"compress/gzip"
)
func main() {
file, err := os.Open("testtar.tar.gz")
archive, err := gzip.NewReader(file)
if err != nil {
fmt.Println("There is a problem with os.Open")
}
tr := tar.NewReader(archive)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("Contents of %s:
", hdr.Name)
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…