diff --git a/archive/tar/reader.go b/archive/tar/reader.go index f817956..c72e002 100644 --- a/archive/tar/reader.go +++ b/archive/tar/reader.go @@ -154,60 +154,48 @@ func (tr *Reader) Next() (*Header, error) { } return hdr, nil case TypeGNULongName: - var b *bytes.Buffer - if tr.RawAccounting { - b = bytes.NewBuffer(tr.RawBytes()) - } // We have a GNU long name header. Its contents are the real file name. realname, err := ioutil.ReadAll(tr) if err != nil { return nil, err } + var buf []byte if tr.RawAccounting { - if _, err = tr.rawBytes.Write(b.Bytes()); err != nil { - return nil, err - } if _, err = tr.rawBytes.Write(realname); err != nil { return nil, err } - b.Reset() - b.Write(tr.RawBytes()) + buf = make([]byte, tr.rawBytes.Len()) + copy(buf[:], tr.RawBytes()) } hdr, err := tr.Next() // since the above call to Next() resets the buffer, we need to throw the bytes over if tr.RawAccounting { - b.Write(tr.RawBytes()) - if _, err = tr.rawBytes.Write(b.Bytes()); err != nil { + buf = append(buf, tr.RawBytes()...) + if _, err = tr.rawBytes.Write(buf); err != nil { return nil, err } } hdr.Name = cString(realname) return hdr, err case TypeGNULongLink: - var b *bytes.Buffer - if tr.RawAccounting { - b = bytes.NewBuffer(tr.RawBytes()) - } // We have a GNU long link header. realname, err := ioutil.ReadAll(tr) if err != nil { return nil, err } + var buf []byte if tr.RawAccounting { - if _, err = tr.rawBytes.Write(b.Bytes()); err != nil { - return nil, err - } if _, err = tr.rawBytes.Write(realname); err != nil { return nil, err } - b.Reset() - b.Write(tr.RawBytes()) + buf = make([]byte, tr.rawBytes.Len()) + copy(buf[:], tr.RawBytes()) } hdr, err := tr.Next() // since the above call to Next() resets the buffer, we need to throw the bytes over if tr.RawAccounting { - b.Write(tr.RawBytes()) - if _, err = tr.rawBytes.Write(b.Bytes()); err != nil { + buf = append(buf, tr.RawBytes()...) + if _, err = tr.rawBytes.Write(buf); err != nil { return nil, err } }