只需将tar.Reader用作要读取的每个文件的io.Reader。
tr := tar.NewReader(r)// get the next file entry h, _ := tr.Next()
如果您需要整个文件作为字符串:
// read the complete content of the file h.Name into the bs []bytebs, _ := ioutil.ReadAll(tr)// convert the []byte to a strings := string(bs)
如果您需要逐行阅读,则更好:
// create a Scanner for reading line by lines := bufio.NewScanner(tr)// line reading loopfor s.Scan() { // read the current last read line of text l := s.Text() // ...and do something with l}// you should check for error at this pointif s.Err() != nil { // handle it}


