Pab*_*oni 6

只需将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 []byte
bs, _ := ioutil.ReadAll(tr)

// convert the []byte to a string
s := string(bs)

如果你需要逐行阅读,那么这会更好:

// create a Scanner for reading line by line
s := bufio.NewScanner(tr)

// line reading loop
for 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 point
if s.Err() != nil {
  // handle it
}
  • @m ..文件的内容(无论是在磁盘上还是在tar文件中)**只是一堆字节,没有更多的内容.请解释原始问题. (3认同)